1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Type\Accessory\AccessoryNumericStringType;
6: use PHPStan\Type\Constant\ConstantArrayType;
7: use PHPStan\Type\Constant\ConstantIntegerType;
8: use PHPStan\Type\Traits\NonCallableTypeTrait;
9: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
10: use PHPStan\Type\Traits\NonGenericTypeTrait;
11: use PHPStan\Type\Traits\NonIterableTypeTrait;
12: use PHPStan\Type\Traits\NonObjectTypeTrait;
13: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
14: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
15: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
16:
17: /** @api */
18: class IntegerType implements Type
19: {
20:
21: use JustNullableTypeTrait;
22: use NonCallableTypeTrait;
23: use NonIterableTypeTrait;
24: use NonObjectTypeTrait;
25: use UndecidedBooleanTypeTrait;
26: use UndecidedComparisonTypeTrait;
27: use NonGenericTypeTrait;
28: use NonOffsetAccessibleTypeTrait;
29: use NonGeneralizableTypeTrait;
30:
31: /** @api */
32: public function __construct()
33: {
34: }
35:
36: public function describe(VerbosityLevel $level): string
37: {
38: return 'int';
39: }
40:
41: /**
42: * @param mixed[] $properties
43: */
44: public static function __set_state(array $properties): Type
45: {
46: return new self();
47: }
48:
49: public function toNumber(): Type
50: {
51: return $this;
52: }
53:
54: public function toFloat(): Type
55: {
56: return new FloatType();
57: }
58:
59: public function toInteger(): Type
60: {
61: return $this;
62: }
63:
64: public function toString(): Type
65: {
66: return new IntersectionType([
67: new StringType(),
68: new AccessoryNumericStringType(),
69: ]);
70: }
71:
72: public function toArray(): Type
73: {
74: return new ConstantArrayType(
75: [new ConstantIntegerType(0)],
76: [$this],
77: [1],
78: );
79: }
80:
81: public function tryRemove(Type $typeToRemove): ?Type
82: {
83: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
84: if ($typeToRemove instanceof IntegerRangeType) {
85: $removeValueMin = $typeToRemove->getMin();
86: $removeValueMax = $typeToRemove->getMax();
87: } else {
88: $removeValueMin = $typeToRemove->getValue();
89: $removeValueMax = $typeToRemove->getValue();
90: }
91: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
92: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
93: if ($lowerPart !== null && $upperPart !== null) {
94: return new UnionType([$lowerPart, $upperPart]);
95: }
96: return $lowerPart ?? $upperPart ?? new NeverType();
97: }
98:
99: return null;
100: }
101:
102: }
103: