/
ScalarTypeComparator.php
642 lines (544 loc) · 22.7 KB
/
ScalarTypeComparator.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php
namespace Psalm\Internal\Type\Comparator;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TCallableString;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TDependentGetClass;
use Psalm\Type\Atomic\TDependentGetDebugType;
use Psalm\Type\Atomic\TDependentGetType;
use Psalm\Type\Atomic\TDependentListKey;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TLiteralClassString;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TLowercaseString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyLowercaseString;
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyScalar;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonFalsyString;
use Psalm\Type\Atomic\TNonspecificLiteralInt;
use Psalm\Type\Atomic\TNonspecificLiteralString;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TPositiveInt;
use Psalm\Type\Atomic\TScalar;
use Psalm\Type\Atomic\TSingleLetter;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateKeyOf;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTemplateParamClass;
use Psalm\Type\Atomic\TTraitString;
use Psalm\Type\Atomic\TTrue;
use function get_class;
use function is_numeric;
use function strtolower;
/**
* @internal
*/
class ScalarTypeComparator
{
public static function isContainedBy(
Codebase $codebase,
Scalar $input_type_part,
Scalar $container_type_part,
bool $allow_interface_equality = false,
bool $allow_float_int_equality = true,
?TypeComparisonResult $atomic_comparison_result = null
): bool {
if (get_class($container_type_part) === TString::class
&& $input_type_part instanceof TString
) {
return true;
}
if (get_class($container_type_part) === TInt::class
&& $input_type_part instanceof TInt
) {
return true;
}
if (get_class($container_type_part) === TFloat::class
&& $input_type_part instanceof TFloat
) {
return true;
}
if ($container_type_part instanceof TNonEmptyString
&& get_class($input_type_part) === TString::class
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($container_type_part instanceof TNonspecificLiteralString
&& ($input_type_part instanceof TLiteralString || $input_type_part instanceof TNonspecificLiteralString)
) {
return true;
}
if ($container_type_part instanceof TNonspecificLiteralString) {
if ($input_type_part instanceof TString) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
}
return false;
}
if ($container_type_part instanceof TNonspecificLiteralInt
&& ($input_type_part instanceof TLiteralInt
|| $input_type_part instanceof TNonspecificLiteralInt)
) {
return true;
}
if ($container_type_part instanceof TNonspecificLiteralInt) {
if ($input_type_part instanceof TInt) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
}
return false;
}
if ($input_type_part instanceof TCallableString
&& (get_class($container_type_part) === TSingleLetter::class
|| get_class($container_type_part) === TNonEmptyString::class
|| get_class($container_type_part) === TNonFalsyString::class
|| get_class($container_type_part) === TLowercaseString::class)
) {
return true;
}
if (($container_type_part instanceof TLowercaseString
|| $container_type_part instanceof TNonEmptyLowercaseString)
&& $input_type_part instanceof TString
) {
if (($input_type_part instanceof TLowercaseString
&& $container_type_part instanceof TLowercaseString)
|| ($input_type_part instanceof TNonEmptyLowercaseString
&& $container_type_part instanceof TNonEmptyLowercaseString)
) {
return true;
}
if ($input_type_part instanceof TNonEmptyLowercaseString
&& $container_type_part instanceof TLowercaseString
) {
return true;
}
if ($input_type_part instanceof TLowercaseString
&& $container_type_part instanceof TNonEmptyLowercaseString
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($input_type_part instanceof TLiteralString) {
if (strtolower($input_type_part->value) === $input_type_part->value) {
return $input_type_part->value || $container_type_part instanceof TLowercaseString;
}
return false;
}
if ($input_type_part instanceof TClassString) {
return false;
}
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($container_type_part instanceof TDependentGetClass) {
$first_type = $container_type_part->as_type->getSingleAtomic();
$container_type_part = new TClassString(
'object',
$first_type instanceof TNamedObject ? $first_type : null
);
}
if ($input_type_part instanceof TDependentGetClass) {
$first_type = $input_type_part->as_type->getSingleAtomic();
if ($first_type instanceof TTemplateParam) {
$object_type = $first_type->as->getSingleAtomic();
$input_type_part = new TTemplateParamClass(
$first_type->param_name,
$first_type->as->getId(),
$object_type instanceof TNamedObject ? $object_type : null,
$first_type->defining_class
);
} else {
$input_type_part = new TClassString(
'object',
$first_type instanceof TNamedObject ? $first_type : null
);
}
}
if ($input_type_part instanceof TDependentGetType) {
$input_type_part = new TString();
if ($container_type_part instanceof TLiteralString) {
return isset(ClassLikeAnalyzer::GETTYPE_TYPES[$container_type_part->value]);
}
}
if ($container_type_part instanceof TDependentGetDebugType) {
return $input_type_part instanceof TString;
}
if ($input_type_part instanceof TDependentGetDebugType) {
$input_type_part = new TString();
}
if ($container_type_part instanceof TDependentGetType) {
$container_type_part = new TString();
if ($input_type_part instanceof TLiteralString) {
return isset(ClassLikeAnalyzer::GETTYPE_TYPES[$input_type_part->value]);
}
}
if ($input_type_part instanceof TFalse
&& $container_type_part instanceof TBool
&& !($container_type_part instanceof TTrue)
) {
return true;
}
if ($input_type_part instanceof TTrue
&& $container_type_part instanceof TBool
&& !($container_type_part instanceof TFalse)
) {
return true;
}
// from https://wiki.php.net/rfc/scalar_type_hints_v5:
//
// > int types can resolve a parameter type of float
if ($input_type_part instanceof TInt
&& $container_type_part instanceof TFloat
&& !$container_type_part instanceof TLiteralFloat
&& $allow_float_int_equality
) {
return true;
}
if ($container_type_part instanceof TArrayKey
&& $input_type_part instanceof TNumeric
) {
return true;
}
if ($container_type_part instanceof TArrayKey
&& ($input_type_part instanceof TInt
|| $input_type_part instanceof TString
|| $input_type_part instanceof TTemplateKeyOf)
) {
return true;
}
if ($input_type_part instanceof TTemplateKeyOf) {
foreach ($input_type_part->as->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TArray) {
/** @var Scalar $array_key_atomic */
foreach ($atomic_type->type_params[0]->getAtomicTypes() as $array_key_atomic) {
if (!self::isContainedBy(
$codebase,
$array_key_atomic,
$container_type_part,
$allow_interface_equality,
$allow_float_int_equality,
$atomic_comparison_result
)) {
return false;
}
}
}
}
return true;
}
if ($input_type_part instanceof TArrayKey &&
($container_type_part instanceof TInt || $container_type_part instanceof TString)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_mixed = true;
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}
return false;
}
if ($container_type_part instanceof TScalar && $input_type_part instanceof Scalar) {
return true;
}
if (get_class($container_type_part) === TDependentListKey::class
&& ($input_type_part instanceof TLiteralInt
|| $input_type_part instanceof TPositiveInt)
) {
return true;
}
if (get_class($container_type_part) === TFloat::class && $input_type_part instanceof TLiteralFloat) {
return true;
}
if ((get_class($container_type_part) === TNonEmptyString::class
|| get_class($container_type_part) === TNonEmptyNonspecificLiteralString::class)
&& $input_type_part instanceof TNonFalsyString
) {
return true;
}
if ($container_type_part instanceof TNonFalsyString
&& $input_type_part instanceof TNonFalsyString
) {
return true;
}
if ($container_type_part instanceof TNonFalsyString
&& ($input_type_part instanceof TNonEmptyString
|| $input_type_part instanceof TNonEmptyNonspecificLiteralString)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($container_type_part instanceof TNonEmptyString
&& $input_type_part instanceof TLiteralString
&& $input_type_part->value === ''
) {
return false;
}
if ($container_type_part instanceof TNonFalsyString
&& $input_type_part instanceof TLiteralString
&& $input_type_part->value === '0'
) {
return false;
}
if ((get_class($container_type_part) === TNonEmptyString::class
|| get_class($container_type_part) === TNonFalsyString::class
|| get_class($container_type_part) === TSingleLetter::class)
&& $input_type_part instanceof TLiteralString
) {
return true;
}
if (get_class($container_type_part) === TDependentListKey::class
&& $input_type_part instanceof TInt
) {
return true;
}
if ((get_class($input_type_part) === TInt::class && $container_type_part instanceof TLiteralInt)
|| (get_class($input_type_part) === TPositiveInt::class
&& $container_type_part instanceof TLiteralInt
&& $container_type_part->value > 0)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if ($input_type_part instanceof TIntRange && $container_type_part instanceof TIntRange) {
return IntegerRangeComparator::isContainedBy(
$input_type_part,
$container_type_part
);
}
if ($input_type_part instanceof TInt && $container_type_part instanceof TPositiveInt) {
if ($input_type_part instanceof TPositiveInt) {
return true;
}
if ($input_type_part instanceof TLiteralInt) {
return $input_type_part->value > 0;
}
if ($input_type_part instanceof TIntRange) {
return $input_type_part->isPositive();
}
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if ($input_type_part instanceof TInt && $container_type_part instanceof TIntRange) {
if ($input_type_part instanceof TPositiveInt) {
if ($container_type_part->min_bound > 1) {
//any positive int can't be pushed inside a range with a min > 1
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if ($container_type_part->max_bound !== null) {
//any positive int can't be pushed inside a range where the max bound isn't max without coercion
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
return true;
}
if ($input_type_part instanceof TLiteralInt) {
$min_bound = $container_type_part->min_bound;
$max_bound = $container_type_part->max_bound;
return
($min_bound === null || $min_bound <= $input_type_part->value) &&
($max_bound === null || $max_bound >= $input_type_part->value);
}
//any int can't be pushed inside a range without coercion (unless the range is from min to max)
if ($container_type_part->min_bound !== null || $container_type_part->max_bound !== null) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
}
return false;
}
if (get_class($input_type_part) === TFloat::class && $container_type_part instanceof TLiteralFloat) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if ((get_class($input_type_part) === TString::class
|| get_class($input_type_part) === TSingleLetter::class
|| $input_type_part instanceof TNonEmptyString
|| $input_type_part instanceof TNonspecificLiteralString)
&& $container_type_part instanceof TLiteralString
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if (($input_type_part instanceof TLowercaseString
|| $input_type_part instanceof TNonEmptyLowercaseString)
&& $container_type_part instanceof TLiteralString
&& strtolower($container_type_part->value) === $container_type_part->value
) {
if ($atomic_comparison_result
&& ($container_type_part->value)
) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
return false;
}
if (($container_type_part instanceof TClassString || $container_type_part instanceof TLiteralClassString)
&& ($input_type_part instanceof TClassString || $input_type_part instanceof TLiteralClassString)
) {
return ClassLikeStringComparator::isContainedBy(
$codebase,
$input_type_part,
$container_type_part,
$allow_interface_equality,
$atomic_comparison_result
);
}
if ($container_type_part instanceof TString && $input_type_part instanceof TTraitString) {
return true;
}
if ($container_type_part instanceof TTraitString
&& (get_class($input_type_part) === TString::class
|| $input_type_part instanceof TNonEmptyString
|| $input_type_part instanceof TNonEmptyNonspecificLiteralString)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if (($input_type_part instanceof TClassString
|| $input_type_part instanceof TLiteralClassString)
&& (get_class($container_type_part) === TSingleLetter::class
|| get_class($container_type_part) === TNonEmptyString::class
|| get_class($container_type_part) === TNonFalsyString::class)
) {
return true;
}
if ($input_type_part instanceof TNumericString
&& get_class($container_type_part) === TNonEmptyString::class
) {
return true;
}
if ($container_type_part instanceof TString
&& $input_type_part instanceof TNumericString
) {
if ($container_type_part instanceof TLiteralString) {
if (is_numeric($container_type_part->value) && $atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
return true;
}
if ($input_type_part instanceof TString
&& $container_type_part instanceof TNumericString
) {
if ($input_type_part instanceof TLiteralString) {
return is_numeric($input_type_part->value);
}
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($container_type_part instanceof TCallableString
&& $input_type_part instanceof TLiteralString
) {
$input_callable = CallableTypeComparator::getCallableFromAtomic($codebase, $input_type_part);
$container_callable = CallableTypeComparator::getCallableFromAtomic($codebase, $container_type_part);
if ($input_callable && $container_callable) {
if (CallableTypeComparator::isContainedBy(
$codebase,
$input_callable,
$container_callable,
$atomic_comparison_result ?? new TypeComparisonResult()
) === false
) {
return false;
}
}
if (!$input_callable) {
//we could not find a callable for the input type, so the input is not contained in the container
return false;
}
return true;
}
if ($input_type_part instanceof TLowercaseString
&& get_class($container_type_part) === TNonEmptyString::class) {
return false;
}
if ($input_type_part->getKey() === $container_type_part->getKey()) {
return true;
}
if (($container_type_part instanceof TClassString
|| $container_type_part instanceof TLiteralClassString
|| $container_type_part instanceof TCallableString)
&& $input_type_part instanceof TString
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
if ($container_type_part instanceof TNumeric
&& $input_type_part->isNumericType()
) {
return true;
}
if ($input_type_part instanceof TNumeric) {
if ($container_type_part->isNumericType()) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}
}
}
if ($input_type_part instanceof Scalar) {
if (!$container_type_part instanceof TLiteralInt
&& !$container_type_part instanceof TLiteralString
&& !$container_type_part instanceof TLiteralFloat
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced
= $atomic_comparison_result->type_coerced_from_scalar
= ($input_type_part instanceof TScalar || $input_type_part instanceof TNonEmptyScalar);
$atomic_comparison_result->scalar_type_match_found = !$container_type_part->from_docblock;
}
}
}
return false;
}
}