From 107c31050438e4aa39bc3996c60a6046ce1b92eb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 29 Aug 2025 15:28:51 +0700 Subject: [PATCH 1/4] [Scope] Handle Scope refreshing on Attribute on print string with NOWDOC --- .../Fixture/fixture.php.inc | 51 +++++++++++++++++++ ...ntStringNowDocUnderAttributeTargetTest.php | 28 ++++++++++ .../Source/SomeAttributeTargetRector.php | 38 ++++++++++++++ .../config/configured_rule.php | 10 ++++ 4 files changed, 127 insertions(+) create mode 100644 tests/Issues/PrintStringNowDocUnderAttributeTarget/Fixture/fixture.php.inc create mode 100644 tests/Issues/PrintStringNowDocUnderAttributeTarget/PrintStringNowDocUnderAttributeTargetTest.php create mode 100644 tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php create mode 100644 tests/Issues/PrintStringNowDocUnderAttributeTarget/config/configured_rule.php diff --git a/tests/Issues/PrintStringNowDocUnderAttributeTarget/Fixture/fixture.php.inc b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Fixture/fixture.php.inc new file mode 100644 index 00000000000..09cae7dd7e7 --- /dev/null +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Fixture/fixture.php.inc @@ -0,0 +1,51 @@ + +----- + \ No newline at end of file diff --git a/tests/Issues/PrintStringNowDocUnderAttributeTarget/PrintStringNowDocUnderAttributeTargetTest.php b/tests/Issues/PrintStringNowDocUnderAttributeTarget/PrintStringNowDocUnderAttributeTargetTest.php new file mode 100644 index 00000000000..9dc43a047e6 --- /dev/null +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/PrintStringNowDocUnderAttributeTargetTest.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/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php new file mode 100644 index 00000000000..28065ee0c4c --- /dev/null +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php @@ -0,0 +1,38 @@ +> + */ + public function getNodeTypes(): array + { + return [Attribute::class]; + } + + /** + * @param Attribute $node + */ + public function refactor(Node $node): Node + { + $node->setAttribute('some_key', 'some_value'); + $node->name = new FullyQualified('SomeNewAttribute'); + + return $node; + } +} \ No newline at end of file diff --git a/tests/Issues/PrintStringNowDocUnderAttributeTarget/config/configured_rule.php b/tests/Issues/PrintStringNowDocUnderAttributeTarget/config/configured_rule.php new file mode 100644 index 00000000000..82b3f20e842 --- /dev/null +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(SomeAttributeTargetRector::class); +}; From 85818801173b86aecbe956fffe63ea312d181ad8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 29 Aug 2025 15:30:48 +0700 Subject: [PATCH 2/4] Fix --- src/Application/ChangedNodeScopeRefresher.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Application/ChangedNodeScopeRefresher.php b/src/Application/ChangedNodeScopeRefresher.php index 81c1437ba9c..2c06ca34b11 100644 --- a/src/Application/ChangedNodeScopeRefresher.php +++ b/src/Application/ChangedNodeScopeRefresher.php @@ -149,10 +149,9 @@ private function setLineAttributesOnClass(Class_ $class, Attribute|AttributeGrou $this->simpleCallableNodeTraverser->traverseNodesWithCallable([$class], function (Node $subNode) use ( $node ): Node { - $subNode->setAttributes([ - 'startLine' => $node->getStartLine(), - 'endLine' => $node->getEndLine(), - ]); + $subNode->setAttribute('startLine', $node->getStartLine()); + $subNode->setAttribute('endLine', $node->getEndLine()); + return $subNode; }); } From 8cc5ae54b4b066ae02b3d975b1ff234014c66ce1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 29 Aug 2025 15:32:01 +0700 Subject: [PATCH 3/4] Fix --- .../Source/SomeAttributeTargetRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php index 28065ee0c4c..e3a9532d235 100644 --- a/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php @@ -10,7 +10,7 @@ use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -class SomeAttributeTargetRector extends AbstractRector +final class SomeAttributeTargetRector extends AbstractRector { public function getRuleDefinition(): RuleDefinition { From 3ffe51c9b8a2c12f46f91a75c3a4e81f008e03ea Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 29 Aug 2025 15:33:36 +0700 Subject: [PATCH 4/4] Fix --- .../Source/SomeAttributeTargetRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php index e3a9532d235..6f89467ac4f 100644 --- a/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php +++ b/tests/Issues/PrintStringNowDocUnderAttributeTarget/Source/SomeAttributeTargetRector.php @@ -30,7 +30,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): Node { - $node->setAttribute('some_key', 'some_value'); $node->name = new FullyQualified('SomeNewAttribute'); return $node;