99use PhpParser \Node \Identifier ;
1010use PhpParser \Node \Name ;
1111use PhpParser \Node \Stmt \Class_ ;
12- use PhpParser \ Node \ Stmt \ ClassMethod ;
12+ use PHPStan \ Reflection \ ClassReflection ;
1313use Rector \Configuration \Parameter \FeatureFlags ;
1414use Rector \Enum \ObjectReference ;
15+ use Rector \PHPStan \ScopeFetcher ;
1516use Rector \Rector \AbstractRector ;
1617use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1718use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
1819
1920/**
21+ * @see https://3v4l.org/VbcrN
2022 * @see \Rector\Tests\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector\StaticToSelfStaticMethodCallOnFinalClassRectorTest
2123 */
2224final class StaticToSelfStaticMethodCallOnFinalClassRector extends AbstractRector
@@ -76,8 +78,14 @@ public function refactor(Node $node): ?Class_
7678 }
7779
7880 $ hasChanged = false ;
81+ $ scope = ScopeFetcher::fetch ($ node );
82+ $ classReflection = $ scope ->getClassReflection ();
7983
80- $ this ->traverseNodesWithCallable ($ node ->stmts , function (Node $ subNode ) use (&$ hasChanged , $ node ): ?StaticCall {
84+ if (! $ classReflection instanceof ClassReflection) {
85+ return null ;
86+ }
87+
88+ $ this ->traverseNodesWithCallable ($ node ->stmts , function (Node $ subNode ) use (&$ hasChanged , $ classReflection , $ scope ): ?StaticCall {
8189 if (! $ subNode instanceof StaticCall) {
8290 return null ;
8391 }
@@ -92,15 +100,15 @@ public function refactor(Node $node): ?Class_
92100 }
93101
94102 $ methodName = (string ) $ this ->getName ($ subNode ->name );
95- $ targetClassMethod = $ node ->getMethod ($ methodName );
96103
97- // skip call non-existing method from current class to ensure transformation is safe
98- if (! $ targetClassMethod instanceof ClassMethod) {
104+ if (! $ classReflection ->hasMethod ($ methodName )) {
99105 return null ;
100106 }
101107
108+ $ methodReflection = $ classReflection ->getMethod ($ methodName , $ scope );
109+
102110 // avoid overlapped change
103- if (! $ targetClassMethod ->isStatic ()) {
111+ if (! $ methodReflection ->isStatic ()) {
104112 return null ;
105113 }
106114
0 commit comments