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; }