Skip to content

Commit

Permalink
[CodeQuality] Skip unused static methods in LocallyCalledStaticMethod…
Browse files Browse the repository at this point in the history
…ToNonStaticRector, as should be handled by another rule
  • Loading branch information
TomasVotruba committed Feb 11, 2025
1 parent 8efb34e commit 3c20bd8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\Fixture;

final class DoubleStaticMethod
final class DoubleStaticPrivateMethod
{
public function output()
{
$result = self::run();
}

private static function run()
{
self::someStatic();
Expand All @@ -20,8 +25,13 @@ final class DoubleStaticMethod

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\Fixture;

final class DoubleStaticMethod
final class DoubleStaticPrivateMethod
{
public function output()
{
$result = $this->run();
}

private function run()
{
$this->someStatic();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\Fixture;

final class SkipCommented
{
public static function run()
{
$result = self::anotherStatic();

// $result = self::someStatic();
}

private static function anotherStatic(): array
{
return [];
}

private static function someStatic(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -57,7 +58,6 @@ private static function someStatic()
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
Expand Down Expand Up @@ -129,6 +129,10 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): ?
return null;
}

if ($this->isNeverCalled($class, $classMethod)) {
return null;
}

// replace all the calls
$classMethodName = $this->getName($classMethod);
$className = $this->getName($class) ?? '';
Expand Down Expand Up @@ -210,7 +214,7 @@ private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, Cl

$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()) {
Expand Down Expand Up @@ -257,4 +261,35 @@ private function isClassMethodCalledInAnotherStaticClassMethod(Class_ $class, Cl

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) {
if ($node instanceof MethodCall && $node->var instanceof Variable && $this->isName(
$node->var,
'this'
) && $this->isName($node->name, $currentMethodName)) {
return true;
}

if ($node instanceof StaticCall && $this->isNames($node->class, ['self', 'static']) && $this->isName(
$node->name,
$currentMethodName
)) {
return true;
}

return false;
});

return ! $methodCall instanceof Node;
}
}

0 comments on commit 3c20bd8

Please sign in to comment.