11
11
use PhpParser \Node \Expr \New_ ;
12
12
use PhpParser \Node \Expr \NullsafeMethodCall ;
13
13
use PhpParser \Node \Expr \StaticCall ;
14
- use PhpParser \Node \Identifier ;
15
14
use PhpParser \Node \Name ;
16
15
use PHPStan \Analyser \Scope ;
17
16
use PHPStan \Collectors \Collector ;
18
17
use PHPStan \Node \ClassMethodsNode ;
19
18
use PHPStan \Node \MethodCallableNode ;
20
19
use PHPStan \Node \StaticMethodCallableNode ;
21
20
use PHPStan \Reflection \ClassReflection ;
22
- use PHPStan \Reflection \ReflectionProvider ;
23
21
use PHPStan \Type \Type ;
24
22
use PHPStan \Type \TypeCombinator ;
25
23
use ShipMonk \PHPStan \DeadCode \Crate \Call ;
31
29
class MethodCallCollector implements Collector
32
30
{
33
31
34
- private ReflectionProvider $ reflectionProvider ;
35
-
36
32
/**
37
33
* @var list<Call>
38
34
*/
39
35
private array $ callsBuffer = [];
40
36
41
- public function __construct (ReflectionProvider $ reflectionProvider )
42
- {
43
- $ this ->reflectionProvider = $ reflectionProvider ;
44
- }
45
-
46
37
public function getNodeType (): string
47
38
{
48
39
return Node::class;
@@ -104,7 +95,7 @@ private function registerMethodCall(
104
95
Scope $ scope
105
96
): void
106
97
{
107
- $ methodName = $ this ->getMethodName ($ methodCall );
98
+ $ methodNames = $ this ->getMethodName ($ methodCall, $ scope );
108
99
109
100
if ($ methodCall instanceof New_) {
110
101
if ($ methodCall ->class instanceof Expr) {
@@ -123,13 +114,15 @@ private function registerMethodCall(
123
114
$ possibleDescendantCall = true ;
124
115
}
125
116
126
- if ($ methodName === null ) {
127
- return ;
128
- }
117
+ foreach ($ methodNames as $ methodName ) {
118
+ foreach ($ this ->getReflectionsWithMethod ($ callerType , $ methodName ) as $ classWithMethod ) {
119
+ if (!$ classWithMethod ->hasMethod ($ methodName )) {
120
+ continue ;
121
+ }
129
122
130
- foreach ( $ this -> getReflectionsWithMethod ( $ callerType , $ methodName ) as $ classWithMethod ) {
131
- $ className = $ classWithMethod -> getMethod ( $ methodName , $ scope )-> getDeclaringClass ()-> getName ( );
132
- $ this -> callsBuffer [] = new Call ( $ className , $ methodName , $ possibleDescendantCall );
123
+ $ className = $ classWithMethod -> getMethod ( $ methodName , $ scope )-> getDeclaringClass ()-> getName ();
124
+ $ this -> callsBuffer [] = new Call ( $ className , $ methodName , $ possibleDescendantCall );
125
+ }
133
126
}
134
127
}
135
128
@@ -138,33 +131,26 @@ private function registerStaticCall(
138
131
Scope $ scope
139
132
): void
140
133
{
141
- $ methodName = $ this ->getMethodName ($ staticCall );
142
-
143
- if ($ methodName === null ) {
144
- return ;
145
- }
134
+ $ methodNames = $ this ->getMethodName ($ staticCall , $ scope );
146
135
147
136
if ($ staticCall ->class instanceof Expr) {
148
137
$ callerType = $ scope ->getType ($ staticCall ->class );
149
- $ classReflections = $ this ->getReflectionsWithMethod ($ callerType , $ methodName );
150
138
$ possibleDescendantCall = true ;
151
139
152
140
} else {
153
- $ className = $ scope ->resolveName ($ staticCall ->class );
141
+ $ callerType = $ scope ->resolveTypeByName ($ staticCall ->class );
154
142
$ possibleDescendantCall = $ staticCall ->class ->toString () === 'static ' ;
155
-
156
- if ($ this ->reflectionProvider ->hasClass ($ className )) {
157
- $ classReflections = [
158
- $ this ->reflectionProvider ->getClass ($ className ),
159
- ];
160
- } else {
161
- $ classReflections = [];
162
- }
163
143
}
164
144
165
- foreach ($ classReflections as $ classWithMethod ) {
166
- $ className = $ classWithMethod ->getMethod ($ methodName , $ scope )->getDeclaringClass ()->getName ();
167
- $ this ->callsBuffer [] = new Call ($ className , $ methodName , $ possibleDescendantCall );
145
+ foreach ($ methodNames as $ methodName ) {
146
+ foreach ($ this ->getReflectionsWithMethod ($ callerType , $ methodName ) as $ classReflection ) {
147
+ if (!$ classReflection ->hasMethod ($ methodName )) {
148
+ continue ;
149
+ }
150
+
151
+ $ className = $ classReflection ->getMethod ($ methodName , $ scope )->getDeclaringClass ()->getName ();
152
+ $ this ->callsBuffer [] = new Call ($ className , $ methodName , $ possibleDescendantCall );
153
+ }
168
154
}
169
155
}
170
156
@@ -200,18 +186,25 @@ private function registerAttribute(Attribute $node, Scope $scope): void
200
186
201
187
/**
202
188
* @param NullsafeMethodCall|MethodCall|StaticCall|New_ $call
189
+ * @return list<string>
203
190
*/
204
- private function getMethodName (CallLike $ call ): ? string
191
+ private function getMethodName (CallLike $ call, Scope $ scope ): array
205
192
{
206
193
if ($ call instanceof New_) {
207
- return '__construct ' ;
194
+ return [ '__construct ' ] ;
208
195
}
209
196
210
- if (!$ call ->name instanceof Identifier) {
211
- return null ;
197
+ if ($ call ->name instanceof Expr) {
198
+ $ possibleMethodNames = [];
199
+
200
+ foreach ($ scope ->getType ($ call ->name )->getConstantStrings () as $ constantString ) {
201
+ $ possibleMethodNames [] = $ constantString ->getValue ();
202
+ }
203
+
204
+ return $ possibleMethodNames ;
212
205
}
213
206
214
- return $ call ->name ->toString ();
207
+ return [ $ call ->name ->toString ()] ;
215
208
}
216
209
217
210
/**
0 commit comments