From 6ea0562ed8ddf6ecfe92f6490a20a90fd5a04192 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Wed, 20 Aug 2025 22:55:27 -0500 Subject: [PATCH] fix: static to self static method call on final class Parent method calls should also be transformed to self::method() calls. --- .../Fixture/parent_static_method.php.inc | 31 +++++++++++++++++++ .../Source/BaseClass.php | 11 +++++++ ...SelfStaticMethodCallOnFinalClassRector.php | 20 ++++++++---- 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/parent_static_method.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Source/BaseClass.php diff --git a/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/parent_static_method.php.inc b/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/parent_static_method.php.inc new file mode 100644 index 00000000000..afb0319859d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Fixture/parent_static_method.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Source/BaseClass.php b/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Source/BaseClass.php new file mode 100644 index 00000000000..1f56d0e22b1 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector/Source/BaseClass.php @@ -0,0 +1,11 @@ +getClassReflection(); - $this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$hasChanged, $node): ?StaticCall { + if (! $classReflection instanceof ClassReflection) { + return null; + } + + $this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (&$hasChanged, $classReflection): ?StaticCall { if (! $subNode instanceof StaticCall) { return null; } @@ -92,15 +100,15 @@ public function refactor(Node $node): ?Class_ } $methodName = (string) $this->getName($subNode->name); - $targetClassMethod = $node->getMethod($methodName); - // skip call non-existing method from current class to ensure transformation is safe - if (! $targetClassMethod instanceof ClassMethod) { + if (! $classReflection->hasNativeMethod($methodName)) { return null; } + $methodReflection = $classReflection->getNativeMethod($methodName); + // avoid overlapped change - if (! $targetClassMethod->isStatic()) { + if (! $methodReflection->isStatic()) { return null; }