17
17
use PHPStan \Collectors \Collector ;
18
18
use PHPStan \Node \MethodCallableNode ;
19
19
use PHPStan \Node \StaticMethodCallableNode ;
20
- use PHPStan \Reflection \MethodReflection ;
21
20
use PHPStan \TrinaryLogic ;
22
21
use PHPStan \Type \Type ;
23
22
use PHPStan \Type \TypeCombinator ;
24
23
use PHPStan \Type \TypeUtils ;
25
24
use ShipMonk \PHPStan \DeadCode \Graph \ClassMethodRef ;
26
25
use ShipMonk \PHPStan \DeadCode \Graph \ClassMethodUsage ;
26
+ use ShipMonk \PHPStan \DeadCode \Graph \UsageOriginDetector ;
27
27
28
28
/**
29
29
* @implements Collector<Node, list<string>>
@@ -33,10 +33,16 @@ class MethodCallCollector implements Collector
33
33
34
34
use BufferedUsageCollector;
35
35
36
+ private UsageOriginDetector $ usageOriginDetector ;
37
+
36
38
private bool $ trackMixedAccess ;
37
39
38
- public function __construct (bool $ trackMixedAccess )
40
+ public function __construct (
41
+ UsageOriginDetector $ usageOriginDetector ,
42
+ bool $ trackMixedAccess
43
+ )
39
44
{
45
+ $ this ->usageOriginDetector = $ usageOriginDetector ;
40
46
$ this ->trackMixedAccess = $ trackMixedAccess ;
41
47
}
42
48
@@ -114,7 +120,7 @@ private function registerMethodCall(
114
120
foreach ($ methodNames as $ methodName ) {
115
121
foreach ($ this ->getDeclaringTypesWithMethod ($ scope , $ callerType , $ methodName , TrinaryLogic::createNo ()) as $ className ) {
116
122
$ this ->usageBuffer [] = new ClassMethodUsage (
117
- $ this ->getCaller ($ scope ),
123
+ $ this ->usageOriginDetector -> detectOrigin ($ scope ),
118
124
new ClassMethodRef ($ className , $ methodName , $ possibleDescendantCall ),
119
125
);
120
126
}
@@ -140,7 +146,7 @@ private function registerStaticCall(
140
146
foreach ($ methodNames as $ methodName ) {
141
147
foreach ($ this ->getDeclaringTypesWithMethod ($ scope , $ callerType , $ methodName , TrinaryLogic::createYes ()) as $ className ) {
142
148
$ this ->usageBuffer [] = new ClassMethodUsage (
143
- $ this ->getCaller ($ scope ),
149
+ $ this ->usageOriginDetector -> detectOrigin ($ scope ),
144
150
new ClassMethodRef ($ className , $ methodName , $ possibleDescendantCall ),
145
151
);
146
152
}
@@ -165,7 +171,7 @@ private function registerArrayCallable(
165
171
166
172
foreach ($ this ->getDeclaringTypesWithMethod ($ scope , $ caller , $ methodName , TrinaryLogic::createMaybe ()) as $ className ) {
167
173
$ this ->usageBuffer [] = new ClassMethodUsage (
168
- $ this ->getCaller ($ scope ),
174
+ $ this ->usageOriginDetector -> detectOrigin ($ scope ),
169
175
new ClassMethodRef ($ className , $ methodName , $ possibleDescendantCall ),
170
176
);
171
177
}
@@ -189,7 +195,7 @@ private function registerClone(Clone_ $node, Scope $scope): void
189
195
190
196
foreach ($ this ->getDeclaringTypesWithMethod ($ scope , $ callerType , $ methodName , TrinaryLogic::createNo ()) as $ className ) {
191
197
$ this ->usageBuffer [] = new ClassMethodUsage (
192
- $ this ->getCaller ($ scope ),
198
+ $ this ->usageOriginDetector -> detectOrigin ($ scope ),
193
199
new ClassMethodRef ($ className , $ methodName , true ),
194
200
);
195
201
}
@@ -255,21 +261,4 @@ private function getDeclaringTypesWithMethod(
255
261
return $ result ;
256
262
}
257
263
258
- private function getCaller (Scope $ scope ): ?ClassMethodRef
259
- {
260
- if (!$ scope ->isInClass ()) {
261
- return null ;
262
- }
263
-
264
- if (!$ scope ->getFunction () instanceof MethodReflection) {
265
- return null ;
266
- }
267
-
268
- return new ClassMethodRef (
269
- $ scope ->getClassReflection ()->getName (),
270
- $ scope ->getFunction ()->getName (),
271
- false ,
272
- );
273
- }
274
-
275
264
}
0 commit comments