|
4 | 4 | namespace Rector\DeadCode\Rector\ClassMethod; |
5 | 5 |
|
6 | 6 | use PhpParser\Node; |
7 | | -use PhpParser\Node\Expr\Array_; |
8 | | -use PhpParser\Node\Expr\MethodCall; |
9 | | -use PhpParser\Node\Expr\Variable; |
10 | 7 | use PhpParser\Node\Name\FullyQualified; |
11 | 8 | use PhpParser\Node\Stmt\Class_; |
12 | 9 | use PhpParser\Node\Stmt\ClassMethod; |
13 | 10 | use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; |
14 | 11 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
15 | 12 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 13 | +use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer; |
16 | 14 | use Rector\DeadCode\NodeManipulator\ControllerClassMethodManipulator; |
17 | 15 | use Rector\NodeAnalyzer\ParamAnalyzer; |
18 | | -use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher; |
19 | 16 | use Rector\NodeManipulator\ClassMethodManipulator; |
20 | | -use Rector\PhpParser\Node\BetterNodeFinder; |
21 | 17 | use Rector\PHPStan\ScopeFetcher; |
22 | 18 | use Rector\Rector\AbstractRector; |
23 | 19 | use Rector\ValueObject\MethodName; |
@@ -47,19 +43,14 @@ final class RemoveEmptyClassMethodRector extends AbstractRector |
47 | 43 | /** |
48 | 44 | * @readonly |
49 | 45 | */ |
50 | | - private BetterNodeFinder $betterNodeFinder; |
51 | | - /** |
52 | | - * @readonly |
53 | | - */ |
54 | | - private ArrayCallableMethodMatcher $arrayCallableMethodMatcher; |
55 | | - public function __construct(ClassMethodManipulator $classMethodManipulator, ControllerClassMethodManipulator $controllerClassMethodManipulator, ParamAnalyzer $paramAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, BetterNodeFinder $betterNodeFinder, ArrayCallableMethodMatcher $arrayCallableMethodMatcher) |
| 46 | + private IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer; |
| 47 | + public function __construct(ClassMethodManipulator $classMethodManipulator, ControllerClassMethodManipulator $controllerClassMethodManipulator, ParamAnalyzer $paramAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer) |
56 | 48 | { |
57 | 49 | $this->classMethodManipulator = $classMethodManipulator; |
58 | 50 | $this->controllerClassMethodManipulator = $controllerClassMethodManipulator; |
59 | 51 | $this->paramAnalyzer = $paramAnalyzer; |
60 | 52 | $this->phpDocInfoFactory = $phpDocInfoFactory; |
61 | | - $this->betterNodeFinder = $betterNodeFinder; |
62 | | - $this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher; |
| 53 | + $this->isClassMethodUsedAnalyzer = $isClassMethodUsedAnalyzer; |
63 | 54 | } |
64 | 55 | public function getRuleDefinition() : RuleDefinition |
65 | 56 | { |
@@ -133,16 +124,10 @@ private function shouldSkipNonFinalNonPrivateClassMethod(Class_ $class, ClassMet |
133 | 124 | } |
134 | 125 | private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod) : bool |
135 | 126 | { |
136 | | - $desiredClassMethodName = $this->getName($classMethod); |
137 | | - $className = (string) $this->getName($class); |
138 | 127 | // is method called somewhere else in the class? |
139 | | - foreach ($class->getMethods() as $anotherClassMethod) { |
140 | | - if ($anotherClassMethod === $classMethod) { |
141 | | - continue; |
142 | | - } |
143 | | - if ($this->containsMethodCallOrArrayCallable($anotherClassMethod, $desiredClassMethodName, $className)) { |
144 | | - return \true; |
145 | | - } |
| 128 | + $scope = ScopeFetcher::fetch($classMethod); |
| 129 | + if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($class, $classMethod, $scope)) { |
| 130 | + return \true; |
146 | 131 | } |
147 | 132 | if ($this->classMethodManipulator->isNamedConstructor($classMethod)) { |
148 | 133 | return \true; |
@@ -173,23 +158,4 @@ private function hasDeprecatedAnnotation(ClassMethod $classMethod) : bool |
173 | 158 | } |
174 | 159 | return $phpDocInfo->hasByType(DeprecatedTagValueNode::class); |
175 | 160 | } |
176 | | - private function containsMethodCallOrArrayCallable(ClassMethod $anotherClassMethod, string $desiredClassMethodName, string $className) : bool |
177 | | - { |
178 | | - $scope = ScopeFetcher::fetch($anotherClassMethod); |
179 | | - return (bool) $this->betterNodeFinder->findFirst($anotherClassMethod, function (Node $node) use($desiredClassMethodName, $className, $scope) : bool { |
180 | | - if ($node instanceof Array_) { |
181 | | - return (bool) $this->arrayCallableMethodMatcher->match($node, $scope, $className); |
182 | | - } |
183 | | - if (!$node instanceof MethodCall) { |
184 | | - return \false; |
185 | | - } |
186 | | - if (!$node->var instanceof Variable) { |
187 | | - return \false; |
188 | | - } |
189 | | - if (!$this->isName($node->var, 'this')) { |
190 | | - return \false; |
191 | | - } |
192 | | - return $this->isName($node->name, $desiredClassMethodName); |
193 | | - }); |
194 | | - } |
195 | 161 | } |
0 commit comments