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