forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerRangeType.php
710 lines (586 loc) · 17.7 KB
/
IntegerRangeType.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use function array_filter;
use function assert;
use function ceil;
use function count;
use function floor;
use function get_class;
use function is_float;
use function is_int;
use function max;
use function min;
use function sprintf;
use const PHP_INT_MAX;
use const PHP_INT_MIN;
/** @api */
class IntegerRangeType extends IntegerType implements CompoundType
{
private function __construct(private ?int $min, private ?int $max)
{
parent::__construct();
assert($min === null || $max === null || $min <= $max);
assert($min !== null || $max !== null);
}
public static function fromInterval(?int $min, ?int $max, int $shift = 0): Type
{
if ($min !== null && $max !== null) {
if ($min > $max) {
return new NeverType();
}
if ($min === $max) {
return new ConstantIntegerType($min + $shift);
}
}
if ($min === null && $max === null) {
return new IntegerType();
}
return (new self($min, $max))->shift($shift);
}
protected static function isDisjoint(?int $minA, ?int $maxA, ?int $minB, ?int $maxB, bool $touchingIsDisjoint = true): bool
{
$offset = $touchingIsDisjoint ? 0 : 1;
return $minA !== null && $maxB !== null && $minA > $maxB + $offset
|| $maxA !== null && $minB !== null && $maxA + $offset < $minB;
}
/**
* Return the range of integers smaller than the given value
*
* @param int|float $value
*/
public static function createAllSmallerThan($value): Type
{
if (is_int($value)) {
return self::fromInterval(null, $value, -1);
}
if ($value > PHP_INT_MAX) {
return new IntegerType();
}
if ($value <= PHP_INT_MIN) {
return new NeverType();
}
return self::fromInterval(null, (int) ceil($value), -1);
}
/**
* Return the range of integers smaller than or equal to the given value
*
* @param int|float $value
*/
public static function createAllSmallerThanOrEqualTo($value): Type
{
if (is_int($value)) {
return self::fromInterval(null, $value);
}
if ($value >= PHP_INT_MAX) {
return new IntegerType();
}
if ($value < PHP_INT_MIN) {
return new NeverType();
}
return self::fromInterval(null, (int) floor($value));
}
/**
* Return the range of integers greater than the given value
*
* @param int|float $value
*/
public static function createAllGreaterThan($value): Type
{
if (is_int($value)) {
return self::fromInterval($value, null, 1);
}
if ($value < PHP_INT_MIN) {
return new IntegerType();
}
if ($value >= PHP_INT_MAX) {
return new NeverType();
}
return self::fromInterval((int) floor($value), null, 1);
}
/**
* Return the range of integers greater than or equal to the given value
*
* @param int|float $value
*/
public static function createAllGreaterThanOrEqualTo($value): Type
{
if (is_int($value)) {
return self::fromInterval($value, null);
}
if ($value <= PHP_INT_MIN) {
return new IntegerType();
}
if ($value > PHP_INT_MAX) {
return new NeverType();
}
return self::fromInterval((int) ceil($value), null);
}
public function getMin(): ?int
{
return $this->min;
}
public function getMax(): ?int
{
return $this->max;
}
public function describe(VerbosityLevel $level): string
{
return sprintf('int<%s, %s>', $this->min ?? 'min', $this->max ?? 'max');
}
public function shift(int $amount): Type
{
if ($amount === 0) {
return $this;
}
$min = $this->min;
$max = $this->max;
if ($amount < 0) {
if ($max !== null) {
if ($max < PHP_INT_MIN - $amount) {
return new NeverType();
}
$max += $amount;
}
if ($min !== null) {
$min = $min < PHP_INT_MIN - $amount ? null : $min + $amount;
}
} else {
if ($min !== null) {
if ($min > PHP_INT_MAX - $amount) {
return new NeverType();
}
$min += $amount;
}
if ($max !== null) {
$max = $max > PHP_INT_MAX - $amount ? null : $max + $amount;
}
}
return self::fromInterval($min, $max);
}
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
return $this->acceptsWithReason($type, $strictTypes)->result;
}
public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
{
if ($type instanceof parent) {
return new AcceptsResult($this->isSuperTypeOf($type), []);
}
if ($type instanceof CompoundType) {
return $type->isAcceptedWithReasonBy($this, $strictTypes);
}
return AcceptsResult::createNo();
}
public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof self || $type instanceof ConstantIntegerType) {
if ($type instanceof self) {
$typeMin = $type->min;
$typeMax = $type->max;
} else {
$typeMin = $type->getValue();
$typeMax = $type->getValue();
}
if (self::isDisjoint($this->min, $this->max, $typeMin, $typeMax)) {
return TrinaryLogic::createNo();
}
if (
($this->min === null || $typeMin !== null && $this->min <= $typeMin)
&& ($this->max === null || $typeMax !== null && $this->max >= $typeMax)
) {
return TrinaryLogic::createYes();
}
return TrinaryLogic::createMaybe();
}
if ($type instanceof parent) {
return TrinaryLogic::createMaybe();
}
if ($type instanceof CompoundType) {
return $type->isSubTypeOf($this);
}
return TrinaryLogic::createNo();
}
public function isSubTypeOf(Type $otherType): TrinaryLogic
{
if ($otherType instanceof parent) {
return $otherType->isSuperTypeOf($this);
}
if ($otherType instanceof UnionType) {
return $this->isSubTypeOfUnion($otherType);
}
if ($otherType instanceof IntersectionType) {
return $otherType->isSuperTypeOf($this);
}
return TrinaryLogic::createNo();
}
private function isSubTypeOfUnion(UnionType $otherType): TrinaryLogic
{
if ($this->min !== null && $this->max !== null) {
$matchingConstantIntegers = array_filter(
$otherType->getTypes(),
fn (Type $type): bool => $type instanceof ConstantIntegerType && $type->getValue() >= $this->min && $type->getValue() <= $this->max,
);
if (count($matchingConstantIntegers) === ($this->max - $this->min + 1)) {
return TrinaryLogic::createYes();
}
}
return TrinaryLogic::createNo()->lazyOr($otherType->getTypes(), fn (Type $innerType) => $this->isSubTypeOf($innerType));
}
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
{
return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result;
}
public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
{
return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
}
public function equals(Type $type): bool
{
return $type instanceof self && $this->min === $type->min && $this->max === $type->max;
}
public function generalize(GeneralizePrecision $precision): Type
{
return new IntegerType();
}
public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createYes();
} else {
$minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThan($otherType, $phpVersion);
}
if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createNo();
} else {
$maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThan($otherType, $phpVersion);
}
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createYes();
} else {
$minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThanOrEqual($otherType, $phpVersion);
}
if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createNo();
} else {
$maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThanOrEqual($otherType, $phpVersion);
}
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}
public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createNo();
} else {
$minIsSmaller = $otherType->isSmallerThan((new ConstantIntegerType($this->min)), $phpVersion);
}
if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createYes();
} else {
$maxIsSmaller = $otherType->isSmallerThan((new ConstantIntegerType($this->max)), $phpVersion);
}
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createNo();
} else {
$minIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->min)), $phpVersion);
}
if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createYes();
} else {
$maxIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->max)), $phpVersion);
}
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}
public function getSmallerType(PhpVersion $phpVersion): Type
{
$subtractedTypes = [
new ConstantBooleanType(true),
];
if ($this->max !== null) {
$subtractedTypes[] = self::createAllGreaterThanOrEqualTo($this->max);
}
return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}
public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
{
$subtractedTypes = [];
if ($this->max !== null) {
$subtractedTypes[] = self::createAllGreaterThan($this->max);
}
return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}
public function getGreaterType(PhpVersion $phpVersion): Type
{
$subtractedTypes = [
new NullType(),
new ConstantBooleanType(false),
];
if ($this->min !== null) {
$subtractedTypes[] = self::createAllSmallerThanOrEqualTo($this->min);
}
if ($this->min !== null && $this->min > 0 || $this->max !== null && $this->max < 0) {
$subtractedTypes[] = new ConstantBooleanType(true);
}
return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}
public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
{
$subtractedTypes = [];
if ($this->min !== null) {
$subtractedTypes[] = self::createAllSmallerThan($this->min);
}
if ($this->min !== null && $this->min > 0 || $this->max !== null && $this->max < 0) {
$subtractedTypes[] = new NullType();
$subtractedTypes[] = new ConstantBooleanType(false);
}
return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}
public function toBoolean(): BooleanType
{
$isZero = (new ConstantIntegerType(0))->isSuperTypeOf($this);
if ($isZero->no()) {
return new ConstantBooleanType(true);
}
if ($isZero->maybe()) {
return new BooleanType();
}
return new ConstantBooleanType(false);
}
public function toAbsoluteNumber(): Type
{
if ($this->min !== null && $this->min >= 0) {
return $this;
}
if ($this->max === null || $this->max >= 0) {
$inversedMin = $this->min !== null ? $this->min * -1 : null;
return self::fromInterval(0, $inversedMin !== null && $this->max !== null ? max($inversedMin, $this->max) : null);
}
return self::fromInterval($this->max * -1, $this->min !== null ? $this->min * -1 : null);
}
public function toString(): Type
{
$isZero = (new ConstantIntegerType(0))->isSuperTypeOf($this);
if ($isZero->no()) {
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
new AccessoryNonFalsyStringType(),
]);
}
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]);
}
/**
* Return the union with another type, but only if it can be expressed in a simpler way than using UnionType
*
*/
public function tryUnion(Type $otherType): ?Type
{
if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
if ($otherType instanceof self) {
$otherMin = $otherType->min;
$otherMax = $otherType->max;
} else {
$otherMin = $otherType->getValue();
$otherMax = $otherType->getValue();
}
if (self::isDisjoint($this->min, $this->max, $otherMin, $otherMax, false)) {
return null;
}
return self::fromInterval(
$this->min !== null && $otherMin !== null ? min($this->min, $otherMin) : null,
$this->max !== null && $otherMax !== null ? max($this->max, $otherMax) : null,
);
}
if (get_class($otherType) === parent::class) {
return $otherType;
}
return null;
}
/**
* Return the intersection with another type, but only if it can be expressed in a simpler way than using
* IntersectionType
*
*/
public function tryIntersect(Type $otherType): ?Type
{
if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
if ($otherType instanceof self) {
$otherMin = $otherType->min;
$otherMax = $otherType->max;
} else {
$otherMin = $otherType->getValue();
$otherMax = $otherType->getValue();
}
if (self::isDisjoint($this->min, $this->max, $otherMin, $otherMax, false)) {
return new NeverType();
}
if ($this->min === null) {
$newMin = $otherMin;
} elseif ($otherMin === null) {
$newMin = $this->min;
} else {
$newMin = max($this->min, $otherMin);
}
if ($this->max === null) {
$newMax = $otherMax;
} elseif ($otherMax === null) {
$newMax = $this->max;
} else {
$newMax = min($this->max, $otherMax);
}
return self::fromInterval($newMin, $newMax);
}
if (get_class($otherType) === parent::class) {
return $this;
}
return null;
}
/**
* Return the different with another type, or null if it cannot be represented.
*
*/
public function tryRemove(Type $typeToRemove): ?Type
{
if (get_class($typeToRemove) === parent::class) {
return new NeverType();
}
if ($typeToRemove instanceof self || $typeToRemove instanceof ConstantIntegerType) {
if ($typeToRemove instanceof self) {
$removeMin = $typeToRemove->min;
$removeMax = $typeToRemove->max;
} else {
$removeMin = $typeToRemove->getValue();
$removeMax = $typeToRemove->getValue();
}
if (
$this->min !== null && $removeMax !== null && $removeMax < $this->min
|| $this->max !== null && $removeMin !== null && $this->max < $removeMin
) {
return $this;
}
if ($removeMin !== null && $removeMin !== PHP_INT_MIN) {
$lowerPart = self::fromInterval($this->min, $removeMin - 1);
} else {
$lowerPart = null;
}
if ($removeMax !== null && $removeMax !== PHP_INT_MAX) {
$upperPart = self::fromInterval($removeMax + 1, $this->max);
} else {
$upperPart = null;
}
if ($lowerPart !== null && $upperPart !== null) {
return TypeCombinator::union($lowerPart, $upperPart);
}
return $lowerPart ?? $upperPart;
}
return null;
}
public function exponentiate(Type $exponent): Type
{
if ($exponent instanceof UnionType) {
$results = [];
foreach ($exponent->getTypes() as $unionType) {
$results[] = $this->exponentiate($unionType);
}
return TypeCombinator::union(...$results);
}
if ($exponent instanceof IntegerRangeType) {
$min = null;
$max = null;
if ($this->getMin() !== null && $exponent->getMin() !== null) {
$min = $this->getMin() ** $exponent->getMin();
}
if ($this->getMax() !== null && $exponent->getMax() !== null) {
$max = $this->getMax() ** $exponent->getMax();
}
if (($min !== null || $max !== null) && !is_float($min) && !is_float($max)) {
return self::fromInterval($min, $max);
}
}
if ($exponent instanceof ConstantScalarType) {
$exponentValue = $exponent->getValue();
if (is_int($exponentValue)) {
$min = null;
$max = null;
if ($this->getMin() !== null) {
$min = $this->getMin() ** $exponentValue;
}
if ($this->getMax() !== null) {
$max = $this->getMax() ** $exponentValue;
}
if (!is_float($min) && !is_float($max)) {
return self::fromInterval($min, $max);
}
}
}
return parent::exponentiate($exponent);
}
/**
* @return list<ConstantIntegerType>
*/
public function getFiniteTypes(): array
{
if ($this->min === null || $this->max === null) {
return [];
}
$size = $this->max - $this->min;
if ($size > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
return [];
}
$types = [];
for ($i = $this->min; $i <= $this->max; $i++) {
$types[] = new ConstantIntegerType($i);
}
return $types;
}
public function toPhpDocNode(): TypeNode
{
if ($this->min === null) {
$min = new IdentifierTypeNode('min');
} else {
$min = new ConstTypeNode(new ConstExprIntegerNode((string) $this->min));
}
if ($this->max === null) {
$max = new IdentifierTypeNode('max');
} else {
$max = new ConstTypeNode(new ConstExprIntegerNode((string) $this->max));
}
return new GenericTypeNode(new IdentifierTypeNode('int'), [$min, $max]);
}
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
{
if ($this->isSmallerThan($type, $phpVersion)->yes() || $this->isGreaterThan($type, $phpVersion)->yes()) {
return new ConstantBooleanType(false);
}
return parent::looseCompare($type, $phpVersion);
}
/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): Type
{
return new self($properties['min'], $properties['max']);
}
}