/
MethodAnalyzer.php
330 lines (276 loc) · 10.1 KB
/
MethodAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace Psalm\Internal\Analyzer;
use LogicException;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Internal\Codebase\InternalCallMapHandler;
use Psalm\Internal\MethodIdentifier;
use Psalm\Issue\InvalidEnumMethod;
use Psalm\Issue\InvalidStaticInvocation;
use Psalm\Issue\MethodSignatureMustOmitReturnType;
use Psalm\Issue\NonStaticSelfCall;
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Storage\MethodStorage;
use UnexpectedValueException;
use function in_array;
use function strtolower;
/**
* @internal
* @extends FunctionLikeAnalyzer<PhpParser\Node\Stmt\ClassMethod>
*/
class MethodAnalyzer extends FunctionLikeAnalyzer
{
// https://github.com/php/php-src/blob/a83923044c48982c80804ae1b45e761c271966d3/Zend/zend_enum.c#L77-L95
private const FORBIDDEN_ENUM_METHODS = [
'__construct',
'__destruct',
'__clone',
'__get',
'__set',
'__unset',
'__isset',
'__tostring',
'__debuginfo',
'__serialize',
'__unserialize',
'__sleep',
'__wakeup',
'__set_state',
];
/** @psalm-external-mutation-free */
public function __construct(
PhpParser\Node\Stmt\ClassMethod $function,
SourceAnalyzer $source,
?MethodStorage $storage = null
) {
$codebase = $source->getCodebase();
$method_name_lc = strtolower((string) $function->name);
$source_fqcln = (string) $source->getFQCLN();
$source_fqcln_lc = strtolower($source_fqcln);
$method_id = new MethodIdentifier($source_fqcln, $method_name_lc);
if (!$storage) {
try {
$storage = $codebase->methods->getStorage($method_id);
} catch (UnexpectedValueException $e) {
$class_storage = $codebase->classlike_storage_provider->get($source_fqcln_lc);
if (!$class_storage->parent_classes) {
throw $e;
}
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
if (!$declaring_method_id) {
throw $e;
}
// happens for fake constructors
$storage = $codebase->methods->getStorage($declaring_method_id);
}
}
parent::__construct($function, $source, $storage);
}
/**
* Determines whether a given method is static or not
* @param array<string> $suppressed_issues
*/
public static function checkStatic(
MethodIdentifier $method_id,
bool $self_call,
bool $is_context_dynamic,
Codebase $codebase,
CodeLocation $code_location,
array $suppressed_issues,
?bool &$is_dynamic_this_method = false
): bool {
$codebase_methods = $codebase->methods;
if ($method_id->fq_class_name === 'Closure'
&& $method_id->method_name === 'fromcallable'
) {
return true;
}
$original_method_id = $method_id;
$method_id = $codebase_methods->getDeclaringMethodId($method_id);
if (!$method_id) {
if (InternalCallMapHandler::inCallMap((string) $original_method_id)) {
return true;
}
throw new LogicException('Declaring method for ' . $original_method_id . ' should not be null');
}
$storage = $codebase_methods->getStorage($method_id);
if (!$storage->is_static) {
if ($self_call) {
if (!$is_context_dynamic) {
if (IssueBuffer::accepts(
new NonStaticSelfCall(
'Method ' . $codebase_methods->getCasedMethodId($method_id) .
' is not static, but is called ' .
'using self::',
$code_location
),
$suppressed_issues
)) {
return false;
}
} else {
$is_dynamic_this_method = true;
}
} else {
if (IssueBuffer::accepts(
new InvalidStaticInvocation(
'Method ' . $codebase_methods->getCasedMethodId($method_id) .
' is not static, but is called ' .
'statically',
$code_location
),
$suppressed_issues
)) {
return false;
}
}
}
return true;
}
/**
* @param string[] $suppressed_issues
* @param lowercase-string|null $calling_method_id
*
*/
public static function checkMethodExists(
Codebase $codebase,
MethodIdentifier $method_id,
CodeLocation $code_location,
array $suppressed_issues,
?string $calling_method_id = null
): ?bool {
if ($codebase->methods->methodExists(
$method_id,
$calling_method_id,
!$calling_method_id
|| $calling_method_id !== strtolower((string) $method_id)
? $code_location
: null,
null,
$code_location->file_path
)) {
return true;
}
if (IssueBuffer::accepts(
new UndefinedMethod('Method ' . $method_id . ' does not exist', $code_location, (string) $method_id),
$suppressed_issues
)) {
return false;
}
return null;
}
public static function isMethodVisible(
MethodIdentifier $method_id,
Context $context,
StatementsSource $source
): bool {
$codebase = $source->getCodebase();
$fq_classlike_name = $method_id->fq_class_name;
$method_name = $method_id->method_name;
if ($codebase->methods->visibility_provider->has($fq_classlike_name)) {
$method_visible = $codebase->methods->visibility_provider->isMethodVisible(
$source,
$fq_classlike_name,
$method_name,
$context,
null
);
if ($method_visible !== null) {
return $method_visible;
}
}
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
if (!$declaring_method_id) {
// this can happen for methods in the callmap that were not reflected
return true;
}
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
$appearing_method_class = null;
if ($appearing_method_id) {
$appearing_method_class = $appearing_method_id->fq_class_name;
// if the calling class is the same, we know the method exists, so it must be visible
if ($appearing_method_class === $context->self) {
return true;
}
}
$declaring_method_class = $declaring_method_id->fq_class_name;
if ($source->getSource() instanceof TraitAnalyzer
&& strtolower($declaring_method_class) === strtolower((string) $source->getFQCLN())
) {
return true;
}
$storage = $codebase->methods->getStorage($declaring_method_id);
switch ($storage->visibility) {
case ClassLikeAnalyzer::VISIBILITY_PUBLIC:
return true;
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
return $context->self && $appearing_method_class === $context->self;
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
if (!$context->self) {
return false;
}
if ($appearing_method_class
&& $codebase->classExtends($appearing_method_class, $context->self)
) {
return true;
}
if ($appearing_method_class
&& !$codebase->classExtends($context->self, $appearing_method_class)
) {
return false;
}
}
return true;
}
/**
* Check that __clone, __construct, and __destruct do not have a return type
* hint in their signature.
*/
public static function checkMethodSignatureMustOmitReturnType(
MethodStorage $method_storage,
CodeLocation $code_location
): void {
if ($method_storage->signature_return_type === null) {
return;
}
if ($method_storage->cased_name === null) {
return;
}
$method_name_lc = strtolower($method_storage->cased_name);
$methodsOfInterest = ['__clone', '__construct', '__destruct'];
if (in_array($method_name_lc, $methodsOfInterest, true)) {
IssueBuffer::maybeAdd(
new MethodSignatureMustOmitReturnType(
'Method ' . $method_storage->cased_name . ' must not declare a return type',
$code_location
)
);
}
}
public function getMethodId(?string $context_self = null): MethodIdentifier
{
$function_name = (string)$this->function->name;
return new MethodIdentifier(
$context_self ?: (string) $this->source->getFQCLN(),
strtolower($function_name)
);
}
public static function checkForbiddenEnumMethod(MethodStorage $method_storage): void
{
if ($method_storage->cased_name === null || $method_storage->location === null) {
return;
}
$method_name_lc = strtolower($method_storage->cased_name);
if (in_array($method_name_lc, self::FORBIDDEN_ENUM_METHODS, true)) {
IssueBuffer::maybeAdd(new InvalidEnumMethod(
'Enums cannot define ' . $method_storage->cased_name,
$method_storage->location,
$method_storage->defining_fqcln . '::' . $method_storage->cased_name
));
}
}
}