[Privatization][Php71] Handle CountOnNullRector + ChangeReadOnlyVariableWithDefaultValueToConstantRector usage on exactly array (#738)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-23 04:18:56 +07:00 committed by GitHub
parent 1cc465b4d5
commit e267f89b34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 1 deletions

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
@ -220,7 +221,7 @@ CODE_SAMPLE
{
$constructor = $param->getAttribute(AttributeKey::METHOD_NODE);
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($constructor);
if ($phpDocInfo === null) {
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Ternary;
@ -132,6 +133,10 @@ CODE_SAMPLE
return true;
}
if ($funcCall->args[0]->value instanceof ClassConstFetch) {
return true;
}
$alreadyChangedOnCount = $funcCall->getAttribute(self::ALREADY_CHANGED_ON_COUNT);
// check if it has some condition before already, if so, probably it's already handled

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\CountOnNullExactlyArray;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class CountOnNullExactlyArrayTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Rector\Core\Tests\Issues\CountOnNullExactlyArray\Fixture;
final class Fixture
{
public function run()
{
$array = [];
if (count($array) > 0) {
}
}
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\CountOnNullExactlyArray\Fixture;
final class Fixture
{
/**
* @var mixed[]
*/
private const ARRAY = [];
public function run()
{
if (count(self::ARRAY) > 0) {
}
}
}
?>

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangeReadOnlyVariableWithDefaultValueToConstantRector::class);
$services->set(CountOnNullRector::class);
};