From 0056291d30ceab4ee96ca1e05b52ea298e687b75 Mon Sep 17 00:00:00 2001 From: APX403 Date: Wed, 18 Nov 2026 11:41:49 +0200 Subject: [PATCH] Fix class scope refresh skipping class attributes when class node is already in class A changed class that is already in class scope only had its stmts processed, so an attribute group added to it, for example by AnnotationToAttributeRector, kept no scope, and refreshing a node that a later rule changed inside it threw "missing scope required for scope refresh". Process the class attribute groups with the class scope too. Fixes rectorphp/rector#9948 --- src/Application/ChangedNodeScopeRefresher.php | 6 ++++ .../ClassAttributeStringClassNameTest.php | 28 +++++++++++++++++++ .../Fixture/discriminator_map.php.inc | 27 ++++++++++++++++++ .../Source/Page.php | 9 ++++++ .../config/configured_rule.php | 14 ++++++++++ 5 files changed, 84 insertions(+) create mode 100644 tests/Issues/ClassAttributeStringClassName/ClassAttributeStringClassNameTest.php create mode 100644 tests/Issues/ClassAttributeStringClassName/Fixture/discriminator_map.php.inc create mode 100644 tests/Issues/ClassAttributeStringClassName/Source/Page.php create mode 100644 tests/Issues/ClassAttributeStringClassName/config/configured_rule.php diff --git a/src/Application/ChangedNodeScopeRefresher.php b/src/Application/ChangedNodeScopeRefresher.php index be0f30fa624..10730b42da1 100644 --- a/src/Application/ChangedNodeScopeRefresher.php +++ b/src/Application/ChangedNodeScopeRefresher.php @@ -95,6 +95,12 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc // the class node itself is not part of its stmts, e.g. an Enum_ replaced by a new Class_ has no scope yet $node->setAttribute(AttributeKey::SCOPE, $mutatingScope); $this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope); + + // the class attributes are not part of its stmts either, e.g. one converted from an annotation + foreach ($node->attrGroups as $attrGroup) { + $this->phpStanNodeScopeResolver->processNodes($this->resolveStmts($attrGroup), $filePath, $mutatingScope); + } + return; } diff --git a/tests/Issues/ClassAttributeStringClassName/ClassAttributeStringClassNameTest.php b/tests/Issues/ClassAttributeStringClassName/ClassAttributeStringClassNameTest.php new file mode 100644 index 00000000000..a791a4216e4 --- /dev/null +++ b/tests/Issues/ClassAttributeStringClassName/ClassAttributeStringClassNameTest.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/ClassAttributeStringClassName/Fixture/discriminator_map.php.inc b/tests/Issues/ClassAttributeStringClassName/Fixture/discriminator_map.php.inc new file mode 100644 index 00000000000..048a20f7f26 --- /dev/null +++ b/tests/Issues/ClassAttributeStringClassName/Fixture/discriminator_map.php.inc @@ -0,0 +1,27 @@ + +----- + \Rector\Tests\Issues\ClassAttributeStringClassName\Source\Page::class])] +class Statistic +{ +} + +?> diff --git a/tests/Issues/ClassAttributeStringClassName/Source/Page.php b/tests/Issues/ClassAttributeStringClassName/Source/Page.php new file mode 100644 index 00000000000..d80fc2a956a --- /dev/null +++ b/tests/Issues/ClassAttributeStringClassName/Source/Page.php @@ -0,0 +1,9 @@ +withConfiguredRule(AnnotationToAttributeRector::class, [ + new AnnotationToAttribute('Doctrine\ORM\Mapping\DiscriminatorMap'), + ]) + ->withRules([StringClassNameToClassConstantRector::class]);