diff --git a/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php b/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php index 4b93aaf2941..cab4d96d362 100644 --- a/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\NodeFinder; use PhpParser\NodeVisitor; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; @@ -123,6 +124,9 @@ CODE_SAMPLE if ($this->isClassMethodCalledInAnotherStaticClassMethod($class, $classMethod)) { return null; } + if ($this->isNeverCalled($class, $classMethod)) { + return null; + } // replace all the calls $classMethodName = $this->getName($classMethod); $className = $this->getName($class) ?? ''; @@ -177,7 +181,7 @@ CODE_SAMPLE $currentClassNamespacedName = (string) $this->getName($class); $currentClassMethodName = $this->getName($classMethod); $isInsideStaticClassMethod = \false; - // check if called stati call somewhere in class, but only in static methods + // check if called static call somewhere in class, but only in static methods foreach ($class->getMethods() as $checkedClassMethod) { // not a problem if (!$checkedClassMethod->isStatic()) { @@ -209,4 +213,20 @@ CODE_SAMPLE } return \false; } + /** + * In case of never called method call, + * it should be skipped and handled by another dead-code rule + */ + private function isNeverCalled(Class_ $class, ClassMethod $classMethod) : bool + { + $currentMethodName = $this->getName($classMethod); + $nodeFinder = new NodeFinder(); + $methodCall = $nodeFinder->findFirst($class, function (Node $node) use($currentMethodName) : bool { + if ($node instanceof MethodCall && $node->var instanceof Variable && $this->isName($node->var, 'this') && $this->isName($node->name, $currentMethodName)) { + return \true; + } + return $node instanceof StaticCall && $this->isNames($node->class, ['self', 'static']) && $this->isName($node->name, $currentMethodName); + }); + return !$methodCall instanceof Node; + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index d6ca7225ca2..0fcb2da331a 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '8efb34e74548d702dcc1fa15f9b6f22eaf2e08e3'; + public const PACKAGE_VERSION = '91df6147a4208433347ba68be65d9dcd7b756329'; /** * @api * @var string */ - public const RELEASE_DATE = '2025-02-11 14:48:34'; + public const RELEASE_DATE = '2025-02-11 14:09:45'; /** * @var int */