forked from vimeo/psalm
/
TemplateInferredTypeReplacer.php
547 lines (472 loc) · 19.6 KB
/
TemplateInferredTypeReplacer.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
namespace Psalm\Internal\Type;
use InvalidArgumentException;
use Psalm\Codebase;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TConditional;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIterable;
use Psalm\Type\Atomic\TKeyOfArray;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Atomic\TPropertiesOf;
use Psalm\Type\Atomic\TTemplateIndexedAccess;
use Psalm\Type\Atomic\TTemplateKeyOf;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTemplateParamClass;
use Psalm\Type\Atomic\TTemplatePropertiesOf;
use Psalm\Type\Atomic\TTemplateValueOf;
use Psalm\Type\Atomic\TValueOf;
use Psalm\Type\Union;
use UnexpectedValueException;
use function array_merge;
use function array_shift;
use function array_values;
use function strpos;
/**
* @internal
*/
class TemplateInferredTypeReplacer
{
/**
* This replaces template types in unions with the inferred types they should be
*/
public static function replace(
Union $union,
TemplateResult $template_result,
?Codebase $codebase
): void {
$keys_to_unset = [];
$new_types = [];
$is_mixed = false;
$inferred_lower_bounds = $template_result->lower_bounds ?: [];
foreach ($union->getAtomicTypes() as $key => $atomic_type) {
$atomic_type->replaceTemplateTypesWithArgTypes($template_result, $codebase);
if ($atomic_type instanceof TTemplateParam) {
$template_type = self::replaceTemplateParam(
$codebase,
$atomic_type,
$inferred_lower_bounds,
$key
);
if ($template_type) {
$keys_to_unset[] = $key;
foreach ($template_type->getAtomicTypes() as $template_type_part) {
if ($template_type_part instanceof TMixed) {
$is_mixed = true;
}
$new_types[] = $template_type_part;
}
}
} elseif ($atomic_type instanceof TTemplateParamClass) {
$template_type = isset($inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class])
? clone TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class],
$codebase
)
: null;
$class_template_type = null;
if ($template_type) {
foreach ($template_type->getAtomicTypes() as $template_type_part) {
if ($template_type_part instanceof TMixed
|| $template_type_part instanceof TObject
) {
$class_template_type = new TClassString();
} elseif ($template_type_part instanceof TNamedObject) {
$class_template_type = new TClassString(
$template_type_part->value,
$template_type_part
);
} elseif ($template_type_part instanceof TTemplateParam) {
$first_atomic_type = $template_type_part->as->getSingleAtomic();
$class_template_type = new TTemplateParamClass(
$template_type_part->param_name,
$template_type_part->as->getId(),
$first_atomic_type instanceof TNamedObject ? $first_atomic_type : null,
$template_type_part->defining_class
);
}
}
}
if ($class_template_type) {
$keys_to_unset[] = $key;
$new_types[] = $class_template_type;
}
} elseif ($atomic_type instanceof TTemplateIndexedAccess) {
$keys_to_unset[] = $key;
$template_type = null;
if (isset($inferred_lower_bounds[$atomic_type->array_param_name][$atomic_type->defining_class])
&& !empty($inferred_lower_bounds[$atomic_type->offset_param_name])
) {
$array_template_type
= TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$atomic_type->array_param_name][$atomic_type->defining_class],
$codebase
);
$offset_template_type
= TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
array_values($inferred_lower_bounds[$atomic_type->offset_param_name])[0],
$codebase
);
if ($array_template_type->isSingle()
&& $offset_template_type->isSingle()
&& !$array_template_type->isMixed()
&& !$offset_template_type->isMixed()
) {
$array_template_type = $array_template_type->getSingleAtomic();
$offset_template_type = $offset_template_type->getSingleAtomic();
if ($array_template_type instanceof TKeyedArray
&& ($offset_template_type instanceof TLiteralString
|| $offset_template_type instanceof TLiteralInt)
&& isset($array_template_type->properties[$offset_template_type->value])
) {
$template_type = clone $array_template_type->properties[$offset_template_type->value];
}
}
}
if ($template_type) {
foreach ($template_type->getAtomicTypes() as $template_type_part) {
if ($template_type_part instanceof TMixed) {
$is_mixed = true;
}
$new_types[] = $template_type_part;
}
} else {
$new_types[] = new TMixed();
}
} elseif ($atomic_type instanceof TTemplateKeyOf
|| $atomic_type instanceof TTemplateValueOf
) {
$new_type = self::replaceTemplateKeyOfValueOf(
$codebase,
$atomic_type,
$inferred_lower_bounds
);
if ($new_type) {
$keys_to_unset[] = $key;
$new_types[] = $new_type;
}
} elseif ($atomic_type instanceof TTemplatePropertiesOf) {
$new_type = self::replaceTemplatePropertiesOf(
$codebase,
$atomic_type,
$inferred_lower_bounds
);
if ($new_type) {
$keys_to_unset[] = $key;
$new_types[] = $new_type;
}
} elseif ($atomic_type instanceof TConditional
&& $codebase
) {
$class_template_type = self::replaceConditional(
$template_result,
$codebase,
$atomic_type,
$inferred_lower_bounds
);
$keys_to_unset[] = $key;
foreach ($class_template_type->getAtomicTypes() as $class_template_atomic_type) {
$new_types[] = $class_template_atomic_type;
}
}
}
$union->bustCache();
if ($is_mixed) {
if (!$new_types) {
throw new UnexpectedValueException('This array should be full');
}
$union->replaceTypes(
TypeCombiner::combine(
$new_types,
$codebase
)->getAtomicTypes()
);
return;
}
foreach ($keys_to_unset as $key) {
$union->removeType($key);
}
$atomic_types = array_values(array_merge($union->getAtomicTypes(), $new_types));
$union->replaceTypes(
TypeCombiner::combine(
$atomic_types,
$codebase
)->getAtomicTypes()
);
}
/**
* @param array<string, array<string, non-empty-list<TemplateBound>>> $inferred_lower_bounds
*/
private static function replaceTemplateParam(
?Codebase $codebase,
TTemplateParam $atomic_type,
array $inferred_lower_bounds,
string $key
): ?Union {
$template_type = null;
$traversed_type = TemplateStandinTypeReplacer::getRootTemplateType(
$inferred_lower_bounds,
$atomic_type->param_name,
$atomic_type->defining_class,
[],
$codebase
);
if ($traversed_type) {
$template_type = $traversed_type;
if (!$atomic_type->as->isMixed() && $template_type->isMixed()) {
$template_type = clone $atomic_type->as;
} else {
$template_type = clone $template_type;
}
if ($atomic_type->extra_types) {
foreach ($template_type->getAtomicTypes() as $template_type_key => $atomic_template_type) {
if ($atomic_template_type instanceof TNamedObject
|| $atomic_template_type instanceof TTemplateParam
|| $atomic_template_type instanceof TIterable
|| $atomic_template_type instanceof TObjectWithProperties
) {
$atomic_template_type->extra_types = array_merge(
$atomic_type->extra_types,
$atomic_template_type->extra_types ?: []
);
} elseif ($atomic_template_type instanceof TObject) {
$first_atomic_type = array_shift($atomic_type->extra_types);
if ($atomic_type->extra_types) {
$first_atomic_type->extra_types = $atomic_type->extra_types;
}
$template_type->removeType($template_type_key);
$template_type->addType($first_atomic_type);
}
}
}
} elseif ($codebase) {
foreach ($inferred_lower_bounds as $template_type_map) {
foreach ($template_type_map as $template_class => $_) {
if (strpos($template_class, 'fn-') === 0) {
continue;
}
try {
$classlike_storage = $codebase->classlike_storage_provider->get($template_class);
if ($classlike_storage->template_extended_params) {
$defining_class = $atomic_type->defining_class;
if (isset($classlike_storage->template_extended_params[$defining_class])) {
$param_map = $classlike_storage->template_extended_params[$defining_class];
if (isset($param_map[$key])) {
$template_name = (string) $param_map[$key];
if (isset($inferred_lower_bounds[$template_name][$template_class])) {
$template_type
= clone TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$template_name][$template_class],
$codebase
);
}
}
}
}
} catch (InvalidArgumentException $e) {
}
}
}
}
return $template_type;
}
/**
* @param TTemplateKeyOf|TTemplateValueOf $atomic_type
* @param array<string, array<string, non-empty-list<TemplateBound>>> $inferred_lower_bounds
*/
private static function replaceTemplateKeyOfValueOf(
?Codebase $codebase,
Atomic $atomic_type,
array $inferred_lower_bounds
): ?Atomic {
if (!isset($inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class])) {
return null;
}
$template_type = clone TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class],
$codebase
);
if ($atomic_type instanceof TTemplateKeyOf
&& TKeyOfArray::isViableTemplateType($template_type)
) {
return new TKeyOfArray(clone $template_type);
}
if ($atomic_type instanceof TTemplateValueOf
&& TValueOf::isViableTemplateType($template_type)
) {
return new TValueOf(clone $template_type);
}
return null;
}
/**
* @param array<string, array<string, non-empty-list<TemplateBound>>> $inferred_lower_bounds
*/
private static function replaceTemplatePropertiesOf(
?Codebase $codebase,
TTemplatePropertiesOf $atomic_type,
array $inferred_lower_bounds
): ?Atomic {
if (!isset($inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class])) {
return null;
}
$template_type = clone TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class],
$codebase
);
$classlike_type = $template_type->getSingleAtomic();
if (!$classlike_type instanceof TNamedObject) {
return null;
}
return new TPropertiesOf(
(string) $classlike_type,
clone $classlike_type,
$atomic_type->visibility_filter
);
}
/**
* @param array<string, array<string, non-empty-list<TemplateBound>>> $inferred_lower_bounds
*/
private static function replaceConditional(
TemplateResult $template_result,
Codebase $codebase,
TConditional $atomic_type,
array $inferred_lower_bounds
): Union {
$template_type = isset($inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class])
? clone TemplateStandinTypeReplacer::getMostSpecificTypeFromBounds(
$inferred_lower_bounds[$atomic_type->param_name][$atomic_type->defining_class],
$codebase
)
: null;
$if_template_type = null;
$else_template_type = null;
$atomic_type = clone $atomic_type;
if ($template_type) {
self::replace(
$atomic_type->as_type,
$template_result,
$codebase
);
if ($atomic_type->as_type->isNullable() && $template_type->isVoid()) {
$template_type = Type::getNull();
}
$matching_if_types = [];
$matching_else_types = [];
foreach ($template_type->getAtomicTypes() as $candidate_atomic_type) {
if (UnionTypeComparator::isContainedBy(
$codebase,
new Union([$candidate_atomic_type]),
$atomic_type->conditional_type,
false,
false,
null,
false,
false
)
&& (!$candidate_atomic_type instanceof TInt
|| $atomic_type->conditional_type->getId() !== 'float')
) {
$matching_if_types[] = $candidate_atomic_type;
} elseif (!UnionTypeComparator::isContainedBy(
$codebase,
$atomic_type->conditional_type,
new Union([$candidate_atomic_type]),
false,
false,
null,
false,
false
)) {
$matching_else_types[] = $candidate_atomic_type;
}
}
$if_candidate_type = $matching_if_types ? new Union($matching_if_types) : null;
$else_candidate_type = $matching_else_types ? new Union($matching_else_types) : null;
if ($if_candidate_type
&& UnionTypeComparator::isContainedBy(
$codebase,
$if_candidate_type,
$atomic_type->conditional_type,
false,
false,
null,
false,
false
)
) {
$if_template_type = clone $atomic_type->if_type;
$refined_template_result = clone $template_result;
$refined_template_result->lower_bounds[$atomic_type->param_name][$atomic_type->defining_class]
= [
new TemplateBound(
$if_candidate_type
)
];
self::replace(
$if_template_type,
$refined_template_result,
$codebase
);
}
if ($else_candidate_type
&& UnionTypeComparator::isContainedBy(
$codebase,
$else_candidate_type,
$atomic_type->as_type,
false,
false,
null,
false,
false
)
) {
$else_template_type = clone $atomic_type->else_type;
$refined_template_result = clone $template_result;
$refined_template_result->lower_bounds[$atomic_type->param_name][$atomic_type->defining_class]
= [
new TemplateBound(
$else_candidate_type
)
];
self::replace(
$else_template_type,
$refined_template_result,
$codebase
);
}
}
if (!$if_template_type && !$else_template_type) {
self::replace(
$atomic_type->if_type,
$template_result,
$codebase
);
self::replace(
$atomic_type->else_type,
$template_result,
$codebase
);
$class_template_type = Type::combineUnionTypes(
$atomic_type->if_type,
$atomic_type->else_type,
$codebase
);
} else {
$class_template_type = Type::combineUnionTypes(
$if_template_type,
$else_template_type,
$codebase
);
}
return $class_template_type;
}
}