1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\TrinaryLogic;
7: use PHPStan\Type\Constant\ConstantArrayType;
8: use PHPStan\Type\Constant\ConstantBooleanType;
9: use PHPStan\Type\Constant\ConstantFloatType;
10: use PHPStan\Type\Constant\ConstantIntegerType;
11: use PHPStan\Type\Constant\ConstantStringType;
12: use PHPStan\Type\Traits\NonArrayTypeTrait;
13: use PHPStan\Type\Traits\NonCallableTypeTrait;
14: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
15: use PHPStan\Type\Traits\NonGenericTypeTrait;
16: use PHPStan\Type\Traits\NonIterableTypeTrait;
17: use PHPStan\Type\Traits\NonObjectTypeTrait;
18: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
19: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
20: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
21:
22: /** @api */
23: class BooleanType implements Type
24: {
25:
26: use JustNullableTypeTrait;
27: use NonArrayTypeTrait;
28: use NonCallableTypeTrait;
29: use NonIterableTypeTrait;
30: use NonObjectTypeTrait;
31: use UndecidedBooleanTypeTrait;
32: use UndecidedComparisonTypeTrait;
33: use NonGenericTypeTrait;
34: use NonOffsetAccessibleTypeTrait;
35: use NonGeneralizableTypeTrait;
36:
37: /** @api */
38: public function __construct()
39: {
40: }
41:
42: public function getConstantStrings(): array
43: {
44: return [];
45: }
46:
47: public function describe(VerbosityLevel $level): string
48: {
49: return 'bool';
50: }
51:
52: public function toNumber(): Type
53: {
54: return $this->toInteger();
55: }
56:
57: public function toString(): Type
58: {
59: return TypeCombinator::union(
60: new ConstantStringType(''),
61: new ConstantStringType('1'),
62: );
63: }
64:
65: public function toInteger(): Type
66: {
67: return TypeCombinator::union(
68: new ConstantIntegerType(0),
69: new ConstantIntegerType(1),
70: );
71: }
72:
73: public function toFloat(): Type
74: {
75: return TypeCombinator::union(
76: new ConstantFloatType(0.0),
77: new ConstantFloatType(1.0),
78: );
79: }
80:
81: public function toArray(): Type
82: {
83: return new ConstantArrayType(
84: [new ConstantIntegerType(0)],
85: [$this],
86: [1],
87: [],
88: true,
89: );
90: }
91:
92: public function toArrayKey(): Type
93: {
94: return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
95: }
96:
97: public function isNull(): TrinaryLogic
98: {
99: return TrinaryLogic::createNo();
100: }
101:
102: public function isTrue(): TrinaryLogic
103: {
104: return TrinaryLogic::createMaybe();
105: }
106:
107: public function isFalse(): TrinaryLogic
108: {
109: return TrinaryLogic::createMaybe();
110: }
111:
112: public function isBoolean(): TrinaryLogic
113: {
114: return TrinaryLogic::createYes();
115: }
116:
117: public function isScalar(): TrinaryLogic
118: {
119: return TrinaryLogic::createYes();
120: }
121:
122: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
123: {
124: return new BooleanType();
125: }
126:
127: public function tryRemove(Type $typeToRemove): ?Type
128: {
129: if ($typeToRemove instanceof ConstantBooleanType) {
130: return new ConstantBooleanType(!$typeToRemove->getValue());
131: }
132:
133: return null;
134: }
135:
136: public function exponentiate(Type $exponent): Type
137: {
138: return ExponentiateHelper::exponentiate($this, $exponent);
139: }
140:
141: /**
142: * @param mixed[] $properties
143: */
144: public static function __set_state(array $properties): Type
145: {
146: return new self();
147: }
148:
149: }
150: