Updated Rector to commit 91df6147a4208433347ba68be65d9dcd7b756329

91df6147a4 [CodeQuality] Skip unused static methods in LocallyCalledStaticMethodToNonStaticRector, as should be handled by another rule (#6731)
This commit is contained in:
Tomas Votruba 2025-02-11 14:12:14 +00:00
parent d312db427a
commit 58e3b14615
2 changed files with 23 additions and 3 deletions

View File

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

View File

@ -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
*/