1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Type\Constant\ConstantArrayType;
6: use PHPStan\Type\Constant\ConstantBooleanType;
7: use PHPStan\Type\Constant\ConstantFloatType;
8: use PHPStan\Type\Constant\ConstantIntegerType;
9: use PHPStan\Type\Constant\ConstantStringType;
10: use PHPStan\Type\Traits\NonCallableTypeTrait;
11: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
12: use PHPStan\Type\Traits\NonGenericTypeTrait;
13: use PHPStan\Type\Traits\NonIterableTypeTrait;
14: use PHPStan\Type\Traits\NonObjectTypeTrait;
15: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
16: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
17: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
18:
19: /** @api */
20: class BooleanType implements Type
21: {
22:
23: use JustNullableTypeTrait;
24: use NonCallableTypeTrait;
25: use NonIterableTypeTrait;
26: use NonObjectTypeTrait;
27: use UndecidedBooleanTypeTrait;
28: use UndecidedComparisonTypeTrait;
29: use NonGenericTypeTrait;
30: use NonOffsetAccessibleTypeTrait;
31: use NonGeneralizableTypeTrait;
32:
33: /** @api */
34: public function __construct()
35: {
36: }
37:
38: public function describe(VerbosityLevel $level): string
39: {
40: return 'bool';
41: }
42:
43: public function toNumber(): Type
44: {
45: return $this->toInteger();
46: }
47:
48: public function toString(): Type
49: {
50: return TypeCombinator::union(
51: new ConstantStringType(''),
52: new ConstantStringType('1'),
53: );
54: }
55:
56: public function toInteger(): Type
57: {
58: return TypeCombinator::union(
59: new ConstantIntegerType(0),
60: new ConstantIntegerType(1),
61: );
62: }
63:
64: public function toFloat(): Type
65: {
66: return TypeCombinator::union(
67: new ConstantFloatType(0.0),
68: new ConstantFloatType(1.0),
69: );
70: }
71:
72: public function toArray(): Type
73: {
74: return new ConstantArrayType(
75: [new ConstantIntegerType(0)],
76: [$this],
77: [1],
78: );
79: }
80:
81: public function tryRemove(Type $typeToRemove): ?Type
82: {
83: if ($typeToRemove instanceof ConstantBooleanType) {
84: return new ConstantBooleanType(!$typeToRemove->getValue());
85: }
86:
87: return null;
88: }
89:
90: /**
91: * @param mixed[] $properties
92: */
93: public static function __set_state(array $properties): Type
94: {
95: return new self();
96: }
97:
98: }
99: