| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Constant; |
| 4: | |
| 5: | use PHPStan\TrinaryLogic; |
| 6: | use PHPStan\Type\CompoundType; |
| 7: | use PHPStan\Type\ConstantScalarType; |
| 8: | use PHPStan\Type\FloatType; |
| 9: | use PHPStan\Type\GeneralizePrecision; |
| 10: | use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait; |
| 11: | use PHPStan\Type\Traits\ConstantScalarTypeTrait; |
| 12: | use PHPStan\Type\Type; |
| 13: | use PHPStan\Type\VerbosityLevel; |
| 14: | use function is_finite; |
| 15: | use function strpos; |
| 16: | |
| 17: | |
| 18: | class ConstantFloatType extends FloatType implements ConstantScalarType |
| 19: | { |
| 20: | |
| 21: | use ConstantScalarTypeTrait; |
| 22: | use ConstantScalarToBooleanTrait; |
| 23: | use ConstantNumericComparisonTypeTrait; |
| 24: | |
| 25: | |
| 26: | public function __construct(private float $value) |
| 27: | { |
| 28: | parent::__construct(); |
| 29: | } |
| 30: | |
| 31: | public function getValue(): float |
| 32: | { |
| 33: | return $this->value; |
| 34: | } |
| 35: | |
| 36: | public function describe(VerbosityLevel $level): string |
| 37: | { |
| 38: | return $level->handle( |
| 39: | static fn (): string => 'float', |
| 40: | function (): string { |
| 41: | $formatted = (string) $this->value; |
| 42: | if (is_finite($this->value) && strpos($formatted, '.') === false) { |
| 43: | $formatted .= '.0'; |
| 44: | } |
| 45: | |
| 46: | return $formatted; |
| 47: | }, |
| 48: | ); |
| 49: | } |
| 50: | |
| 51: | public function isSuperTypeOf(Type $type): TrinaryLogic |
| 52: | { |
| 53: | if ($type instanceof self) { |
| 54: | if (!$this->equals($type)) { |
| 55: | if ($this->describe(VerbosityLevel::value()) === $type->describe(VerbosityLevel::value())) { |
| 56: | return TrinaryLogic::createMaybe(); |
| 57: | } |
| 58: | |
| 59: | return TrinaryLogic::createNo(); |
| 60: | } |
| 61: | |
| 62: | return TrinaryLogic::createYes(); |
| 63: | } |
| 64: | |
| 65: | if ($type instanceof parent) { |
| 66: | return TrinaryLogic::createMaybe(); |
| 67: | } |
| 68: | |
| 69: | if ($type instanceof CompoundType) { |
| 70: | return $type->isSubTypeOf($this); |
| 71: | } |
| 72: | |
| 73: | return TrinaryLogic::createNo(); |
| 74: | } |
| 75: | |
| 76: | public function toString(): Type |
| 77: | { |
| 78: | return new ConstantStringType((string) $this->value); |
| 79: | } |
| 80: | |
| 81: | public function toInteger(): Type |
| 82: | { |
| 83: | return new ConstantIntegerType((int) $this->value); |
| 84: | } |
| 85: | |
| 86: | public function generalize(GeneralizePrecision $precision): Type |
| 87: | { |
| 88: | return new FloatType(); |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | public static function __set_state(array $properties): Type |
| 95: | { |
| 96: | return new self($properties['value']); |
| 97: | } |
| 98: | |
| 99: | } |
| 100: | |