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: /** @api */
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: /** @api */
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 toBitwiseNotType(): Type
70: {
71: return new ErrorType();
72: }
73:
74: public function toAbsoluteNumber(): Type
75: {
76: return $this->toNumber()->toAbsoluteNumber();
77: }
78:
79: public function toString(): Type
80: {
81: return new UnionType([
82: new ConstantStringType(''),
83: new ConstantStringType('1'),
84: ]);
85: }
86:
87: public function toInteger(): Type
88: {
89: return new UnionType([
90: new ConstantIntegerType(0),
91: new ConstantIntegerType(1),
92: ]);
93: }
94:
95: public function toFloat(): Type
96: {
97: return new UnionType([
98: new ConstantFloatType(0.0),
99: new ConstantFloatType(1.0),
100: ]);
101: }
102:
103: public function toArray(): Type
104: {
105: return new ConstantArrayType(
106: [new ConstantIntegerType(0)],
107: [$this],
108: [1],
109: isList: TrinaryLogic::createYes(),
110: );
111: }
112:
113: public function toArrayKey(): Type
114: {
115: return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
116: }
117:
118: public function toCoercedArgumentType(bool $strictTypes): Type
119: {
120: if (!$strictTypes) {
121: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this->toString(), $this);
122: }
123:
124: return $this;
125: }
126:
127: public function isOffsetAccessLegal(): TrinaryLogic
128: {
129: return TrinaryLogic::createYes();
130: }
131:
132: public function isNull(): TrinaryLogic
133: {
134: return TrinaryLogic::createNo();
135: }
136:
137: public function isTrue(): TrinaryLogic
138: {
139: return TrinaryLogic::createMaybe();
140: }
141:
142: public function isFalse(): TrinaryLogic
143: {
144: return TrinaryLogic::createMaybe();
145: }
146:
147: public function isBoolean(): TrinaryLogic
148: {
149: return TrinaryLogic::createYes();
150: }
151:
152: public function isScalar(): TrinaryLogic
153: {
154: return TrinaryLogic::createYes();
155: }
156:
157: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
158: {
159: return new BooleanType();
160: }
161:
162: public function tryRemove(Type $typeToRemove): ?Type
163: {
164: if ($typeToRemove instanceof ConstantBooleanType) {
165: return new ConstantBooleanType(!$typeToRemove->getValue());
166: }
167:
168: return null;
169: }
170:
171: public function getFiniteTypes(): array
172: {
173: return [
174: new ConstantBooleanType(true),
175: new ConstantBooleanType(false),
176: ];
177: }
178:
179: public function exponentiate(Type $exponent): Type
180: {
181: return ExponentiateHelper::exponentiate($this, $exponent);
182: }
183:
184: public function toPhpDocNode(): TypeNode
185: {
186: return new IdentifierTypeNode('bool');
187: }
188:
189: public function toTrinaryLogic(): TrinaryLogic
190: {
191: if ($this->isTrue()->yes()) {
192: return TrinaryLogic::createYes();
193: }
194: if ($this->isFalse()->yes()) {
195: return TrinaryLogic::createNo();
196: }
197:
198: return TrinaryLogic::createMaybe();
199: }
200:
201: public function hasTemplateOrLateResolvableType(): bool
202: {
203: return false;
204: }
205:
206: }
207: