Skip to content

Commit ced707e

Browse files
[DeadCode] Use IsClassMethodUsedAnalyzer service on RemoveEmptyClassMethodRector (#6909)
* [DeadCode] Use IsClassMethodUsedAnalyzer service on RemoveEmptyClassMethodRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent b68824a commit ced707e

2 files changed

Lines changed: 11 additions & 50 deletions

File tree

ecs.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
56
use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer;
67
use Symplify\EasyCodingStandard\Config\ECSConfig;
78

@@ -26,5 +27,10 @@
2627
// double to Double false positive
2728
__DIR__ . '/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php',
2829
],
30+
31+
GeneralPhpdocAnnotationRemoveFixer::class => [
32+
// bug remove @author annotation
33+
__DIR__ . '/src/Util/ArrayParametersMerger.php',
34+
],
2935
])
3036
->withRootFiles();

rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
namespace Rector\DeadCode\Rector\ClassMethod;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\Array_;
9-
use PhpParser\Node\Expr\MethodCall;
10-
use PhpParser\Node\Expr\Variable;
118
use PhpParser\Node\Name\FullyQualified;
129
use PhpParser\Node\Stmt\Class_;
1310
use PhpParser\Node\Stmt\ClassMethod;
1411
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
1512
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1613
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
14+
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
1715
use Rector\DeadCode\NodeManipulator\ControllerClassMethodManipulator;
1816
use Rector\NodeAnalyzer\ParamAnalyzer;
19-
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
2017
use Rector\NodeManipulator\ClassMethodManipulator;
21-
use Rector\PhpParser\Node\BetterNodeFinder;
2218
use Rector\PHPStan\ScopeFetcher;
2319
use Rector\Rector\AbstractRector;
2420
use Rector\ValueObject\MethodName;
@@ -35,8 +31,7 @@ public function __construct(
3531
private readonly ControllerClassMethodManipulator $controllerClassMethodManipulator,
3632
private readonly ParamAnalyzer $paramAnalyzer,
3733
private readonly PhpDocInfoFactory $phpDocInfoFactory,
38-
private readonly BetterNodeFinder $betterNodeFinder,
39-
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher
34+
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer
4035
) {
4136
}
4237

@@ -135,18 +130,10 @@ private function shouldSkipNonFinalNonPrivateClassMethod(Class_ $class, ClassMet
135130

136131
private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod): bool
137132
{
138-
$desiredClassMethodName = $this->getName($classMethod);
139-
$className = (string) $this->getName($class);
140-
141133
// is method called somewhere else in the class?
142-
foreach ($class->getMethods() as $anotherClassMethod) {
143-
if ($anotherClassMethod === $classMethod) {
144-
continue;
145-
}
146-
147-
if ($this->containsMethodCallOrArrayCallable($anotherClassMethod, $desiredClassMethodName, $className)) {
148-
return true;
149-
}
134+
$scope = ScopeFetcher::fetch($classMethod);
135+
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($class, $classMethod, $scope)) {
136+
return true;
150137
}
151138

152139
if ($this->classMethodManipulator->isNamedConstructor($classMethod)) {
@@ -186,36 +173,4 @@ private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
186173

187174
return $phpDocInfo->hasByType(DeprecatedTagValueNode::class);
188175
}
189-
190-
private function containsMethodCallOrArrayCallable(ClassMethod $anotherClassMethod, string $desiredClassMethodName, string $className): bool
191-
{
192-
$scope = ScopeFetcher::fetch($anotherClassMethod);
193-
return (bool) $this->betterNodeFinder->findFirst($anotherClassMethod, function (Node $node) use (
194-
$desiredClassMethodName,
195-
$className,
196-
$scope
197-
): bool {
198-
if ($node instanceof Array_) {
199-
return (bool) $this->arrayCallableMethodMatcher->match(
200-
$node,
201-
$scope,
202-
$className
203-
);
204-
}
205-
206-
if (! $node instanceof MethodCall) {
207-
return false;
208-
}
209-
210-
if (! $node->var instanceof Variable) {
211-
return false;
212-
}
213-
214-
if (! $this->isName($node->var, 'this')) {
215-
return false;
216-
}
217-
218-
return $this->isName($node->name, $desiredClassMethodName);
219-
});
220-
}
221176
}

0 commit comments

Comments
 (0)