1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type; |
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\Constant\ConstantArrayType; |
10: | use PHPStan\Type\Constant\ConstantBooleanType; |
11: | use PHPStan\Type\Constant\ConstantFloatType; |
12: | use PHPStan\Type\Constant\ConstantIntegerType; |
13: | use PHPStan\Type\Constant\ConstantStringType; |
14: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
15: | use PHPStan\Type\Traits\NonCallableTypeTrait; |
16: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
17: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
18: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
19: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
20: | use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait; |
21: | use PHPStan\Type\Traits\UndecidedBooleanTypeTrait; |
22: | use PHPStan\Type\Traits\UndecidedComparisonTypeTrait; |
23: | |
24: | |
25: | class BooleanType implements Type |
26: | { |
27: | |
28: | use JustNullableTypeTrait; |
29: | use NonArrayTypeTrait; |
30: | use NonCallableTypeTrait; |
31: | use NonIterableTypeTrait; |
32: | use NonObjectTypeTrait; |
33: | use UndecidedBooleanTypeTrait; |
34: | use UndecidedComparisonTypeTrait; |
35: | use NonGenericTypeTrait; |
36: | use NonOffsetAccessibleTypeTrait; |
37: | use NonGeneralizableTypeTrait; |
38: | |
39: | |
40: | public function __construct() |
41: | { |
42: | } |
43: | |
44: | public function getConstantStrings(): array |
45: | { |
46: | return []; |
47: | } |
48: | |
49: | public function getConstantScalarTypes(): array |
50: | { |
51: | return [new ConstantBooleanType(true), new ConstantBooleanType(false)]; |
52: | } |
53: | |
54: | public function getConstantScalarValues(): array |
55: | { |
56: | return [true, false]; |
57: | } |
58: | |
59: | public function describe(VerbosityLevel $level): string |
60: | { |
61: | return 'bool'; |
62: | } |
63: | |
64: | public function toNumber(): Type |
65: | { |
66: | return $this->toInteger(); |
67: | } |
68: | |
69: | public function toAbsoluteNumber(): Type |
70: | { |
71: | return $this->toNumber()->toAbsoluteNumber(); |
72: | } |
73: | |
74: | public function toString(): Type |
75: | { |
76: | return TypeCombinator::union( |
77: | new ConstantStringType(''), |
78: | new ConstantStringType('1'), |
79: | ); |
80: | } |
81: | |
82: | public function toInteger(): Type |
83: | { |
84: | return TypeCombinator::union( |
85: | new ConstantIntegerType(0), |
86: | new ConstantIntegerType(1), |
87: | ); |
88: | } |
89: | |
90: | public function toFloat(): Type |
91: | { |
92: | return TypeCombinator::union( |
93: | new ConstantFloatType(0.0), |
94: | new ConstantFloatType(1.0), |
95: | ); |
96: | } |
97: | |
98: | public function toArray(): Type |
99: | { |
100: | return new ConstantArrayType( |
101: | [new ConstantIntegerType(0)], |
102: | [$this], |
103: | [1], |
104: | [], |
105: | TrinaryLogic::createYes(), |
106: | ); |
107: | } |
108: | |
109: | public function toArrayKey(): Type |
110: | { |
111: | return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]); |
112: | } |
113: | |
114: | public function toCoercedArgumentType(bool $strictTypes): Type |
115: | { |
116: | if (!$strictTypes) { |
117: | return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this->toString(), $this); |
118: | } |
119: | |
120: | return $this; |
121: | } |
122: | |
123: | public function isOffsetAccessLegal(): TrinaryLogic |
124: | { |
125: | return TrinaryLogic::createYes(); |
126: | } |
127: | |
128: | public function isNull(): TrinaryLogic |
129: | { |
130: | return TrinaryLogic::createNo(); |
131: | } |
132: | |
133: | public function isTrue(): TrinaryLogic |
134: | { |
135: | return TrinaryLogic::createMaybe(); |
136: | } |
137: | |
138: | public function isFalse(): TrinaryLogic |
139: | { |
140: | return TrinaryLogic::createMaybe(); |
141: | } |
142: | |
143: | public function isBoolean(): TrinaryLogic |
144: | { |
145: | return TrinaryLogic::createYes(); |
146: | } |
147: | |
148: | public function isScalar(): TrinaryLogic |
149: | { |
150: | return TrinaryLogic::createYes(); |
151: | } |
152: | |
153: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
154: | { |
155: | return new BooleanType(); |
156: | } |
157: | |
158: | public function tryRemove(Type $typeToRemove): ?Type |
159: | { |
160: | if ($typeToRemove instanceof ConstantBooleanType) { |
161: | return new ConstantBooleanType(!$typeToRemove->getValue()); |
162: | } |
163: | |
164: | return null; |
165: | } |
166: | |
167: | public function getFiniteTypes(): array |
168: | { |
169: | return [ |
170: | new ConstantBooleanType(true), |
171: | new ConstantBooleanType(false), |
172: | ]; |
173: | } |
174: | |
175: | public function exponentiate(Type $exponent): Type |
176: | { |
177: | return ExponentiateHelper::exponentiate($this, $exponent); |
178: | } |
179: | |
180: | public function toPhpDocNode(): TypeNode |
181: | { |
182: | return new IdentifierTypeNode('bool'); |
183: | } |
184: | |
185: | public function toTrinaryLogic(): TrinaryLogic |
186: | { |
187: | if ($this->isTrue()->yes()) { |
188: | return TrinaryLogic::createYes(); |
189: | } |
190: | if ($this->isFalse()->yes()) { |
191: | return TrinaryLogic::createNo(); |
192: | } |
193: | |
194: | return TrinaryLogic::createMaybe(); |
195: | } |
196: | |
197: | } |
198: | |