| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Constant; |
| 4: | |
| 5: | use PHPStan\Type\BooleanType; |
| 6: | use PHPStan\Type\ConstantScalarType; |
| 7: | use PHPStan\Type\GeneralizePrecision; |
| 8: | use PHPStan\Type\MixedType; |
| 9: | use PHPStan\Type\NeverType; |
| 10: | use PHPStan\Type\StaticTypeFactory; |
| 11: | use PHPStan\Type\Traits\ConstantScalarTypeTrait; |
| 12: | use PHPStan\Type\Type; |
| 13: | use PHPStan\Type\VerbosityLevel; |
| 14: | |
| 15: | |
| 16: | class ConstantBooleanType extends BooleanType implements ConstantScalarType |
| 17: | { |
| 18: | |
| 19: | use ConstantScalarTypeTrait; |
| 20: | |
| 21: | |
| 22: | public function __construct(private bool $value) |
| 23: | { |
| 24: | parent::__construct(); |
| 25: | } |
| 26: | |
| 27: | public function getValue(): bool |
| 28: | { |
| 29: | return $this->value; |
| 30: | } |
| 31: | |
| 32: | public function describe(VerbosityLevel $level): string |
| 33: | { |
| 34: | return $this->value ? 'true' : 'false'; |
| 35: | } |
| 36: | |
| 37: | public function getSmallerType(): Type |
| 38: | { |
| 39: | if ($this->value) { |
| 40: | return StaticTypeFactory::falsey(); |
| 41: | } |
| 42: | return new NeverType(); |
| 43: | } |
| 44: | |
| 45: | public function getSmallerOrEqualType(): Type |
| 46: | { |
| 47: | if ($this->value) { |
| 48: | return new MixedType(); |
| 49: | } |
| 50: | return StaticTypeFactory::falsey(); |
| 51: | } |
| 52: | |
| 53: | public function getGreaterType(): Type |
| 54: | { |
| 55: | if ($this->value) { |
| 56: | return new NeverType(); |
| 57: | } |
| 58: | return StaticTypeFactory::truthy(); |
| 59: | } |
| 60: | |
| 61: | public function getGreaterOrEqualType(): Type |
| 62: | { |
| 63: | if ($this->value) { |
| 64: | return StaticTypeFactory::truthy(); |
| 65: | } |
| 66: | return new MixedType(); |
| 67: | } |
| 68: | |
| 69: | public function toBoolean(): BooleanType |
| 70: | { |
| 71: | return $this; |
| 72: | } |
| 73: | |
| 74: | public function toNumber(): Type |
| 75: | { |
| 76: | return new ConstantIntegerType((int) $this->value); |
| 77: | } |
| 78: | |
| 79: | public function toString(): Type |
| 80: | { |
| 81: | return new ConstantStringType((string) $this->value); |
| 82: | } |
| 83: | |
| 84: | public function toInteger(): Type |
| 85: | { |
| 86: | return new ConstantIntegerType((int) $this->value); |
| 87: | } |
| 88: | |
| 89: | public function toFloat(): Type |
| 90: | { |
| 91: | return new ConstantFloatType((float) $this->value); |
| 92: | } |
| 93: | |
| 94: | public function generalize(GeneralizePrecision $precision): Type |
| 95: | { |
| 96: | return new BooleanType(); |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public static function __set_state(array $properties): Type |
| 103: | { |
| 104: | return new self($properties['value']); |
| 105: | } |
| 106: | |
| 107: | } |
| 108: | |