Skip to content

Commit

Permalink
Fix StaticCallOnNonStaticToInstanceCallRector to skip parent's parent…
Browse files Browse the repository at this point in the history
…'s method calls. (#4098)

[Issue 7974](rectorphp/rector#7974)

Co-authored-by: Alexey Klimko <aklimko@sugarcrm.com>
  • Loading branch information
klimslim and Alexey Klimko committed Jun 7, 2023
1 parent 3c9a245 commit 8cc8c7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;

class Grandpa
{
protected function test()
{
}
}

class Father extends Grandpa
{
protected function test()
{
parent::test();
}
}

class Son extends Father
{
protected function test()
{
Grandpa::test();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -167,6 +168,11 @@ private function shouldSkip(string $methodName, string $className, StaticCall $s
return true;
}

$reflection = $scope->getClassReflection();
if ($reflection instanceof ClassReflection && $reflection->isSubclassOf($className)) {
return true;
}

$className = $this->getName($staticCall->class);
if (in_array($className, [ObjectReference::PARENT, ObjectReference::SELF, ObjectReference::STATIC], true)) {
return true;
Expand Down

0 comments on commit 8cc8c7c

Please sign in to comment.