mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 23:52:23 +00:00
[Privatization][Php71] Handle CountOnNullRector + ChangeReadOnlyVariableWithDefaultValueToConstantRector usage on exactly array (#738)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
1cc465b4d5
commit
e267f89b34
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
34
tests/Issues/CountOnNullExactlyArray/Fixture/fixture.php.inc
Normal file
34
tests/Issues/CountOnNullExactlyArray/Fixture/fixture.php.inc
Normal 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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user