13
13
use ShipMonk \PHPStan \DeadCode \Collector \MethodCallCollector ;
14
14
use ShipMonk \PHPStan \DeadCode \Crate \Call ;
15
15
use ShipMonk \PHPStan \DeadCode \Crate \Kind ;
16
+ use ShipMonk \PHPStan \DeadCode \Crate \Method ;
16
17
use ShipMonk \PHPStan \DeadCode \Crate \Visibility ;
17
18
use ShipMonk \PHPStan \DeadCode \Hierarchy \ClassHierarchy ;
18
19
use function array_key_exists ;
@@ -66,7 +67,7 @@ class DeadMethodRule implements Rule
66
67
/**
67
68
* @var array<string, list<string>>
68
69
*/
69
- private array $ methodsToMarkAsUsedCache = [];
70
+ private array $ methodAlternativesCache = [];
70
71
71
72
private bool $ reportTransitivelyDeadMethodAsSeparateError ;
72
73
@@ -149,17 +150,18 @@ public function processNode(
149
150
foreach ($ callsInFile as $ calls ) {
150
151
foreach ($ calls as $ callString ) {
151
152
$ call = Call::fromString ($ callString );
152
-
153
- $ callerKey = $ call ->caller === null || $ this ->isAnonymousClass ($ call ->caller ->className )
154
- ? ''
155
- : $ call ->caller ->toString ();
156
153
$ isWhite = $ this ->isConsideredWhite ($ call );
157
154
158
- foreach ($ this ->getAlternativeCalleeKeys ($ call ) as $ possibleCalleeKey ) {
159
- $ this ->callGraph [$ callerKey ][] = $ possibleCalleeKey ;
155
+ $ alternativeCalleeKeys = $ this ->getAlternativeMethodKeys ($ call ->callee , $ call ->possibleDescendantCall );
156
+ $ alternativeCallerKeys = $ call ->caller !== null ? $ this ->getAlternativeMethodKeys ($ call ->caller , false ) : [];
157
+
158
+ foreach ($ alternativeCalleeKeys as $ alternativeCalleeKey ) {
159
+ foreach ($ alternativeCallerKeys as $ alternativeCallerKey ) {
160
+ $ this ->callGraph [$ alternativeCallerKey ][] = $ alternativeCalleeKey ;
161
+ }
160
162
161
163
if ($ isWhite ) {
162
- $ whiteCallees [] = $ possibleCalleeKey ;
164
+ $ whiteCallees [] = $ alternativeCalleeKey ;
163
165
}
164
166
}
165
167
}
@@ -177,8 +179,8 @@ public function processNode(
177
179
foreach ($ entrypoints as $ entrypoint ) {
178
180
$ call = Call::fromString ($ entrypoint );
179
181
180
- foreach ($ this ->getAlternativeCalleeKeys ($ call ) as $ methodDefinition ) {
181
- unset($ this ->blackMethods [$ methodDefinition ]);
182
+ foreach ($ this ->getAlternativeMethodKeys ($ call-> callee , $ call -> possibleDescendantCall ) as $ alternativeCalleeKey ) {
183
+ unset($ this ->blackMethods [$ alternativeCalleeKey ]);
182
184
}
183
185
184
186
$ this ->markTransitiveCallsWhite ($ call ->callee ->toString ());
@@ -276,32 +278,32 @@ private function isAnonymousClass(string $className): bool
276
278
/**
277
279
* @return list<string>
278
280
*/
279
- private function getAlternativeCalleeKeys ( Call $ call ): array
281
+ private function getAlternativeMethodKeys ( Method $ method , bool $ possibleDescendant ): array
280
282
{
281
- $ calleeCacheKey = "{ $ call -> callee -> className } :: { $ call -> callee -> methodName }" ;
283
+ $ methodKey = $ method -> toString () ;
282
284
283
- if (isset ($ this ->methodsToMarkAsUsedCache [ $ calleeCacheKey ])) {
284
- return $ this ->methodsToMarkAsUsedCache [ $ calleeCacheKey ];
285
+ if (isset ($ this ->methodAlternativesCache [ $ methodKey ])) {
286
+ return $ this ->methodAlternativesCache [ $ methodKey ];
285
287
}
286
288
287
- $ result = [$ this -> getMethodKey ( $ call -> callee -> className , $ call -> callee -> methodName ) ];
289
+ $ result = [$ methodKey ];
288
290
289
- if ($ call -> possibleDescendantCall ) {
290
- foreach ($ this ->classHierarchy ->getClassDescendants ($ call -> callee ->className ) as $ descendantName ) {
291
- $ result [] = $ this ->getMethodKey ($ descendantName , $ call -> callee ->methodName );
291
+ if ($ possibleDescendant ) {
292
+ foreach ($ this ->classHierarchy ->getClassDescendants ($ method ->className ) as $ descendantName ) {
293
+ $ result [] = $ this ->getMethodKey ($ descendantName , $ method ->methodName );
292
294
}
293
295
}
294
296
295
297
// each descendant can be a trait user
296
- foreach ($ result as $ methodDefinition ) {
297
- $ traitMethodKey = $ this ->classHierarchy ->getDeclaringTraitMethodKey ($ methodDefinition );
298
+ foreach ($ result as $ resultKey ) {
299
+ $ traitMethodKey = $ this ->classHierarchy ->getDeclaringTraitMethodKey ($ resultKey );
298
300
299
301
if ($ traitMethodKey !== null ) {
300
302
$ result [] = $ traitMethodKey ;
301
303
}
302
304
}
303
305
304
- $ this ->methodsToMarkAsUsedCache [ $ calleeCacheKey ] = $ result ;
306
+ $ this ->methodAlternativesCache [ $ methodKey ] = $ result ;
305
307
306
308
return $ result ;
307
309
}
0 commit comments