-
Notifications
You must be signed in to change notification settings - Fork 667
/
StubsGenerator.php
419 lines (344 loc) · 13.2 KB
/
StubsGenerator.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php declare(strict_types=1);
namespace Psalm\Internal\Stubs\Generator;
use Psalm\Codebase;
use Psalm\Internal\Provider\ClassLikeStorageProvider;
use Psalm\Internal\Provider\FileStorageProvider;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TIterable;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TLiteralClassString;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Atomic\Scalar;
use PhpParser;
use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Node\Expr\VirtualArray;
use Psalm\Node\VirtualArrayItem;
use Psalm\Node\Expr\VirtualClassConstFetch;
use Psalm\Node\Expr\VirtualConstFetch;
use Psalm\Node\Expr\VirtualVariable;
use Psalm\Node\Name\VirtualFullyQualified;
use Psalm\Node\Scalar\VirtualFloat;
use Psalm\Node\Scalar\VirtualInt;
use Psalm\Node\Scalar\VirtualString;
use Psalm\Node\Stmt\VirtualFunction;
use Psalm\Node\Stmt\VirtualNamespace;
use Psalm\Node\VirtualConst;
use Psalm\Node\Stmt\VirtualConst as StmtVirtualConst_;
use Psalm\Node\VirtualIdentifier;
use Psalm\Node\VirtualName;
use Psalm\Node\VirtualNullableType;
use Psalm\Node\VirtualParam;
use Psalm\Type;
use Psalm\Type\Union;
use UnexpectedValueException;
use function dirname;
use function is_int;
use function rtrim;
use function strpos;
/**
* @internal
*/
final class StubsGenerator
{
public static function getAll(
Codebase $codebase,
ClassLikeStorageProvider $class_provider,
FileStorageProvider $file_provider
): string {
$namespaced_nodes = [];
$psalm_base = dirname(__DIR__, 5);
foreach ($class_provider->getAll() as $storage) {
if (str_starts_with($storage->name, 'Psalm\\')) {
continue;
}
if ($storage->location
&& str_starts_with($storage->location->file_path, $psalm_base)
) {
continue;
}
if ($storage->stubbed) {
continue;
}
$name_parts = explode('\\', $storage->name);
$classlike_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
if (!isset($namespaced_nodes[$namespace_name])) {
$namespaced_nodes[$namespace_name] = [];
}
$namespaced_nodes[$namespace_name][$classlike_name] = ClassLikeStubGenerator::getClassLikeNode(
$codebase,
$storage,
$classlike_name
);
}
$all_function_names = [];
foreach ($codebase->functions->getAllStubbedFunctions() as $function_storage) {
if ($function_storage->location
&& str_starts_with($function_storage->location->file_path, $psalm_base)
) {
continue;
}
if (!$function_storage->cased_name) {
throw new UnexpectedValueException('very bad');
}
$fq_name = $function_storage->cased_name;
$all_function_names[$fq_name] = true;
$name_parts = explode('\\', $fq_name);
$function_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = self::getFunctionNode(
$function_storage,
$function_name,
$namespace_name
);
}
foreach ($codebase->getAllStubbedConstants() as $fq_name => $type) {
if ($type->isMixed()) {
continue;
}
$name_parts = explode('\\', $fq_name);
$constant_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = new StmtVirtualConst_(
[
new VirtualConst(
$constant_name,
self::getExpressionFromType($type)
)
]
);
}
foreach ($file_provider->getAll() as $file_storage) {
if (str_starts_with($file_storage->file_path, $psalm_base)) {
continue;
}
foreach ($file_storage->functions as $function_storage) {
if (!$function_storage->cased_name) {
continue;
}
$fq_name = $function_storage->cased_name;
if (isset($all_function_names[$fq_name])) {
continue;
}
$all_function_names[$fq_name] = true;
$name_parts = explode('\\', $fq_name);
$function_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = self::getFunctionNode(
$function_storage,
$function_name,
$namespace_name
);
}
foreach ($file_storage->constants as $fq_name => $type) {
if ($type->isMixed()) {
continue;
}
$name_parts = explode('\\', $fq_name);
$constant_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = new StmtVirtualConst_(
[
new VirtualConst(
$constant_name,
self::getExpressionFromType($type)
)
]
);
}
}
ksort($namespaced_nodes);
$namespace_stmts = [];
foreach ($namespaced_nodes as $namespace_name => $stmts) {
ksort($stmts);
$namespace_stmts[] = new VirtualNamespace(
$namespace_name ? new VirtualName($namespace_name) : null,
array_values($stmts),
['kind' => PhpParser\Node\Stmt\Namespace_::KIND_BRACED]
);
}
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
return $prettyPrinter->prettyPrintFile($namespace_stmts);
}
private static function getFunctionNode(
FunctionLikeStorage $function_storage,
string $function_name,
string $namespace_name
) : PhpParser\Node\Stmt\Function_ {
$docblock = new ParsedDocblock('', []);
foreach ($function_storage->template_types ?: [] as $template_name => $map) {
$type = array_values($map)[0];
$docblock->tags['template'][] = $template_name . ' as ' . $type->toNamespacedString(
$namespace_name,
[],
null,
false
);
}
foreach ($function_storage->params as $param) {
if ($param->type && $param->type !== $param->signature_type) {
$docblock->tags['param'][] = $param->type->toNamespacedString(
$namespace_name,
[],
null,
false
) . ' $' . $param->name;
}
}
if ($function_storage->return_type
&& $function_storage->signature_return_type !== $function_storage->return_type
) {
$docblock->tags['return'][] = $function_storage->return_type->toNamespacedString(
$namespace_name,
[],
null,
false
);
}
foreach ($function_storage->throws ?: [] as $exception_name => $_) {
$docblock->tags['throws'][] = Type::getStringFromFQCLN(
$exception_name,
$namespace_name,
[],
null,
false
);
}
return new VirtualFunction(
$function_name,
[
'params' => self::getFunctionParamNodes($function_storage),
'returnType' => $function_storage->signature_return_type
? self::getParserTypeFromPsalmType($function_storage->signature_return_type)
: null,
'stmts' => [],
],
[
'comments' => $docblock->tags
? [
new PhpParser\Comment\Doc(
rtrim($docblock->render(' '))
)
]
: []
]
);
}
/**
* @return list<PhpParser\Node\Param>
*/
public static function getFunctionParamNodes(FunctionLikeStorage $method_storage): array
{
$param_nodes = [];
foreach ($method_storage->params as $param) {
$param_nodes[] = new VirtualParam(
new VirtualVariable($param->name),
$param->default_type instanceof Union
? self::getExpressionFromType($param->default_type)
: null,
$param->signature_type
? self::getParserTypeFromPsalmType($param->signature_type)
: null,
$param->by_ref,
$param->is_variadic
);
}
return $param_nodes;
}
/**
* @return PhpParser\Node\Identifier|PhpParser\Node\Name\FullyQualified|PhpParser\Node\NullableType|null
*/
public static function getParserTypeFromPsalmType(Union $type): ?PhpParser\NodeAbstract
{
$nullable = $type->isNullable();
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TNull) {
continue;
}
if ($atomic_type instanceof Scalar
|| $atomic_type instanceof TObject
|| $atomic_type instanceof TArray
|| $atomic_type instanceof TIterable
) {
$identifier_string = $atomic_type->toPhpString(null, [], null, 8_00_00);
if ($identifier_string === null) {
throw new UnexpectedValueException(
$atomic_type->getId() . ' could not be converted to an identifier'
);
}
$identifier = new VirtualIdentifier($identifier_string);
if ($nullable) {
return new VirtualNullableType($identifier);
}
return $identifier;
}
if ($atomic_type instanceof TNamedObject) {
$name_node = new VirtualFullyQualified($atomic_type->value);
if ($nullable) {
return new VirtualNullableType($name_node);
}
return $name_node;
}
}
return null;
}
public static function getExpressionFromType(Union $type) : PhpParser\Node\Expr
{
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TLiteralClassString) {
return new VirtualClassConstFetch(new VirtualName('\\' . $atomic_type->value), new VirtualIdentifier('class'));
}
if ($atomic_type instanceof TLiteralString) {
return new VirtualString($atomic_type->value);
}
if ($atomic_type instanceof TLiteralInt) {
return new VirtualInt($atomic_type->value);
}
if ($atomic_type instanceof TLiteralFloat) {
return new VirtualFloat($atomic_type->value);
}
if ($atomic_type instanceof TFalse) {
return new VirtualConstFetch(new VirtualName('false'));
}
if ($atomic_type instanceof TTrue) {
return new VirtualConstFetch(new VirtualName('true'));
}
if ($atomic_type instanceof TNull) {
return new VirtualConstFetch(new VirtualName('null'));
}
if ($atomic_type instanceof TArray) {
return new VirtualArray([]);
}
if ($atomic_type instanceof TKeyedArray) {
$new_items = [];
foreach ($atomic_type->properties as $property_name => $property_type) {
if ($atomic_type->is_list) {
$key_type = null;
} elseif (is_int($property_name)) {
$key_type = new VirtualInt($property_name);
} else {
$key_type = new VirtualString($property_name);
}
$new_items[] = new VirtualArrayItem(
self::getExpressionFromType($property_type),
$key_type
);
}
return new VirtualArray($new_items);
}
if ($atomic_type instanceof TEnumCase) {
return new VirtualClassConstFetch(new VirtualName('\\' . $atomic_type->value), new VirtualIdentifier($atomic_type->case_name));
}
}
return new VirtualString('Psalm could not infer this type');
}
}