| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Constant; |
| 4: | |
| 5: | use PHPStan\Php\PhpVersion; |
| 6: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 7: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 8: | use PHPStan\TrinaryLogic; |
| 9: | use PHPStan\Type\BooleanType; |
| 10: | use PHPStan\Type\ConstantScalarType; |
| 11: | use PHPStan\Type\GeneralizePrecision; |
| 12: | use PHPStan\Type\MixedType; |
| 13: | use PHPStan\Type\NeverType; |
| 14: | use PHPStan\Type\StaticTypeFactory; |
| 15: | use PHPStan\Type\Traits\ConstantScalarTypeTrait; |
| 16: | use PHPStan\Type\Type; |
| 17: | use PHPStan\Type\VerbosityLevel; |
| 18: | |
| 19: | |
| 20: | class ConstantBooleanType extends BooleanType implements ConstantScalarType |
| 21: | { |
| 22: | |
| 23: | use ConstantScalarTypeTrait { |
| 24: | looseCompare as private scalarLooseCompare; |
| 25: | } |
| 26: | |
| 27: | |
| 28: | public function __construct(private bool $value) |
| 29: | { |
| 30: | parent::__construct(); |
| 31: | } |
| 32: | |
| 33: | public function getValue(): bool |
| 34: | { |
| 35: | return $this->value; |
| 36: | } |
| 37: | |
| 38: | public function describe(VerbosityLevel $level): string |
| 39: | { |
| 40: | return $this->value ? 'true' : 'false'; |
| 41: | } |
| 42: | |
| 43: | public function getSmallerType(PhpVersion $phpVersion): Type |
| 44: | { |
| 45: | if ($this->value) { |
| 46: | return StaticTypeFactory::falsey(); |
| 47: | } |
| 48: | return new NeverType(); |
| 49: | } |
| 50: | |
| 51: | public function getSmallerOrEqualType(PhpVersion $phpVersion): Type |
| 52: | { |
| 53: | if ($this->value) { |
| 54: | return new MixedType(); |
| 55: | } |
| 56: | return StaticTypeFactory::falsey(); |
| 57: | } |
| 58: | |
| 59: | public function getGreaterType(PhpVersion $phpVersion): Type |
| 60: | { |
| 61: | if ($this->value) { |
| 62: | return new NeverType(); |
| 63: | } |
| 64: | return StaticTypeFactory::truthy(); |
| 65: | } |
| 66: | |
| 67: | public function getGreaterOrEqualType(PhpVersion $phpVersion): Type |
| 68: | { |
| 69: | if ($this->value) { |
| 70: | return StaticTypeFactory::truthy(); |
| 71: | } |
| 72: | return new MixedType(); |
| 73: | } |
| 74: | |
| 75: | public function toBoolean(): BooleanType |
| 76: | { |
| 77: | return $this; |
| 78: | } |
| 79: | |
| 80: | public function toNumber(): Type |
| 81: | { |
| 82: | return new ConstantIntegerType((int) $this->value); |
| 83: | } |
| 84: | |
| 85: | public function toAbsoluteNumber(): Type |
| 86: | { |
| 87: | return $this->toNumber()->toAbsoluteNumber(); |
| 88: | } |
| 89: | |
| 90: | public function toString(): Type |
| 91: | { |
| 92: | return new ConstantStringType((string) $this->value); |
| 93: | } |
| 94: | |
| 95: | public function toInteger(): Type |
| 96: | { |
| 97: | return new ConstantIntegerType((int) $this->value); |
| 98: | } |
| 99: | |
| 100: | public function toFloat(): Type |
| 101: | { |
| 102: | return new ConstantFloatType((float) $this->value); |
| 103: | } |
| 104: | |
| 105: | public function toArrayKey(): Type |
| 106: | { |
| 107: | return new ConstantIntegerType((int) $this->value); |
| 108: | } |
| 109: | |
| 110: | public function isTrue(): TrinaryLogic |
| 111: | { |
| 112: | return TrinaryLogic::createFromBoolean($this->value === true); |
| 113: | } |
| 114: | |
| 115: | public function isFalse(): TrinaryLogic |
| 116: | { |
| 117: | return TrinaryLogic::createFromBoolean($this->value === false); |
| 118: | } |
| 119: | |
| 120: | public function generalize(GeneralizePrecision $precision): Type |
| 121: | { |
| 122: | return new BooleanType(); |
| 123: | } |
| 124: | |
| 125: | public function toPhpDocNode(): TypeNode |
| 126: | { |
| 127: | return new IdentifierTypeNode($this->value ? 'true' : 'false'); |
| 128: | } |
| 129: | |
| 130: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
| 131: | { |
| 132: | if ($type->isObject()->yes()) { |
| 133: | return $this; |
| 134: | } |
| 135: | |
| 136: | return $this->scalarLooseCompare($type, $phpVersion); |
| 137: | } |
| 138: | |
| 139: | } |
| 140: | |