1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\TrinaryLogic;
9: use PHPStan\Type\Accessory\AccessoryNumericStringType;
10: use PHPStan\Type\Constant\ConstantArrayType;
11: use PHPStan\Type\Constant\ConstantIntegerType;
12: use PHPStan\Type\Traits\NonArrayTypeTrait;
13: use PHPStan\Type\Traits\NonCallableTypeTrait;
14: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
15: use PHPStan\Type\Traits\NonGenericTypeTrait;
16: use PHPStan\Type\Traits\NonIterableTypeTrait;
17: use PHPStan\Type\Traits\NonObjectTypeTrait;
18: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
19: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
20: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
21:
22: /** @api */
23: class IntegerType implements Type
24: {
25:
26: use JustNullableTypeTrait;
27: use NonArrayTypeTrait;
28: use NonCallableTypeTrait;
29: use NonIterableTypeTrait;
30: use NonObjectTypeTrait;
31: use UndecidedBooleanTypeTrait;
32: use UndecidedComparisonTypeTrait;
33: use NonGenericTypeTrait;
34: use NonOffsetAccessibleTypeTrait;
35: use NonGeneralizableTypeTrait;
36:
37: /** @api */
38: public function __construct()
39: {
40: }
41:
42: public function describe(VerbosityLevel $level): string
43: {
44: return 'int';
45: }
46:
47: public function getConstantStrings(): array
48: {
49: return [];
50: }
51:
52: /**
53: * @param mixed[] $properties
54: */
55: public static function __set_state(array $properties): Type
56: {
57: return new self();
58: }
59:
60: public function toNumber(): Type
61: {
62: return $this;
63: }
64:
65: public function toFloat(): Type
66: {
67: return new FloatType();
68: }
69:
70: public function toInteger(): Type
71: {
72: return $this;
73: }
74:
75: public function toString(): Type
76: {
77: return new IntersectionType([
78: new StringType(),
79: new AccessoryNumericStringType(),
80: ]);
81: }
82:
83: public function toArray(): Type
84: {
85: return new ConstantArrayType(
86: [new ConstantIntegerType(0)],
87: [$this],
88: [1],
89: [],
90: TrinaryLogic::createYes(),
91: );
92: }
93:
94: public function toArrayKey(): Type
95: {
96: return $this;
97: }
98:
99: public function isNull(): TrinaryLogic
100: {
101: return TrinaryLogic::createNo();
102: }
103:
104: public function isTrue(): TrinaryLogic
105: {
106: return TrinaryLogic::createNo();
107: }
108:
109: public function isFalse(): TrinaryLogic
110: {
111: return TrinaryLogic::createNo();
112: }
113:
114: public function isBoolean(): TrinaryLogic
115: {
116: return TrinaryLogic::createNo();
117: }
118:
119: public function isFloat(): TrinaryLogic
120: {
121: return TrinaryLogic::createNo();
122: }
123:
124: public function isInteger(): TrinaryLogic
125: {
126: return TrinaryLogic::createYes();
127: }
128:
129: public function isScalar(): TrinaryLogic
130: {
131: return TrinaryLogic::createYes();
132: }
133:
134: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
135: {
136: return new BooleanType();
137: }
138:
139: public function tryRemove(Type $typeToRemove): ?Type
140: {
141: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
142: if ($typeToRemove instanceof IntegerRangeType) {
143: $removeValueMin = $typeToRemove->getMin();
144: $removeValueMax = $typeToRemove->getMax();
145: } else {
146: $removeValueMin = $typeToRemove->getValue();
147: $removeValueMax = $typeToRemove->getValue();
148: }
149: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
150: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
151: if ($lowerPart !== null && $upperPart !== null) {
152: return new UnionType([$lowerPart, $upperPart]);
153: }
154: return $lowerPart ?? $upperPart ?? new NeverType();
155: }
156:
157: return null;
158: }
159:
160: public function getFiniteTypes(): array
161: {
162: return [];
163: }
164:
165: public function exponentiate(Type $exponent): Type
166: {
167: return ExponentiateHelper::exponentiate($this, $exponent);
168: }
169:
170: public function toPhpDocNode(): TypeNode
171: {
172: return new IdentifierTypeNode('int');
173: }
174:
175: }
176: