| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Constant; |
| 4: | |
| 5: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode; |
| 6: | use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode; |
| 7: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 8: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 9: | use PHPStan\Type\BooleanType; |
| 10: | use PHPStan\Type\CompoundType; |
| 11: | use PHPStan\Type\ConstantScalarType; |
| 12: | use PHPStan\Type\FloatRangeType; |
| 13: | use PHPStan\Type\FloatType; |
| 14: | use PHPStan\Type\GeneralizePrecision; |
| 15: | use PHPStan\Type\IsSuperTypeOfResult; |
| 16: | use PHPStan\Type\NumberSet; |
| 17: | use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait; |
| 18: | use PHPStan\Type\Traits\ConstantScalarTypeTrait; |
| 19: | use PHPStan\Type\Type; |
| 20: | use PHPStan\Type\UnionType; |
| 21: | use PHPStan\Type\VerbosityLevel; |
| 22: | use function abs; |
| 23: | use function ini_get; |
| 24: | use function ini_set; |
| 25: | use function is_finite; |
| 26: | use function is_nan; |
| 27: | use function str_contains; |
| 28: | |
| 29: | |
| 30: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/ConstantFloatType.cpp')] |
| 31: | class ConstantFloatType extends FloatType implements ConstantScalarType |
| 32: | { |
| 33: | |
| 34: | use ConstantScalarTypeTrait; |
| 35: | use ConstantNumericComparisonTypeTrait; |
| 36: | |
| 37: | |
| 38: | public function __construct(private float $value) |
| 39: | { |
| 40: | parent::__construct(); |
| 41: | } |
| 42: | |
| 43: | public function getValue(): float |
| 44: | { |
| 45: | return $this->value; |
| 46: | } |
| 47: | |
| 48: | public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
| 49: | { |
| 50: | if ($type instanceof self) { |
| 51: | return IsSuperTypeOfResult::createFromBoolean($this->equals($type)); |
| 52: | } |
| 53: | |
| 54: | if ($type instanceof FloatRangeType) { |
| 55: | return $type->isSuperTypeOf($this)->no() ? IsSuperTypeOfResult::createNo() : IsSuperTypeOfResult::createMaybe(); |
| 56: | } |
| 57: | |
| 58: | if ($type instanceof parent) { |
| 59: | return IsSuperTypeOfResult::createMaybe(); |
| 60: | } |
| 61: | |
| 62: | if ($type instanceof CompoundType) { |
| 63: | return $type->isSubTypeOf($this); |
| 64: | } |
| 65: | |
| 66: | return IsSuperTypeOfResult::createNo(); |
| 67: | } |
| 68: | |
| 69: | public function equals(Type $type): bool |
| 70: | { |
| 71: | return $type instanceof self && ($this->value === $type->value || is_nan($this->value) && is_nan($type->value)); |
| 72: | } |
| 73: | |
| 74: | private function castFloatToString(float $value): string |
| 75: | { |
| 76: | $precisionBackup = ini_get('precision'); |
| 77: | ini_set('precision', '-1'); |
| 78: | try { |
| 79: | if (is_nan($value)) { |
| 80: | return 'NAN'; |
| 81: | } |
| 82: | |
| 83: | $valueStr = (string) $value; |
| 84: | if (is_finite($value) && !str_contains($valueStr, '.')) { |
| 85: | $valueStr .= '.0'; |
| 86: | } |
| 87: | |
| 88: | return $valueStr; |
| 89: | } finally { |
| 90: | ini_set('precision', $precisionBackup); |
| 91: | } |
| 92: | } |
| 93: | |
| 94: | public function describe(VerbosityLevel $level): string |
| 95: | { |
| 96: | return $level->handle( |
| 97: | static fn (): string => 'float', |
| 98: | fn (): string => $this->castFloatToString($this->value), |
| 99: | ); |
| 100: | } |
| 101: | |
| 102: | public function toString(): Type |
| 103: | { |
| 104: | if ($this->value === 0.0) { |
| 105: | return new UnionType([ |
| 106: | new ConstantStringType('0'), |
| 107: | new ConstantStringType('-0'), |
| 108: | ]); |
| 109: | } |
| 110: | |
| 111: | if (is_nan($this->value)) { |
| 112: | return new ConstantStringType('NAN'); |
| 113: | } |
| 114: | |
| 115: | return new ConstantStringType((string) $this->value); |
| 116: | } |
| 117: | |
| 118: | public function toBoolean(): BooleanType |
| 119: | { |
| 120: | |
| 121: | return new ConstantBooleanType($this->value !== 0.0); |
| 122: | } |
| 123: | |
| 124: | |
| 125: | private function toIntegerValue(): int |
| 126: | { |
| 127: | return is_finite($this->value) ? (int) $this->value : 0; |
| 128: | } |
| 129: | |
| 130: | public function toNumberSet(): NumberSet |
| 131: | { |
| 132: | if (is_nan($this->value)) { |
| 133: | return new NumberSet([], [], true); |
| 134: | } |
| 135: | |
| 136: | return new NumberSet([], [[$this->value, $this->value]], false); |
| 137: | } |
| 138: | |
| 139: | public function toInteger(): Type |
| 140: | { |
| 141: | return new ConstantIntegerType($this->toIntegerValue()); |
| 142: | } |
| 143: | |
| 144: | public function toBitwiseNotType(): Type |
| 145: | { |
| 146: | return new ConstantIntegerType(~ $this->toIntegerValue()); |
| 147: | } |
| 148: | |
| 149: | public function toAbsoluteNumber(): Type |
| 150: | { |
| 151: | return new self(abs($this->value)); |
| 152: | } |
| 153: | |
| 154: | public function toArrayKey(): Type |
| 155: | { |
| 156: | return new ConstantIntegerType($this->toIntegerValue()); |
| 157: | } |
| 158: | |
| 159: | public function getFiniteTypes(): array |
| 160: | { |
| 161: | if (is_nan($this->value)) { |
| 162: | return []; |
| 163: | } |
| 164: | |
| 165: | return [$this]; |
| 166: | } |
| 167: | |
| 168: | public function generalize(GeneralizePrecision $precision): Type |
| 169: | { |
| 170: | return new FloatType(); |
| 171: | } |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | public function toPhpDocNode(): TypeNode |
| 177: | { |
| 178: | return new ConstTypeNode(new ConstExprFloatNode($this->castFloatToString($this->value))); |
| 179: | } |
| 180: | |
| 181: | } |
| 182: | |