-
Notifications
You must be signed in to change notification settings - Fork 651
/
UnionTypeComparator.php
499 lines (419 loc) · 16.7 KB
/
UnionTypeComparator.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
<?php
namespace Psalm\Internal\Type\Comparator;
use Psalm\Codebase;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TClassConstant;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TPositiveInt;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTypeAlias;
use Psalm\Type\Union;
use function array_merge;
use function array_pop;
use function array_push;
use function array_reverse;
/**
* @internal
*/
class UnionTypeComparator
{
/**
* Does the input param type match the given param type
*/
public static function isContainedBy(
Codebase $codebase,
Union $input_type,
Union $container_type,
bool $ignore_null = false,
bool $ignore_false = false,
?TypeComparisonResult $union_comparison_result = null,
bool $allow_interface_equality = false,
bool $allow_float_int_equality = true
): bool {
if ($container_type->isMixed()) {
return true;
}
if ($input_type->isNever()) {
return true;
}
if ($union_comparison_result) {
$union_comparison_result->scalar_type_match_found = true;
}
if ($input_type->possibly_undefined
&& !$input_type->possibly_undefined_from_try
&& !$container_type->possibly_undefined
) {
return false;
}
if ($container_type->hasMixed() && !$container_type->isEmptyMixed()) {
return true;
}
$container_has_template = $container_type->hasTemplateOrStatic();
$input_atomic_types = array_reverse(self::getTypeParts($codebase, $input_type));
while ($input_type_part = array_pop($input_atomic_types)) {
if ($input_type_part instanceof TNull && $ignore_null) {
continue;
}
if ($input_type_part instanceof TFalse && $ignore_false) {
continue;
}
if ($input_type_part instanceof TTemplateParam
&& !$container_has_template
&& !$input_type_part->extra_types
) {
$input_atomic_types = array_merge($input_type_part->as->getAtomicTypes(), $input_atomic_types);
continue;
}
$type_match_found = false;
$scalar_type_match_found = false;
$all_to_string_cast = true;
$all_type_coerced = null;
$all_type_coerced_from_mixed = null;
$all_type_coerced_from_as_mixed = null;
$some_type_coerced = false;
$some_type_coerced_from_mixed = false;
if ($input_type_part instanceof TArrayKey
&& ($container_type->hasInt() && $container_type->hasString())
) {
continue;
}
if ($input_type_part instanceof TArrayKey && $container_type->hasTemplate()) {
foreach ($container_type->getTemplateTypes() as $template_type) {
if ($template_type->as->isArrayKey()) {
continue 2;
}
}
}
if ($input_type_part instanceof TIntRange && $container_type->hasInt()) {
if (IntegerRangeComparator::isContainedByUnion(
$input_type_part,
$container_type
)) {
continue;
}
}
foreach (self::getTypeParts($codebase, $container_type) as $container_type_part) {
if ($ignore_null
&& $container_type_part instanceof TNull
&& !$input_type_part instanceof TNull
) {
continue;
}
if ($ignore_false
&& $container_type_part instanceof TFalse
&& !$input_type_part instanceof TFalse
) {
continue;
}
if ($union_comparison_result) {
$atomic_comparison_result = new TypeComparisonResult();
} else {
$atomic_comparison_result = null;
}
$is_atomic_contained_by = AtomicTypeComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
$allow_interface_equality,
$allow_float_int_equality,
$atomic_comparison_result
);
if ($input_type_part instanceof TMixed
&& $input_type->from_template_default
&& $input_type->from_docblock
&& $atomic_comparison_result
&& $atomic_comparison_result->type_coerced_from_mixed
) {
$atomic_comparison_result->type_coerced_from_as_mixed = true;
}
if ($atomic_comparison_result) {
if ($atomic_comparison_result->scalar_type_match_found !== null) {
$scalar_type_match_found = $atomic_comparison_result->scalar_type_match_found;
}
if ($union_comparison_result
&& $atomic_comparison_result->type_coerced_from_scalar !== null
) {
$union_comparison_result->type_coerced_from_scalar
= $atomic_comparison_result->type_coerced_from_scalar;
}
if ($is_atomic_contained_by
&& $union_comparison_result
&& $atomic_comparison_result->replacement_atomic_type
) {
if (!$union_comparison_result->replacement_union_type) {
$union_comparison_result->replacement_union_type = clone $input_type;
}
$union_comparison_result->replacement_union_type->removeType($input_type->getKey());
$union_comparison_result->replacement_union_type->addType(
$atomic_comparison_result->replacement_atomic_type
);
}
}
if ($input_type_part instanceof TNumeric
&& $container_type->hasString()
&& $container_type->hasInt()
&& $container_type->hasFloat()
) {
$scalar_type_match_found = false;
$is_atomic_contained_by = true;
}
if ($atomic_comparison_result) {
if ($atomic_comparison_result->type_coerced) {
$some_type_coerced = true;
}
if ($atomic_comparison_result->type_coerced_from_mixed) {
$some_type_coerced_from_mixed = true;
}
if ($atomic_comparison_result->type_coerced !== true || $all_type_coerced === false) {
$all_type_coerced = false;
} else {
$all_type_coerced = true;
}
if ($atomic_comparison_result->type_coerced_from_mixed !== true
|| $all_type_coerced_from_mixed === false
) {
$all_type_coerced_from_mixed = false;
} else {
$all_type_coerced_from_mixed = true;
}
if ($atomic_comparison_result->type_coerced_from_as_mixed !== true
|| $all_type_coerced_from_as_mixed === false
) {
$all_type_coerced_from_as_mixed = false;
} else {
$all_type_coerced_from_as_mixed = true;
}
}
if ($is_atomic_contained_by) {
$type_match_found = true;
if ($atomic_comparison_result) {
if ($atomic_comparison_result->to_string_cast !== true) {
$all_to_string_cast = false;
}
}
$all_type_coerced_from_mixed = false;
$all_type_coerced_from_as_mixed = false;
$all_type_coerced = false;
}
}
if ($union_comparison_result) {
// only set this flag if we're definite that the only
// reason the type match has been found is because there
// was a __toString cast
if ($all_to_string_cast && $type_match_found) {
$union_comparison_result->to_string_cast = true;
}
if ($all_type_coerced) {
$union_comparison_result->type_coerced = true;
}
if ($all_type_coerced_from_mixed) {
$union_comparison_result->type_coerced_from_mixed = true;
if (($input_type->from_template_default && $input_type->from_docblock)
|| $all_type_coerced_from_as_mixed
) {
$union_comparison_result->type_coerced_from_as_mixed = true;
}
}
}
if (!$type_match_found) {
if ($union_comparison_result) {
if ($some_type_coerced) {
$union_comparison_result->type_coerced = true;
}
if ($some_type_coerced_from_mixed) {
$union_comparison_result->type_coerced_from_mixed = true;
if (($input_type->from_template_default && $input_type->from_docblock)
|| $all_type_coerced_from_as_mixed
) {
$union_comparison_result->type_coerced_from_as_mixed = true;
}
}
if (!$scalar_type_match_found) {
$union_comparison_result->scalar_type_match_found = false;
}
}
return false;
}
}
return true;
}
/**
* Used for comparing signature typehints, uses PHP's light contravariance rules
*
*
*/
public static function isContainedByInPhp(
?Union $input_type,
Union $container_type
): bool {
if ($container_type->isMixed()) {
return !!$input_type;
}
if (!$input_type) {
return false;
}
if ($input_type->isNever()) {
return true;
}
if ($input_type->getId() === $container_type->getId()) {
return true;
}
if ($input_type->isNullable() && !$container_type->isNullable()) {
return false;
}
$input_type_not_null = clone $input_type;
$input_type_not_null->removeType('null');
$container_type_not_null = clone $container_type;
$container_type_not_null->removeType('null');
if ($input_type_not_null->getId() === $container_type_not_null->getId()) {
return true;
}
if ($input_type_not_null->hasArray() && $container_type_not_null->hasType('iterable')) {
return true;
}
return false;
}
/**
* Does the input param type match the given param type
*/
public static function canBeContainedBy(
Codebase $codebase,
Union $input_type,
Union $container_type,
bool $ignore_null = false,
bool $ignore_false = false,
array &$matching_input_keys = []
): bool {
if ($container_type->hasMixed()) {
return true;
}
if ($input_type->isNever()) {
return true;
}
if ($input_type->possibly_undefined && !$container_type->possibly_undefined) {
return false;
}
foreach (self::getTypeParts($codebase, $container_type) as $container_type_part) {
if ($container_type_part instanceof TNull && $ignore_null) {
continue;
}
if ($container_type_part instanceof TFalse && $ignore_false) {
continue;
}
foreach (self::getTypeParts($codebase, $input_type) as $input_type_part) {
$atomic_comparison_result = new TypeComparisonResult();
$is_atomic_contained_by = AtomicTypeComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
false,
false,
$atomic_comparison_result
);
if (($is_atomic_contained_by && !$atomic_comparison_result->to_string_cast)
|| $atomic_comparison_result->type_coerced_from_mixed
) {
$matching_input_keys[$input_type_part->getKey()] = true;
}
}
}
return (bool)$matching_input_keys;
}
/**
* Can any part of the $type1 be equal to any part of $type2
*
*/
public static function canExpressionTypesBeIdentical(
Codebase $codebase,
Union $type1,
Union $type2,
bool $allow_interface_equality = true
): bool {
if ($type1->hasMixed() || $type2->hasMixed()) {
return true;
}
if ($type1->isNullable() && $type2->isNullable()) {
return true;
}
foreach (self::getTypeParts($codebase, $type1) as $type1_part) {
foreach (self::getTypeParts($codebase, $type2) as $type2_part) {
//special cases for TIntRange because it can contain a part of the other type.
//For exemple int<0,1> and positive-int can be identical but none contain the other
if (($type1_part instanceof TIntRange && $type2_part instanceof TPositiveInt)) {
$intersection_range = TIntRange::intersectIntRanges(
TIntRange::convertToIntRange($type2_part),
$type1_part
);
return $intersection_range !== null;
}
if ($type2_part instanceof TIntRange && $type1_part instanceof TPositiveInt) {
$intersection_range = TIntRange::intersectIntRanges(
TIntRange::convertToIntRange($type1_part),
$type2_part
);
return $intersection_range !== null;
}
if ($type1_part instanceof TIntRange && $type2_part instanceof TIntRange) {
$intersection_range = TIntRange::intersectIntRanges(
$type1_part,
$type2_part
);
return $intersection_range !== null;
}
$either_contains = AtomicTypeComparator::canBeIdentical(
$codebase,
$type1_part,
$type2_part,
$allow_interface_equality
);
if ($either_contains) {
return true;
}
}
}
return false;
}
/**
* @return list<Atomic>
*/
private static function getTypeParts(
Codebase $codebase,
Union $union_type
): array {
$atomic_types = [];
foreach ($union_type->getAtomicTypes() as $atomic_type) {
if (!$atomic_type instanceof TTypeAlias && !$atomic_type instanceof TClassConstant) {
$atomic_types[] = $atomic_type;
continue;
}
if ($atomic_type instanceof TTypeAlias) {
$fq_classlike_name = $atomic_type->declaring_fq_classlike_name;
} else {
$fq_classlike_name = $atomic_type->fq_classlike_name;
}
$expanded = TypeExpander::expandAtomic(
$codebase,
$atomic_type,
$fq_classlike_name,
$fq_classlike_name,
null,
true,
true
);
if ($expanded instanceof Atomic) {
if (!$expanded instanceof TTypeAlias && !$expanded instanceof TClassConstant) {
$atomic_types[] = $expanded;
}
continue;
}
array_push($atomic_types, ...$expanded);
}
return $atomic_types;
}
}