1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Type\BooleanType;
7: use PHPStan\Type\CompoundType;
8: use PHPStan\Type\Constant\ConstantArrayType;
9: use PHPStan\Type\Constant\ConstantIntegerType;
10: use PHPStan\Type\ErrorType;
11: use PHPStan\Type\FloatType;
12: use PHPStan\Type\GeneralizePrecision;
13: use PHPStan\Type\IntegerType;
14: use PHPStan\Type\IntersectionType;
15: use PHPStan\Type\MixedType;
16: use PHPStan\Type\StringType;
17: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
18: use PHPStan\Type\Traits\NonGenericTypeTrait;
19: use PHPStan\Type\Traits\NonIterableTypeTrait;
20: use PHPStan\Type\Traits\NonObjectTypeTrait;
21: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
22: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
23: use PHPStan\Type\Type;
24: use PHPStan\Type\UnionType;
25: use PHPStan\Type\VerbosityLevel;
26:
27: class AccessoryLiteralStringType implements CompoundType, AccessoryType
28: {
29:
30: use MaybeCallableTypeTrait;
31: use NonObjectTypeTrait;
32: use NonIterableTypeTrait;
33: use UndecidedComparisonCompoundTypeTrait;
34: use NonGenericTypeTrait;
35: use NonRemoveableTypeTrait;
36:
37: /** @api */
38: public function __construct()
39: {
40: }
41:
42: public function getReferencedClasses(): array
43: {
44: return [];
45: }
46:
47: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
48: {
49: if ($type instanceof MixedType) {
50: return TrinaryLogic::createNo();
51: }
52: if ($type instanceof CompoundType) {
53: return $type->isAcceptedBy($this, $strictTypes);
54: }
55:
56: return $type->isLiteralString();
57: }
58:
59: public function isSuperTypeOf(Type $type): TrinaryLogic
60: {
61: if ($type instanceof CompoundType) {
62: return $type->isSubTypeOf($this);
63: }
64:
65: if ($this->equals($type)) {
66: return TrinaryLogic::createYes();
67: }
68:
69: return $type->isLiteralString();
70: }
71:
72: public function isSubTypeOf(Type $otherType): TrinaryLogic
73: {
74: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
75: return $otherType->isSuperTypeOf($this);
76: }
77:
78: return $otherType->isLiteralString()
79: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
80: }
81:
82: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
83: {
84: return $this->isSubTypeOf($acceptingType);
85: }
86:
87: public function equals(Type $type): bool
88: {
89: return $type instanceof self;
90: }
91:
92: public function describe(VerbosityLevel $level): string
93: {
94: return 'literal-string';
95: }
96:
97: public function isOffsetAccessible(): TrinaryLogic
98: {
99: return TrinaryLogic::createYes();
100: }
101:
102: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
103: {
104: return (new IntegerType())->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe());
105: }
106:
107: public function getOffsetValueType(Type $offsetType): Type
108: {
109: if ($this->hasOffsetValueType($offsetType)->no()) {
110: return new ErrorType();
111: }
112:
113: return new StringType();
114: }
115:
116: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
117: {
118: return $this;
119: }
120:
121: public function unsetOffset(Type $offsetType): Type
122: {
123: return new ErrorType();
124: }
125:
126: public function isArray(): TrinaryLogic
127: {
128: return TrinaryLogic::createNo();
129: }
130:
131: public function isOversizedArray(): TrinaryLogic
132: {
133: return TrinaryLogic::createNo();
134: }
135:
136: public function toNumber(): Type
137: {
138: return new ErrorType();
139: }
140:
141: public function toInteger(): Type
142: {
143: return new IntegerType();
144: }
145:
146: public function toFloat(): Type
147: {
148: return new FloatType();
149: }
150:
151: public function toString(): Type
152: {
153: return $this;
154: }
155:
156: public function toBoolean(): BooleanType
157: {
158: return new BooleanType();
159: }
160:
161: public function toArray(): Type
162: {
163: return new ConstantArrayType(
164: [new ConstantIntegerType(0)],
165: [$this],
166: [1],
167: );
168: }
169:
170: public function isString(): TrinaryLogic
171: {
172: return TrinaryLogic::createYes();
173: }
174:
175: public function isNumericString(): TrinaryLogic
176: {
177: return TrinaryLogic::createMaybe();
178: }
179:
180: public function isNonEmptyString(): TrinaryLogic
181: {
182: return TrinaryLogic::createMaybe();
183: }
184:
185: public function isNonFalsyString(): TrinaryLogic
186: {
187: return TrinaryLogic::createMaybe();
188: }
189:
190: public function isLiteralString(): TrinaryLogic
191: {
192: return TrinaryLogic::createYes();
193: }
194:
195: public function traverse(callable $cb): Type
196: {
197: return $this;
198: }
199:
200: public function generalize(GeneralizePrecision $precision): Type
201: {
202: return new StringType();
203: }
204:
205: public static function __set_state(array $properties): Type
206: {
207: return new self();
208: }
209:
210: }
211: