From 76f595ddcc25ee765a5e7e061a300c0c618684ec Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Tue, 3 Feb 2026 15:22:49 +0000 Subject: [PATCH] Fix StaticCallOnNonStaticToInstanceCallRector for same-class calls When calling a non-static method statically from within the same class, convert to $this->method() instead of skipping. This eliminates the PHP deprecation warning for non-static method called statically. Calls to ancestor methods (e.g., Grandpa::method() from a descendant) are still skipped as they are valid non-static usage. --- .../Fixture/same_class_non_static.php.inc | 37 +++++++++++++++++++ ...ticCallOnNonStaticToInstanceCallRector.php | 13 ++++++- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/same_class_non_static.php.inc diff --git a/rules-tests/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/same_class_non_static.php.inc b/rules-tests/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/same_class_non_static.php.inc new file mode 100644 index 00000000000..b167c53c62e --- /dev/null +++ b/rules-tests/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/same_class_non_static.php.inc @@ -0,0 +1,37 @@ + +----- +doWork(); + } + + public function doWork() + { + return 'work done'; + } +} + +?> diff --git a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php index 935a42354ed..23d3004f249 100644 --- a/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php +++ b/rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php @@ -10,6 +10,7 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Expr\Variable; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; @@ -123,6 +124,11 @@ public function refactor(Node $node): ?Node return null; } + $classReflection = $scope->getClassReflection(); + if ($classReflection instanceof ClassReflection && $classReflection->getName() === $className) { + return new MethodCall(new Variable('this'), $node->name, $node->args); + } + if ($this->isInstantiable($className, $scope)) { $new = new New_($node->class); return new MethodCall($new, $node->name, $node->args); @@ -168,8 +174,11 @@ private function shouldSkip(string $methodName, string $className, StaticCall $s return true; } - $reflection = $scope->getClassReflection(); - if ($reflection instanceof ClassReflection && $reflection->is($className)) { + $currentClassReflection = $scope->getClassReflection(); + if ($currentClassReflection instanceof ClassReflection + && $this->reflectionProvider->hasClass($className) + && $currentClassReflection->isSubclassOfClass($this->reflectionProvider->getClass($className)) + ) { return true; }