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: /** A subclass may hold numbers the set does not describe, so only the subclasses that override this give theirs. */
152: public function toExactNumberSet(): ?NumberSet
153: {
154: return static::class === self::class ? $this->toNumberSet() : null;
155: }
156:
157: public function toBitwiseNotType(): Type
158: {
159: return new IntegerType();
160: }
161:
162: public function toAbsoluteNumber(): Type
163: {
164: return $this;
165: }
166:
167: public function toFloat(): Type
168: {
169: return $this;
170: }
171:
172: public function toInteger(): Type
173: {
174: return new IntegerType();
175: }
176:
177: public function toString(): Type
178: {
179: // a finite float is numeric, INF, -INF and NAN are not
180: return TypeCombinator::union(
181: new IntersectionType([
182: new StringType(),
183: new AccessoryNumericStringType(),
184: new AccessoryUppercaseStringType(),
185: ]),
186: new ConstantStringType('INF'),
187: new ConstantStringType('-INF'),
188: new ConstantStringType('NAN'),
189: );
190: }
191:
192: public function toArray(): Type
193: {
194: return new ConstantArrayType(
195: [new ConstantIntegerType(0)],
196: [$this],
197: [1],
198: isList: TrinaryLogic::createYes(),
199: );
200: }
201:
202: public function toArrayKey(): Type
203: {
204: return new IntegerType();
205: }
206:
207: public function toCoercedArgumentType(bool $strictTypes): Type
208: {
209: if (!$strictTypes) {
210: return TypeCombinator::union($this->toInteger(), $this, $this->toString(), $this->toBoolean());
211: }
212:
213: return $this;
214: }
215:
216: public function isOffsetAccessLegal(): TrinaryLogic
217: {
218: return TrinaryLogic::createYes();
219: }
220:
221: public function getKindMask(): int
222: {
223: return TypeKind::FLOAT;
224: }
225:
226: public function isNull(): TrinaryLogic
227: {
228: return TrinaryLogic::createNo();
229: }
230:
231: public function isConstantValue(): TrinaryLogic
232: {
233: return TrinaryLogic::createNo();
234: }
235:
236: public function isConstantScalarValue(): TrinaryLogic
237: {
238: return TrinaryLogic::createNo();
239: }
240:
241: public function getConstantScalarTypes(): array
242: {
243: return [];
244: }
245:
246: public function getConstantScalarValues(): array
247: {
248: return [];
249: }
250:
251: public function isTrue(): TrinaryLogic
252: {
253: return TrinaryLogic::createNo();
254: }
255:
256: public function isFalse(): TrinaryLogic
257: {
258: return TrinaryLogic::createNo();
259: }
260:
261: public function isBoolean(): TrinaryLogic
262: {
263: return TrinaryLogic::createNo();
264: }
265:
266: public function isFloat(): TrinaryLogic
267: {
268: return TrinaryLogic::createYes();
269: }
270:
271: public function isInteger(): TrinaryLogic
272: {
273: return TrinaryLogic::createNo();
274: }
275:
276: public function isString(): TrinaryLogic
277: {
278: return TrinaryLogic::createNo();
279: }
280:
281: public function isNumericString(): TrinaryLogic
282: {
283: return TrinaryLogic::createNo();
284: }
285:
286: public function isDecimalIntegerString(): TrinaryLogic
287: {
288: return TrinaryLogic::createNo();
289: }
290:
291: public function isNonEmptyString(): TrinaryLogic
292: {
293: return TrinaryLogic::createNo();
294: }
295:
296: public function isNonFalsyString(): TrinaryLogic
297: {
298: return TrinaryLogic::createNo();
299: }
300:
301: public function isLiteralString(): TrinaryLogic
302: {
303: return TrinaryLogic::createNo();
304: }
305:
306: public function getStringRefinement(): ?int
307: {
308: return null;
309: }
310:
311: public function isLowercaseString(): TrinaryLogic
312: {
313: return TrinaryLogic::createNo();
314: }
315:
316: public function isClassString(): TrinaryLogic
317: {
318: return TrinaryLogic::createNo();
319: }
320:
321: public function isUppercaseString(): TrinaryLogic
322: {
323: return TrinaryLogic::createNo();
324: }
325:
326: public function getClassStringObjectType(): Type
327: {
328: return new ErrorType();
329: }
330:
331: public function getObjectTypeOrClassStringObjectType(): Type
332: {
333: return new ErrorType();
334: }
335:
336: public function isVoid(): TrinaryLogic
337: {
338: return TrinaryLogic::createNo();
339: }
340:
341: public function isScalar(): TrinaryLogic
342: {
343: return TrinaryLogic::createYes();
344: }
345:
346: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
347: {
348: return new BooleanType();
349: }
350:
351: public function traverse(callable $cb): Type
352: {
353: return $this;
354: }
355:
356: public function traverseSimultaneously(Type $right, callable $cb): Type
357: {
358: return $this;
359: }
360:
361: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
362: {
363: return $this->toValueSet()->arithmeticPower($exponent->toValueSet(), $phpVersion);
364: }
365:
366: public function toPhpDocNode(): TypeNode
367: {
368: return new IdentifierTypeNode('float');
369: }
370:
371: public function toNativePhpTypeNode(NativeTypePosition $position)
372: {
373: return $position->isScalarAllowed() ? new Identifier('float') : null;
374: }
375:
376: public function getFiniteTypes(): array
377: {
378: return [];
379: }
380:
381: public function hasTemplateOrLateResolvableType(): bool
382: {
383: return false;
384: }
385:
386: public function hasUnresolvedTemplateArgumentType(): bool
387: {
388: return false;
389: }
390:
391: public function toValueSet(): ValueSet
392: {
393: return ValueSet::createNumbers($this->toNumberSet());
394: }
395:
396: public function tryUnion(Type $otherType): ?Type
397: {
398: return null;
399: }
400:
401: public function tryIntersect(Type $otherType): ?Type
402: {
403: return null;
404: }
405:
406: }
407: