diff --git a/rector.php b/rector.php index 79c435dbc75..413ba7e51ae 100644 --- a/rector.php +++ b/rector.php @@ -11,7 +11,6 @@ use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector; use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; use Rector\PHPUnit\Set\PHPUnitSetList; -use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector; @@ -88,10 +87,6 @@ \Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [ __DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php', ], - - ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class => [ - __DIR__ . '/src/Kernel/RectorKernel.php', - ], ]); $rectorConfig->phpstanConfig(__DIR__ . '/phpstan-for-rector.neon'); diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index 1bf526ed316..5e8bb9fd6c2 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -17,7 +17,7 @@ final class RectorKernel /** * @var string */ - private const CACHE_KEY = 'v14'; + private const CACHE_KEY = 'v15'; private ContainerInterface|null $container = null; diff --git a/src/NodeManipulator/AssignManipulator.php b/src/NodeManipulator/AssignManipulator.php index efb2b9d6978..b8fd5418ff2 100644 --- a/src/NodeManipulator/AssignManipulator.php +++ b/src/NodeManipulator/AssignManipulator.php @@ -21,6 +21,7 @@ use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\FunctionLike; use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer; +use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\Util\MultiInstanceofChecker; use Rector\NodeNameResolver\NodeNameResolver; @@ -44,7 +45,8 @@ public function __construct( private readonly NodeNameResolver $nodeNameResolver, private readonly BetterNodeFinder $betterNodeFinder, private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer, - private readonly MultiInstanceofChecker $multiInstanceofChecker + private readonly MultiInstanceofChecker $multiInstanceofChecker, + private readonly NodeComparator $nodeComparator ) { } @@ -72,8 +74,16 @@ public function isLeftPartOfAssign(Node $node): bool $parentNode, self::MODIFYING_NODE_TYPES )) { - /** @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode */ - return $parentNode->var === $node; + /** + * @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode + * + * Compare start token pos to ensure php_doc_info info not be checked + */ + if ($parentNode->var->getStartTokenPos() !== $node->getStartTokenPos()) { + return false; + } + + return $this->nodeComparator->areNodesEqual($parentNode->var, $node); } if ($this->isOnArrayDestructuring($parentNode)) { diff --git a/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/DoNotReplaceNonReadonlyPropertyToConstantTest.php b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/DoNotReplaceNonReadonlyPropertyToConstantTest.php new file mode 100644 index 00000000000..9841db07e4b --- /dev/null +++ b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/DoNotReplaceNonReadonlyPropertyToConstantTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/Fixture/change_only_type.php.inc b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/Fixture/change_only_type.php.inc new file mode 100644 index 00000000000..1159b551fdd --- /dev/null +++ b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/Fixture/change_only_type.php.inc @@ -0,0 +1,50 @@ + +----- + diff --git a/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/config/configured_rule.php b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/config/configured_rule.php new file mode 100644 index 00000000000..88cf33d2087 --- /dev/null +++ b/tests/Issues/DoNotReplaceNonReadonlyPropertyToConstant/config/configured_rule.php @@ -0,0 +1,14 @@ +rules([ + ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class, + TypedPropertyFromAssignsRector::class, + ]); +};