forked from vimeo/psalm
/
ExpressionScanner.php
332 lines (287 loc) · 12.4 KB
/
ExpressionScanner.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
331
332
<?php
namespace Psalm\Internal\PhpVisitor\Reflector;
use PhpParser;
use Psalm\Aliases;
use Psalm\Codebase;
use Psalm\Config;
use Psalm\Exception\FileIncludeException;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\IncludeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\SimpleTypeInferer;
use Psalm\Internal\Codebase\InternalCallMapHandler;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Storage\FileStorage;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type;
use function assert;
use function defined;
use function dirname;
use function explode;
use function implode;
use function in_array;
use function preg_match;
use function strpos;
use function strtolower;
use function substr;
use const DIRECTORY_SEPARATOR;
class ExpressionScanner
{
public static function scan(
Codebase $codebase,
FileScanner $file_scanner,
FileStorage $file_storage,
Aliases $aliases,
PhpParser\Node\Expr $node,
?FunctionLikeStorage $functionlike_storage,
?int $skip_if_descendants
): void {
if ($node instanceof PhpParser\Node\Expr\Include_ && !$skip_if_descendants) {
self::visitInclude(
$codebase,
$file_storage,
$node,
$file_scanner->will_analyze
);
} elseif ($node instanceof PhpParser\Node\Expr\Yield_ || $node instanceof PhpParser\Node\Expr\YieldFrom) {
if ($functionlike_storage) {
$functionlike_storage->has_yield = true;
}
} elseif ($node instanceof PhpParser\Node\Expr\Cast\Object_) {
$codebase->scanner->queueClassLikeForScanning('stdClass', false, false);
$file_storage->referenced_classlikes['stdclass'] = 'stdClass';
} elseif (($node instanceof PhpParser\Node\Expr\New_
|| $node instanceof PhpParser\Node\Expr\Instanceof_
|| $node instanceof PhpParser\Node\Expr\StaticPropertyFetch
|| $node instanceof PhpParser\Node\Expr\ClassConstFetch
|| $node instanceof PhpParser\Node\Expr\StaticCall)
&& $node->class instanceof PhpParser\Node\Name
) {
$fq_classlike_name = ClassLikeAnalyzer::getFQCLNFromNameObject($node->class, $aliases);
if (!in_array(strtolower($fq_classlike_name), ['self', 'static', 'parent'], true)) {
$codebase->scanner->queueClassLikeForScanning(
$fq_classlike_name,
false,
!($node instanceof PhpParser\Node\Expr\ClassConstFetch)
|| !($node->name instanceof PhpParser\Node\Identifier)
|| strtolower($node->name->name) !== 'class'
);
$file_storage->referenced_classlikes[strtolower($fq_classlike_name)] = $fq_classlike_name;
}
} elseif ($node instanceof PhpParser\Node\Expr\FuncCall && $node->name instanceof PhpParser\Node\Name) {
$function_id = implode('\\', $node->name->parts);
if (InternalCallMapHandler::inCallMap($function_id)) {
self::registerClassMapFunctionCall(
$codebase,
$file_storage,
$file_scanner,
$aliases,
$function_id,
$node,
$functionlike_storage,
$skip_if_descendants
);
}
}
}
private static function registerClassMapFunctionCall(
Codebase $codebase,
FileStorage $file_storage,
FileScanner $file_scanner,
Aliases $aliases,
string $function_id,
PhpParser\Node\Expr\FuncCall $node,
?FunctionLikeStorage $functionlike_storage,
?int $skip_if_descendants
): void {
$callables = InternalCallMapHandler::getCallablesFromCallMap($function_id);
if ($callables) {
foreach ($callables as $callable) {
assert($callable->params !== null);
foreach ($callable->params as $function_param) {
if ($function_param->type) {
$function_param->type->queueClassLikesForScanning(
$codebase,
$file_storage
);
}
}
if ($callable->return_type && !$callable->return_type->hasMixed()) {
$callable->return_type->queueClassLikesForScanning($codebase, $file_storage);
}
}
}
if ($node->isFirstClassCallable()) {
return;
}
if ($function_id === 'define') {
$first_arg_value = isset($node->getArgs()[0]) ? $node->getArgs()[0]->value : null;
$second_arg_value = isset($node->getArgs()[1]) ? $node->getArgs()[1]->value : null;
if ($first_arg_value && $second_arg_value) {
$type_provider = new NodeDataProvider();
$const_name = ConstFetchAnalyzer::getConstName(
$first_arg_value,
$type_provider,
$codebase,
$aliases
);
if ($const_name !== null) {
$const_type = SimpleTypeInferer::infer(
$codebase,
$type_provider,
$second_arg_value,
$aliases
) ?? Type::getMixed();
$config = Config::getInstance();
if ($functionlike_storage && !$config->hoist_constants) {
$functionlike_storage->defined_constants[$const_name] = $const_type;
} else {
$file_storage->constants[$const_name] = $const_type;
$file_storage->declaring_constants[$const_name] = $file_storage->file_path;
}
if (($codebase->register_stub_files || $codebase->register_autoload_files)
&& (!defined($const_name) || $const_type->isMixed())
) {
$codebase->addGlobalConstantType($const_name, $const_type);
}
}
}
}
$mapping_function_ids = [];
if (($function_id === 'array_map' && isset($node->getArgs()[0]))
|| ($function_id === 'array_filter' && isset($node->getArgs()[1]))
) {
$node_arg_value = $function_id === 'array_map' ? $node->getArgs()[0]->value : $node->getArgs()[1]->value;
if ($node_arg_value instanceof PhpParser\Node\Scalar\String_
|| $node_arg_value instanceof PhpParser\Node\Expr\Array_
|| $node_arg_value instanceof PhpParser\Node\Expr\BinaryOp\Concat
) {
$mapping_function_ids = CallAnalyzer::getFunctionIdsFromCallableArg(
$file_scanner,
$node_arg_value
);
}
foreach ($mapping_function_ids as $potential_method_id) {
if (strpos($potential_method_id, '::') === false) {
continue;
}
[$callable_fqcln] = explode('::', $potential_method_id);
if (!in_array(strtolower($callable_fqcln), ['self', 'parent', 'static'], true)) {
$codebase->scanner->queueClassLikeForScanning(
$callable_fqcln
);
}
}
}
if ($function_id === 'func_get_arg'
|| $function_id === 'func_get_args'
|| $function_id === 'func_num_args'
) {
if ($functionlike_storage) {
$functionlike_storage->variadic = true;
}
}
if ($function_id === 'is_a' || $function_id === 'is_subclass_of') {
$second_arg = $node->getArgs()[1]->value ?? null;
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
$codebase->scanner->queueClassLikeForScanning(
$second_arg->value
);
}
}
if ($function_id === 'class_alias' && !$skip_if_descendants) {
$first_arg = $node->getArgs()[0]->value ?? null;
$second_arg = $node->getArgs()[1]->value ?? null;
if ($first_arg instanceof PhpParser\Node\Scalar\String_) {
$first_arg_value = $first_arg->value;
} elseif ($first_arg instanceof PhpParser\Node\Expr\ClassConstFetch
&& $first_arg->class instanceof PhpParser\Node\Name
&& $first_arg->name instanceof PhpParser\Node\Identifier
&& strtolower($first_arg->name->name) === 'class'
) {
/** @var string */
$first_arg_value = $first_arg->class->getAttribute('resolvedName');
} else {
$first_arg_value = null;
}
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
$second_arg_value = $second_arg->value;
} elseif ($second_arg instanceof PhpParser\Node\Expr\ClassConstFetch
&& $second_arg->class instanceof PhpParser\Node\Name
&& $second_arg->name instanceof PhpParser\Node\Identifier
&& strtolower($second_arg->name->name) === 'class'
) {
/** @var string */
$second_arg_value = $second_arg->class->getAttribute('resolvedName');
} else {
$second_arg_value = null;
}
if ($first_arg_value !== null && $second_arg_value !== null) {
if ($first_arg_value[0] === '\\') {
$first_arg_value = substr($first_arg_value, 1);
}
if ($second_arg_value[0] === '\\') {
$second_arg_value = substr($second_arg_value, 1);
}
$second_arg_value = strtolower($second_arg_value);
$codebase->classlikes->addClassAlias(
$first_arg_value,
$second_arg_value
);
$file_storage->classlike_aliases[$second_arg_value] = $first_arg_value;
}
}
}
public static function visitInclude(
Codebase $codebase,
FileStorage $file_storage,
PhpParser\Node\Expr\Include_ $stmt,
bool $scan_deep
): void {
$config = Config::getInstance();
if (!$config->allow_includes) {
throw new FileIncludeException(
'File includes are not allowed per your Psalm config - check the allowFileIncludes flag.'
);
}
if ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
$path_to_file = $stmt->expr->value;
// attempts to resolve using get_include_path dirs
$include_path = IncludeAnalyzer::resolveIncludePath($path_to_file, dirname($file_storage->file_path));
$path_to_file = $include_path ?: $path_to_file;
if (DIRECTORY_SEPARATOR === '/') {
$is_path_relative = $path_to_file[0] !== DIRECTORY_SEPARATOR;
} else {
$is_path_relative = !preg_match('~^[A-Z]:\\\\~i', $path_to_file);
}
if ($is_path_relative) {
$path_to_file = $config->base_dir . DIRECTORY_SEPARATOR . $path_to_file;
}
} else {
$path_to_file = IncludeAnalyzer::getPathTo(
$stmt->expr,
null,
null,
$file_storage->file_path,
$config
);
}
if ($path_to_file) {
$path_to_file = IncludeAnalyzer::normalizeFilePath($path_to_file);
if ($file_storage->file_path === $path_to_file) {
return;
}
if ($codebase->fileExists($path_to_file)) {
if ($scan_deep) {
$codebase->scanner->addFileToDeepScan($path_to_file);
} else {
$codebase->scanner->addFileToShallowScan($path_to_file);
}
$file_storage->required_file_paths[strtolower($path_to_file)] = $path_to_file;
return;
}
}
}
}