55namespace Rector \DeadCode \Rector \ClassMethod ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \Expr \Array_ ;
9- use PhpParser \Node \Expr \MethodCall ;
10- use PhpParser \Node \Expr \Variable ;
118use PhpParser \Node \Name \FullyQualified ;
129use PhpParser \Node \Stmt \Class_ ;
1310use PhpParser \Node \Stmt \ClassMethod ;
1411use PHPStan \PhpDocParser \Ast \PhpDoc \DeprecatedTagValueNode ;
1512use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1613use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
14+ use Rector \DeadCode \NodeAnalyzer \IsClassMethodUsedAnalyzer ;
1715use Rector \DeadCode \NodeManipulator \ControllerClassMethodManipulator ;
1816use Rector \NodeAnalyzer \ParamAnalyzer ;
19- use Rector \NodeCollector \NodeAnalyzer \ArrayCallableMethodMatcher ;
2017use Rector \NodeManipulator \ClassMethodManipulator ;
21- use Rector \PhpParser \Node \BetterNodeFinder ;
2218use Rector \PHPStan \ScopeFetcher ;
2319use Rector \Rector \AbstractRector ;
2420use 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