1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
7: use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
9: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
10: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
11: use PHPStan\Reflection\InitializerExprTypeResolver;
12: use PHPStan\TrinaryLogic;
13: use PHPStan\Turbo\ShadowedByTurboExtension;
14: use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
15: use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
16: use PHPStan\Type\Constant\ConstantBooleanType;
17: use PHPStan\Type\Constant\ConstantFloatType;
18: use PHPStan\Type\Constant\ConstantIntegerType;
19: use PHPStan\Type\Traits\OperatorTypeTrait;
20: use function array_filter;
21: use function array_map;
22: use function assert;
23: use function ceil;
24: use function count;
25: use function floor;
26: use function get_class;
27: use function is_int;
28: use function is_nan;
29: use function is_numeric;
30: use function is_string;
31: use function max;
32: use function min;
33: use function sprintf;
34: use const NAN;
35: use const PHP_INT_MAX;
36: use const PHP_INT_MIN;
37:
38: /** @api */
39: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/IntegerRangeType.cpp')]
40: class IntegerRangeType extends IntegerType implements CompoundType
41: {
42:
43: use OperatorTypeTrait;
44:
45: private function __construct(private ?int $min, private ?int $max)
46: {
47: parent::__construct();
48: assert($min === null || $max === null || $min <= $max);
49: assert($min !== null || $max !== null);
50: }
51:
52: public static function fromInterval(?int $min, ?int $max, int $shift = 0): Type
53: {
54: if ($min !== null && $max !== null) {
55: if ($min > $max) {
56: return new NeverType();
57: }
58: if ($min === $max) {
59: return new ConstantIntegerType($min + $shift);
60: }
61: }
62:
63: if ($min === null && $max === null) {
64: return new IntegerType();
65: }
66:
67: $range = (new self($min, $max))->shift($shift);
68: if (!$range instanceof self) {
69: return $range;
70: }
71:
72: // Nothing is smaller than the smallest integer, and nothing is bigger than the biggest one,
73: // so an unbounded side that reaches either one holds a single value.
74: if ($range->min === null && $range->max === PHP_INT_MIN) {
75: return new ConstantIntegerType(PHP_INT_MIN);
76: }
77: if ($range->min === PHP_INT_MAX && $range->max === null) {
78: return new ConstantIntegerType(PHP_INT_MAX);
79: }
80:
81: return $range;
82: }
83:
84: protected static function isDisjoint(?int $minA, ?int $maxA, ?int $minB, ?int $maxB, bool $touchingIsDisjoint = true): bool
85: {
86: $offset = $touchingIsDisjoint ? 0 : 1;
87: return $minA !== null && $maxB !== null && $minA > $maxB + $offset
88: || $maxA !== null && $minB !== null && $maxA + $offset < $minB;
89: }
90:
91: /**
92: * Return the range of integers smaller than the given value
93: *
94: * @param int|float $value
95: */
96: public static function createAllSmallerThan($value): Type
97: {
98: if (is_int($value)) {
99: return self::fromInterval(null, $value, -1);
100: }
101:
102: // a float never holds PHP_INT_MAX itself, so the first float past the
103: // int range is (float) PHP_INT_MAX: every int is smaller than that
104: if ($value >= PHP_INT_MAX) {
105: return new IntegerType();
106: }
107:
108: if ($value <= PHP_INT_MIN) {
109: return new NeverType();
110: }
111:
112: // (int) NAN is 0, but PHP 8.5 warns when it casts NAN
113: return self::fromInterval(null, is_nan($value) ? 0 : (int) ceil($value), -1);
114: }
115:
116: /**
117: * Return the range of integers smaller than or equal to the given value
118: *
119: * @param int|float $value
120: */
121: public static function createAllSmallerThanOrEqualTo($value): Type
122: {
123: if (is_int($value)) {
124: return self::fromInterval(null, $value);
125: }
126:
127: if ($value >= PHP_INT_MAX) {
128: return new IntegerType();
129: }
130:
131: if ($value < PHP_INT_MIN) {
132: return new NeverType();
133: }
134:
135: return self::fromInterval(null, is_nan($value) ? 0 : (int) floor($value));
136: }
137:
138: /**
139: * Return the range of integers greater than the given value
140: *
141: * @param int|float $value
142: */
143: public static function createAllGreaterThan($value): Type
144: {
145: if (is_int($value)) {
146: return self::fromInterval($value, null, 1);
147: }
148:
149: if ($value < PHP_INT_MIN) {
150: return new IntegerType();
151: }
152:
153: if ($value >= PHP_INT_MAX) {
154: return new NeverType();
155: }
156:
157: return self::fromInterval(is_nan($value) ? 0 : (int) floor($value), null, 1);
158: }
159:
160: /**
161: * Return the range of integers greater than or equal to the given value
162: *
163: * @param int|float $value
164: */
165: public static function createAllGreaterThanOrEqualTo($value): Type
166: {
167: if (is_int($value)) {
168: return self::fromInterval($value, null);
169: }
170:
171: if ($value <= PHP_INT_MIN) {
172: return new IntegerType();
173: }
174:
175: // (float) PHP_INT_MAX is already past the int range, so no int
176: // reaches it
177: if ($value >= PHP_INT_MAX) {
178: return new NeverType();
179: }
180:
181: return self::fromInterval(is_nan($value) ? 0 : (int) ceil($value), null);
182: }
183:
184: public function toNumberSet(): NumberSet
185: {
186: return new NumberSet([[$this->min, $this->max]], [], false);
187: }
188:
189: public function getMin(): ?int
190: {
191: return $this->min;
192: }
193:
194: public function getMax(): ?int
195: {
196: return $this->max;
197: }
198:
199: public function describe(VerbosityLevel $level): string
200: {
201: return sprintf('int<%s, %s>', $this->min ?? 'min', $this->max ?? 'max');
202: }
203:
204: public function shift(int $amount): Type
205: {
206: if ($amount === 0) {
207: return $this;
208: }
209:
210: $min = $this->min;
211: $max = $this->max;
212:
213: if ($amount < 0) {
214: if ($max !== null) {
215: if ($max < PHP_INT_MIN - $amount) {
216: return new NeverType();
217: }
218: $max += $amount;
219: }
220: if ($min !== null) {
221: $min = $min < PHP_INT_MIN - $amount ? null : $min + $amount;
222: }
223: } else {
224: if ($min !== null) {
225: if ($min > PHP_INT_MAX - $amount) {
226: return new NeverType();
227: }
228: $min += $amount;
229: }
230: if ($max !== null) {
231: $max = $max > PHP_INT_MAX - $amount ? null : $max + $amount;
232: }
233: }
234:
235: return self::fromInterval($min, $max);
236: }
237:
238: public function accepts(Type $type, bool $strictTypes): AcceptsResult
239: {
240: if ($type instanceof parent) {
241: return $this->isSuperTypeOf($type)->toAcceptsResult();
242: }
243:
244: if ($type instanceof CompoundType) {
245: return $type->isAcceptedBy($this, $strictTypes);
246: }
247:
248: return AcceptsResult::createNo();
249: }
250:
251: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
252: {
253: if ($type instanceof self || $type instanceof ConstantIntegerType) {
254: if ($type instanceof self) {
255: $typeMin = $type->min;
256: $typeMax = $type->max;
257: } else {
258: $typeMin = $type->getValue();
259: $typeMax = $type->getValue();
260: }
261:
262: if (self::isDisjoint($this->min, $this->max, $typeMin, $typeMax)) {
263: return IsSuperTypeOfResult::createNo();
264: }
265:
266: if (
267: ($this->min === null || $typeMin !== null && $this->min <= $typeMin)
268: && ($this->max === null || $typeMax !== null && $this->max >= $typeMax)
269: ) {
270: return IsSuperTypeOfResult::createYes();
271: }
272:
273: return IsSuperTypeOfResult::createMaybe();
274: }
275:
276: if ($type instanceof parent) {
277: return IsSuperTypeOfResult::createMaybe();
278: }
279:
280: if ($type instanceof CompoundType) {
281: return $type->isSubTypeOf($this);
282: }
283:
284: return IsSuperTypeOfResult::createNo();
285: }
286:
287: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
288: {
289: if ($otherType instanceof parent) {
290: return $otherType->isSuperTypeOf($this);
291: }
292:
293: if ($otherType instanceof UnionType) {
294: return $this->isSubTypeOfUnionWithReason($otherType);
295: }
296:
297: if ($otherType instanceof IntersectionType) {
298: return $otherType->isSuperTypeOf($this);
299: }
300:
301: return IsSuperTypeOfResult::createNo();
302: }
303:
304: private function isSubTypeOfUnionWithReason(UnionType $otherType): IsSuperTypeOfResult
305: {
306: if ($this->min !== null && $this->max !== null) {
307: $matchingConstantIntegers = array_filter(
308: $otherType->getTypes(),
309: fn (Type $type): bool => $type instanceof ConstantIntegerType && $type->getValue() >= $this->min && $type->getValue() <= $this->max,
310: );
311:
312: if (count($matchingConstantIntegers) === ($this->max - $this->min + 1)) {
313: return IsSuperTypeOfResult::createYes();
314: }
315: }
316:
317: return IsSuperTypeOfResult::createNo()->or(...array_map(fn (Type $innerType) => $this->isSubTypeOf($innerType), $otherType->getTypes()));
318: }
319:
320: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
321: {
322: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
323: }
324:
325: public function equals(Type $type): bool
326: {
327: return $type instanceof self && $this->min === $type->min && $this->max === $type->max;
328: }
329:
330: public function generalize(GeneralizePrecision $precision): Type
331: {
332: return new IntegerType();
333: }
334:
335: public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
336: {
337: $unorderedComparison = $this->compareEachInteger($otherType, $phpVersion, static fn (ConstantIntegerType $integer): TrinaryLogic => $integer->isSmallerThan($otherType, $phpVersion));
338: if ($unorderedComparison !== null) {
339: return $unorderedComparison;
340: }
341:
342: if ($this->min === null) {
343: $minIsSmaller = TrinaryLogic::createYes();
344: } else {
345: $minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThan($otherType, $phpVersion);
346: }
347:
348: if ($this->max === null) {
349: $maxIsSmaller = TrinaryLogic::createNo();
350: } else {
351: $maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThan($otherType, $phpVersion);
352: }
353:
354: // 0 can have different results in contrast to the interval edges, see https://3v4l.org/iGoti
355: $zeroInt = new ConstantIntegerType(0);
356: if (!$zeroInt->isSuperTypeOf($this)->no()) {
357: return TrinaryLogic::extremeIdentity(
358: $zeroInt->isSmallerThan($otherType, $phpVersion),
359: $minIsSmaller,
360: $maxIsSmaller,
361: );
362: }
363:
364: return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
365: }
366:
367: public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
368: {
369: $unorderedComparison = $this->compareEachInteger($otherType, $phpVersion, static fn (ConstantIntegerType $integer): TrinaryLogic => $integer->isSmallerThanOrEqual($otherType, $phpVersion));
370: if ($unorderedComparison !== null) {
371: return $unorderedComparison;
372: }
373:
374: if ($this->min === null) {
375: $minIsSmaller = TrinaryLogic::createYes();
376: } else {
377: $minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThanOrEqual($otherType, $phpVersion);
378: }
379:
380: if ($this->max === null) {
381: $maxIsSmaller = TrinaryLogic::createNo();
382: } else {
383: $maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThanOrEqual($otherType, $phpVersion);
384: }
385:
386: // 0 can have different results in contrast to the interval edges, see https://3v4l.org/iGoti
387: $zeroInt = new ConstantIntegerType(0);
388: if (!$zeroInt->isSuperTypeOf($this)->no()) {
389: return TrinaryLogic::extremeIdentity(
390: $zeroInt->isSmallerThanOrEqual($otherType, $phpVersion),
391: $minIsSmaller,
392: $maxIsSmaller,
393: );
394: }
395:
396: return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
397: }
398:
399: public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
400: {
401: $unorderedComparison = $this->compareEachInteger($otherType, $phpVersion, static fn (ConstantIntegerType $integer): TrinaryLogic => $otherType->isSmallerThan($integer, $phpVersion));
402: if ($unorderedComparison !== null) {
403: return $unorderedComparison;
404: }
405:
406: if ($this->min === null) {
407: $minIsSmaller = TrinaryLogic::createNo();
408: } else {
409: $minIsSmaller = $otherType->isSmallerThan(new ConstantIntegerType($this->min), $phpVersion);
410: }
411:
412: if ($this->max === null) {
413: $maxIsSmaller = TrinaryLogic::createYes();
414: } else {
415: $maxIsSmaller = $otherType->isSmallerThan(new ConstantIntegerType($this->max), $phpVersion);
416: }
417:
418: // 0 can have different results in contrast to the interval edges, see https://3v4l.org/iGoti
419: $zeroInt = new ConstantIntegerType(0);
420: if (!$zeroInt->isSuperTypeOf($this)->no()) {
421: return TrinaryLogic::extremeIdentity(
422: $otherType->isSmallerThan($zeroInt, $phpVersion),
423: $minIsSmaller,
424: $maxIsSmaller,
425: );
426: }
427:
428: return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
429: }
430:
431: public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
432: {
433: $unorderedComparison = $this->compareEachInteger($otherType, $phpVersion, static fn (ConstantIntegerType $integer): TrinaryLogic => $otherType->isSmallerThanOrEqual($integer, $phpVersion));
434: if ($unorderedComparison !== null) {
435: return $unorderedComparison;
436: }
437:
438: if ($this->min === null) {
439: $minIsSmaller = TrinaryLogic::createNo();
440: } else {
441: $minIsSmaller = $otherType->isSmallerThanOrEqual(new ConstantIntegerType($this->min), $phpVersion);
442: }
443:
444: if ($this->max === null) {
445: $maxIsSmaller = TrinaryLogic::createYes();
446: } else {
447: $maxIsSmaller = $otherType->isSmallerThanOrEqual(new ConstantIntegerType($this->max), $phpVersion);
448: }
449:
450: // 0 can have different results in contrast to the interval edges, see https://3v4l.org/iGoti
451: $zeroInt = new ConstantIntegerType(0);
452: if (!$zeroInt->isSuperTypeOf($this)->no()) {
453: return TrinaryLogic::extremeIdentity(
454: $otherType->isSmallerThanOrEqual($zeroInt, $phpVersion),
455: $minIsSmaller,
456: $maxIsSmaller,
457: );
458: }
459:
460: return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
461: }
462:
463: /**
464: * PHP 8 compares an integer with a non-numeric string as strings, which
465: * does not keep the order of the integers ('10' < '1abc' < '2'), so the
466: * bounds do not decide the comparison then and each integer does.
467: *
468: * @param callable(ConstantIntegerType): TrinaryLogic $compare
469: */
470: private function compareEachInteger(Type $otherType, PhpVersion $phpVersion, callable $compare): ?TrinaryLogic
471: {
472: if (!$phpVersion->castsNumbersToStringsOnLooseComparison()) {
473: return null;
474: }
475:
476: $hasNonNumericString = false;
477: foreach ($otherType->getConstantScalarValues() as $value) {
478: if (is_string($value) && !is_numeric($value)) {
479: $hasNonNumericString = true;
480: break;
481: }
482: }
483: if (!$hasNonNumericString) {
484: return null;
485: }
486:
487: $integers = $this->getFiniteTypes();
488: if ($integers === []) {
489: return TrinaryLogic::createMaybe();
490: }
491:
492: return TrinaryLogic::extremeIdentity(...array_map($compare, $integers));
493: }
494:
495: public function getSmallerType(PhpVersion $phpVersion): Type
496: {
497: $subtractedTypes = [
498: new ConstantBooleanType(true),
499: new ConstantFloatType(NAN),
500: ];
501:
502: if ($this->max !== null) {
503: $subtractedTypes[] = self::createAllGreaterThanOrEqualTo($this->max);
504: $subtractedTypes[] = FloatRangeType::createAllGreaterThanOrEqualTo($this->max);
505: }
506:
507: return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
508: }
509:
510: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
511: {
512: $subtractedTypes = [
513: new ConstantFloatType(NAN),
514: ];
515:
516: if ($this->max !== null) {
517: $subtractedTypes[] = self::createAllGreaterThan($this->max);
518: $subtractedTypes[] = FloatRangeType::createAllGreaterThan($this->max);
519: }
520:
521: return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
522: }
523:
524: public function getGreaterType(PhpVersion $phpVersion): Type
525: {
526: $subtractedTypes = [
527: new NullType(),
528: new ConstantBooleanType(false),
529: new ConstantFloatType(NAN),
530: ];
531:
532: if ($this->min !== null) {
533: $subtractedTypes[] = self::createAllSmallerThanOrEqualTo($this->min);
534: $subtractedTypes[] = FloatRangeType::createAllSmallerThanOrEqualTo($this->min);
535: }
536:
537: if ($this->min !== null && $this->min > 0 || $this->max !== null && $this->max < 0) {
538: $subtractedTypes[] = new ConstantBooleanType(true);
539: }
540:
541: return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
542: }
543:
544: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
545: {
546: $subtractedTypes = [
547: new ConstantFloatType(NAN),
548: ];
549:
550: if ($this->min !== null) {
551: $subtractedTypes[] = self::createAllSmallerThan($this->min);
552: $subtractedTypes[] = FloatRangeType::createAllSmallerThan($this->min);
553: }
554:
555: if ($this->min !== null && $this->min > 0 || $this->max !== null && $this->max < 0) {
556: $subtractedTypes[] = new NullType();
557: $subtractedTypes[] = new ConstantBooleanType(false);
558: }
559:
560: return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
561: }
562:
563: /** An int converts to the float closest to it, which keeps the order of the ints. */
564: public function toFloat(): Type
565: {
566: return FloatRangeType::fromInterval((float) ($this->min ?? PHP_INT_MIN), (float) ($this->max ?? PHP_INT_MAX));
567: }
568:
569: public function toBoolean(): BooleanType
570: {
571: $isZero = (new ConstantIntegerType(0))->isSuperTypeOf($this);
572: if ($isZero->no()) {
573: return new ConstantBooleanType(true);
574: }
575:
576: if ($isZero->maybe()) {
577: return new BooleanType();
578: }
579:
580: return new ConstantBooleanType(false);
581: }
582:
583: public function toAbsoluteNumber(): Type
584: {
585: if ($this->min !== null && $this->min >= 0) {
586: return $this;
587: }
588:
589: // Negating the smallest integer overflows, so its absolute value is treated as unbounded,
590: // the same way an unbounded lower bound is. This keeps abs(int<min, 0>) and
591: // abs(int<-9223372036854775808, 0>) in agreement.
592: $inversedMin = $this->min !== null && $this->min !== PHP_INT_MIN ? -$this->min : null;
593:
594: if ($this->max === null || $this->max >= 0) {
595: return self::fromInterval(0, $inversedMin !== null && $this->max !== null ? max($inversedMin, $this->max) : null);
596: }
597:
598: return self::fromInterval(-$this->max, $inversedMin);
599: }
600:
601: public function toBitwiseNotType(): Type
602: {
603: // ~int<a, b> = int<~b, ~a> (bitwise NOT reverses the order)
604: return self::fromInterval(
605: $this->max !== null ? ~$this->max : null,
606: $this->min !== null ? ~$this->min : null,
607: );
608: }
609:
610: public function toString(): Type
611: {
612: $finiteTypes = $this->getFiniteTypes();
613: if ($finiteTypes !== []) {
614: return TypeCombinator::union(...$finiteTypes)->toString();
615: }
616:
617: $isZero = (new ConstantIntegerType(0))->isSuperTypeOf($this);
618: if ($isZero->no()) {
619: return new IntersectionType([
620: new StringType(),
621: new AccessoryDecimalIntegerStringType(),
622: new AccessoryNonFalsyStringType(),
623: ]);
624: }
625:
626: return new IntersectionType([
627: new StringType(),
628: new AccessoryDecimalIntegerStringType(),
629: ]);
630: }
631:
632: /**
633: * Return the union with another type, but only if it can be expressed in a simpler way than using UnionType
634: *
635: */
636: public function tryUnion(Type $otherType): ?Type
637: {
638: if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
639: if ($otherType instanceof self) {
640: $otherMin = $otherType->min;
641: $otherMax = $otherType->max;
642: } else {
643: $otherMin = $otherType->getValue();
644: $otherMax = $otherType->getValue();
645: }
646:
647: if (self::isDisjoint($this->min, $this->max, $otherMin, $otherMax, false)) {
648: return null;
649: }
650:
651: return self::fromInterval(
652: $this->min !== null && $otherMin !== null ? min($this->min, $otherMin) : null,
653: $this->max !== null && $otherMax !== null ? max($this->max, $otherMax) : null,
654: );
655: }
656:
657: if (get_class($otherType) === parent::class) {
658: return $otherType;
659: }
660:
661: return null;
662: }
663:
664: /**
665: * Return the intersection with another type, but only if it can be expressed in a simpler way than using
666: * IntersectionType
667: *
668: */
669: public function tryIntersect(Type $otherType): ?Type
670: {
671: if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
672: if ($otherType instanceof self) {
673: $otherMin = $otherType->min;
674: $otherMax = $otherType->max;
675: } else {
676: $otherMin = $otherType->getValue();
677: $otherMax = $otherType->getValue();
678: }
679:
680: if (self::isDisjoint($this->min, $this->max, $otherMin, $otherMax, false)) {
681: return new NeverType();
682: }
683:
684: if ($this->min === null) {
685: $newMin = $otherMin;
686: } elseif ($otherMin === null) {
687: $newMin = $this->min;
688: } else {
689: $newMin = max($this->min, $otherMin);
690: }
691:
692: if ($this->max === null) {
693: $newMax = $otherMax;
694: } elseif ($otherMax === null) {
695: $newMax = $this->max;
696: } else {
697: $newMax = min($this->max, $otherMax);
698: }
699:
700: return self::fromInterval($newMin, $newMax);
701: }
702:
703: if (get_class($otherType) === parent::class) {
704: return $this;
705: }
706:
707: return null;
708: }
709:
710: /**
711: * Return the different with another type, or null if it cannot be represented.
712: *
713: */
714: public function tryRemove(Type $typeToRemove): ?Type
715: {
716: if (get_class($typeToRemove) === parent::class) {
717: return new NeverType();
718: }
719:
720: if ($typeToRemove instanceof self || $typeToRemove instanceof ConstantIntegerType) {
721: if ($typeToRemove instanceof self) {
722: $removeMin = $typeToRemove->min;
723: $removeMax = $typeToRemove->max;
724: } else {
725: $removeMin = $typeToRemove->getValue();
726: $removeMax = $typeToRemove->getValue();
727: }
728:
729: if (
730: $this->min !== null && $removeMax !== null && $removeMax < $this->min
731: || $this->max !== null && $removeMin !== null && $this->max < $removeMin
732: ) {
733: return $this;
734: }
735:
736: if ($removeMin !== null && $removeMin !== PHP_INT_MIN) {
737: $lowerPart = self::fromInterval($this->min, $removeMin - 1);
738: } else {
739: $lowerPart = null;
740: }
741: if ($removeMax !== null && $removeMax !== PHP_INT_MAX) {
742: $upperPart = self::fromInterval($removeMax + 1, $this->max);
743: } else {
744: $upperPart = null;
745: }
746:
747: if ($lowerPart !== null && $upperPart !== null) {
748: return TypeCombinator::union($lowerPart, $upperPart);
749: }
750:
751: return $lowerPart ?? $upperPart;
752: }
753:
754: return null;
755: }
756:
757: /**
758: * @return list<ConstantIntegerType>
759: */
760: public function getFiniteTypes(): array
761: {
762: if ($this->min === null || $this->max === null) {
763: return [];
764: }
765:
766: $size = $this->max - $this->min;
767: if ($size > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
768: return [];
769: }
770:
771: $types = [];
772: for ($i = 0; $i <= $size; $i++) {
773: // $this->min + $size is $this->max, so nothing here overflows —
774: // unlike $i++ past PHP_INT_MAX, which would turn $i into a float
775: $types[] = new ConstantIntegerType($this->min + $i);
776: }
777:
778: return $types;
779: }
780:
781: public function toPhpDocNode(): TypeNode
782: {
783: if ($this->min === null) {
784: $min = new IdentifierTypeNode('min');
785: } else {
786: $min = new ConstTypeNode(new ConstExprIntegerNode((string) $this->min));
787: }
788:
789: if ($this->max === null) {
790: $max = new IdentifierTypeNode('max');
791: } else {
792: $max = new ConstTypeNode(new ConstExprIntegerNode((string) $this->max));
793: }
794:
795: return new GenericTypeNode(new IdentifierTypeNode('int'), [$min, $max]);
796: }
797:
798: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
799: {
800: $zeroInt = new ConstantIntegerType(0);
801: if ($zeroInt->isSuperTypeOf($this)->no()) {
802: if ($type->isTrue()->yes()) {
803: return new ConstantBooleanType(true);
804: }
805: if ($type->isFalse()->yes()) {
806: return new ConstantBooleanType(false);
807: }
808: }
809:
810: if (
811: $this->isSmallerThan($type, $phpVersion)->yes()
812: || $this->isGreaterThan($type, $phpVersion)->yes()
813: ) {
814: return new ConstantBooleanType(false);
815: }
816:
817: return parent::looseCompare($type, $phpVersion);
818: }
819:
820: public function toValueSet(): ValueSet
821: {
822: return ValueSet::createNumbers($this->toNumberSet());
823: }
824:
825: }
826: