1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\Accessory\AccessoryNumericStringType;
12: use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
13: use PHPStan\Type\Constant\ConstantArrayType;
14: use PHPStan\Type\Constant\ConstantFloatType;
15: use PHPStan\Type\Constant\ConstantIntegerType;
16: use PHPStan\Type\Constant\ConstantStringType;
17: use PHPStan\Type\Traits\NonArrayTypeTrait;
18: use PHPStan\Type\Traits\NonCallableTypeTrait;
19: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
20: use PHPStan\Type\Traits\NonGenericTypeTrait;
21: use PHPStan\Type\Traits\NonIterableTypeTrait;
22: use PHPStan\Type\Traits\NonObjectTypeTrait;
23: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
24: use PHPStan\Type\Traits\OperatorTypeTrait;
25: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
26: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
27: use function get_class;
28: use function is_finite;
29: use function is_nan;
30: use const INF;
31: use const NAN;
32:
33: /** @api */
34: #[InstanceofDeprecated(insteadUse: 'Type::isFloat()')]
35: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/FloatType.cpp')]
36: class FloatType implements Type
37: {
38:
39: use NonArrayTypeTrait;
40: use NonCallableTypeTrait;
41: use NonIterableTypeTrait;
42: use NonObjectTypeTrait;
43: use UndecidedBooleanTypeTrait;
44: use UndecidedComparisonTypeTrait;
45: use NonGenericTypeTrait;
46: use NonOffsetAccessibleTypeTrait;
47: use NonGeneralizableTypeTrait;
48: use OperatorTypeTrait;
49:
50: /** @api */
51: public function __construct()
52: {
53: }
54:
55: public function getReferencedClasses(): array
56: {
57: return [];
58: }
59:
60: public function getObjectClassNames(): array
61: {
62: return [];
63: }
64:
65: public function getObjectClassReflections(): array
66: {
67: return [];
68: }
69:
70: public function getConstantStrings(): array
71: {
72: return [];
73: }
74:
75: public function accepts(Type $type, bool $strictTypes): AcceptsResult
76: {
77: if ($type instanceof self || $type->isInteger()->yes()) {
78: return AcceptsResult::createYes();
79: }
80:
81: if ($type instanceof CompoundType) {
82: return $type->isAcceptedBy($this, $strictTypes);
83: }
84:
85: return AcceptsResult::createNo();
86: }
87:
88: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
89: {
90: if ($type instanceof self) {
91: return IsSuperTypeOfResult::createYes();
92: }
93:
94: if ($type instanceof CompoundType) {
95: return $type->isSubTypeOf($this);
96: }
97:
98: return IsSuperTypeOfResult::createNo();
99: }
100:
101: /**
102: * float is float<min, max>|NAN: removing NAN leaves the range, removing a
103: * range or an infinity leaves the rest of the range and NAN.
104: */
105: public function tryRemove(Type $typeToRemove): ?Type
106: {
107: // a constant is a single value: removing it takes it or leaves it
108: if ($this->isConstantValue()->yes()) {
109: return null;
110: }
111:
112: $notNan = FloatRangeType::fromInterval(-INF, INF);
113: if ($typeToRemove instanceof ConstantFloatType && is_nan($typeToRemove->getValue())) {
114: return $notNan;
115: }
116:
117: // a finite constant would split the range: float !== 1.5 stays float
118: // rather than spelling out the two ranges around 1.5
119: if ($typeToRemove instanceof ConstantFloatType && is_finite($typeToRemove->getValue())) {
120: return null;
121: }
122:
123: $removed = $notNan->tryRemove($typeToRemove);
124: if ($removed === null) {
125: return null;
126: }
127:
128: return TypeCombinator::union($removed, new ConstantFloatType(NAN));
129: }
130:
131: public function equals(Type $type): bool
132: {
133: return get_class($type) === static::class;
134: }
135:
136: public function describe(VerbosityLevel $level): string
137: {
138: return 'float';
139: }
140:
141: public function toNumber(): Type
142: {
143: return $this;
144: }
145:
146: public function toNumberSet(): NumberSet
147: {
148: return new NumberSet([], [[-INF, INF]], true);
149: }
150:
151: public function toBitwiseNotType(): Type
152: {
153: return new IntegerType();
154: }
155:
156: public function toAbsoluteNumber(): Type
157: {
158: return $this;
159: }
160:
161: public function toFloat(): Type
162: {
163: return $this;
164: }
165:
166: public function toInteger(): Type
167: {
168: return new IntegerType();
169: }
170:
171: public function toString(): Type
172: {
173: // a finite float is numeric, INF, -INF and NAN are not
174: return TypeCombinator::union(
175: new IntersectionType([
176: new StringType(),
177: new AccessoryNumericStringType(),
178: new AccessoryUppercaseStringType(),
179: ]),
180: new ConstantStringType('INF'),
181: new ConstantStringType('-INF'),
182: new ConstantStringType('NAN'),
183: );
184: }
185:
186: public function toArray(): Type
187: {
188: return new ConstantArrayType(
189: [new ConstantIntegerType(0)],
190: [$this],
191: [1],
192: isList: TrinaryLogic::createYes(),
193: );
194: }
195:
196: public function toArrayKey(): Type
197: {
198: return new IntegerType();
199: }
200:
201: public function toCoercedArgumentType(bool $strictTypes): Type
202: {
203: if (!$strictTypes) {
204: return TypeCombinator::union($this->toInteger(), $this, $this->toString(), $this->toBoolean());
205: }
206:
207: return $this;
208: }
209:
210: public function isOffsetAccessLegal(): TrinaryLogic
211: {
212: return TrinaryLogic::createYes();
213: }
214:
215: public function isNull(): TrinaryLogic
216: {
217: return TrinaryLogic::createNo();
218: }
219:
220: public function isConstantValue(): TrinaryLogic
221: {
222: return TrinaryLogic::createNo();
223: }
224:
225: public function isConstantScalarValue(): TrinaryLogic
226: {
227: return TrinaryLogic::createNo();
228: }
229:
230: public function getConstantScalarTypes(): array
231: {
232: return [];
233: }
234:
235: public function getConstantScalarValues(): array
236: {
237: return [];
238: }
239:
240: public function isTrue(): TrinaryLogic
241: {
242: return TrinaryLogic::createNo();
243: }
244:
245: public function isFalse(): TrinaryLogic
246: {
247: return TrinaryLogic::createNo();
248: }
249:
250: public function isBoolean(): TrinaryLogic
251: {
252: return TrinaryLogic::createNo();
253: }
254:
255: public function isFloat(): TrinaryLogic
256: {
257: return TrinaryLogic::createYes();
258: }
259:
260: public function isInteger(): TrinaryLogic
261: {
262: return TrinaryLogic::createNo();
263: }
264:
265: public function isString(): TrinaryLogic
266: {
267: return TrinaryLogic::createNo();
268: }
269:
270: public function isNumericString(): TrinaryLogic
271: {
272: return TrinaryLogic::createNo();
273: }
274:
275: public function isDecimalIntegerString(): TrinaryLogic
276: {
277: return TrinaryLogic::createNo();
278: }
279:
280: public function isNonEmptyString(): TrinaryLogic
281: {
282: return TrinaryLogic::createNo();
283: }
284:
285: public function isNonFalsyString(): TrinaryLogic
286: {
287: return TrinaryLogic::createNo();
288: }
289:
290: public function isLiteralString(): TrinaryLogic
291: {
292: return TrinaryLogic::createNo();
293: }
294:
295: public function isLowercaseString(): TrinaryLogic
296: {
297: return TrinaryLogic::createNo();
298: }
299:
300: public function isClassString(): TrinaryLogic
301: {
302: return TrinaryLogic::createNo();
303: }
304:
305: public function isUppercaseString(): TrinaryLogic
306: {
307: return TrinaryLogic::createNo();
308: }
309:
310: public function getClassStringObjectType(): Type
311: {
312: return new ErrorType();
313: }
314:
315: public function getObjectTypeOrClassStringObjectType(): Type
316: {
317: return new ErrorType();
318: }
319:
320: public function isVoid(): TrinaryLogic
321: {
322: return TrinaryLogic::createNo();
323: }
324:
325: public function isScalar(): TrinaryLogic
326: {
327: return TrinaryLogic::createYes();
328: }
329:
330: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
331: {
332: return new BooleanType();
333: }
334:
335: public function traverse(callable $cb): Type
336: {
337: return $this;
338: }
339:
340: public function traverseSimultaneously(Type $right, callable $cb): Type
341: {
342: return $this;
343: }
344:
345: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
346: {
347: return $this->toValueSet()->arithmeticPower($exponent->toValueSet(), $phpVersion);
348: }
349:
350: public function toPhpDocNode(): TypeNode
351: {
352: return new IdentifierTypeNode('float');
353: }
354:
355: public function toNativePhpTypeNode(NativeTypePosition $position)
356: {
357: return $position->isScalarAllowed() ? new Identifier('float') : null;
358: }
359:
360: public function getFiniteTypes(): array
361: {
362: return [];
363: }
364:
365: public function hasTemplateOrLateResolvableType(): bool
366: {
367: return false;
368: }
369:
370: public function toValueSet(): ValueSet
371: {
372: return ValueSet::createNumbers($this->toNumberSet());
373: }
374:
375: }
376: