44
55namespace Rector \DeadCode \Rector \ClassMethod ;
66
7+ use PHPStan \Type \ObjectType ;
78use PhpParser \Node ;
89use PhpParser \Node \Expr \MethodCall ;
9- use PhpParser \Node \Expr \Variable ;
10+ use PhpParser \Node \Expr \StaticCall ;
1011use PhpParser \Node \Param ;
1112use PhpParser \Node \Stmt \Class_ ;
1213use PhpParser \Node \Stmt \ClassMethod ;
@@ -129,9 +130,10 @@ private function removeCallerArgs(Class_ $class, ClassMethod $classMethod, array
129130 $ methodName = $ this ->getName ($ classMethod );
130131 $ keysArg = array_keys ($ unusedParameters );
131132
133+ $ classObjectType = new ObjectType ((string ) $ this ->getName ($ class ));
132134 foreach ($ classMethods as $ classMethod ) {
133- /** @var MethodCall[] $callers */
134- $ callers = $ this ->resolveCallers ($ classMethod , $ methodName );
135+ /** @var MethodCall[]|StaticCall[] $callers */
136+ $ callers = $ this ->resolveCallers ($ classMethod , $ methodName, $ classObjectType );
135137 if ($ callers === []) {
136138 continue ;
137139 }
@@ -145,42 +147,42 @@ private function removeCallerArgs(Class_ $class, ClassMethod $classMethod, array
145147 /**
146148 * @param int[] $keysArg
147149 */
148- private function cleanupArgs (MethodCall $ methodCall , array $ keysArg ): void
150+ private function cleanupArgs (MethodCall | StaticCall $ call , array $ keysArg ): void
149151 {
150- if ($ methodCall ->isFirstClassCallable ()) {
152+ if ($ call ->isFirstClassCallable ()) {
151153 return ;
152154 }
153155
154- $ args = $ methodCall ->getArgs ();
156+ $ args = $ call ->getArgs ();
155157 foreach (array_keys ($ args ) as $ key ) {
156158 if (in_array ($ key , $ keysArg , true )) {
157159 unset($ args [$ key ]);
158160 }
159161 }
160162
161163 // reset arg keys
162- $ methodCall ->args = array_values ($ args );
164+ $ call ->args = array_values ($ args );
163165 }
164166
165167 /**
166- * @return MethodCall[]
168+ * @return MethodCall[]|StaticCall[]
167169 */
168- private function resolveCallers (ClassMethod $ classMethod , string $ methodName ): array
170+ private function resolveCallers (ClassMethod $ classMethod , string $ methodName, ObjectType $ classObjectType ): array
169171 {
170- return $ this ->betterNodeFinder ->find ($ classMethod , function (Node $ subNode ) use ($ methodName ): bool {
171- if (! $ subNode instanceof MethodCall) {
172+ return $ this ->betterNodeFinder ->find ($ classMethod , function (Node $ subNode ) use ($ methodName, $ classObjectType ): bool {
173+ if (! $ subNode instanceof MethodCall && ! $ subNode instanceof StaticCall ) {
172174 return false ;
173175 }
174176
175177 if ($ subNode ->isFirstClassCallable ()) {
176178 return false ;
177179 }
178180
179- if (! $ subNode-> var instanceof Variable) {
180- return false ;
181- }
181+ $ nodeToCheck = $ subNode instanceof MethodCall
182+ ? $ subNode -> var
183+ : $ subNode -> class ;
182184
183- if (! $ this ->isName ( $ subNode -> var , ' this ' )) {
185+ if (! $ this ->isObjectType ( $ nodeToCheck , $ classObjectType )) {
184186 return false ;
185187 }
186188
0 commit comments