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\NonArrayTypeTrait; |
19: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
20: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
21: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
22: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
23: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
24: | use PHPStan\Type\Type; |
25: | use PHPStan\Type\UnionType; |
26: | use PHPStan\Type\VerbosityLevel; |
27: | |
28: | class AccessoryLiteralStringType implements CompoundType, AccessoryType |
29: | { |
30: | |
31: | use MaybeCallableTypeTrait; |
32: | use NonArrayTypeTrait; |
33: | use NonObjectTypeTrait; |
34: | use NonIterableTypeTrait; |
35: | use UndecidedComparisonCompoundTypeTrait; |
36: | use NonGenericTypeTrait; |
37: | use NonRemoveableTypeTrait; |
38: | |
39: | |
40: | public function __construct() |
41: | { |
42: | } |
43: | |
44: | public function getReferencedClasses(): array |
45: | { |
46: | return []; |
47: | } |
48: | |
49: | public function getConstantStrings(): array |
50: | { |
51: | return []; |
52: | } |
53: | |
54: | public function accepts(Type $type, bool $strictTypes): TrinaryLogic |
55: | { |
56: | if ($type instanceof MixedType) { |
57: | return TrinaryLogic::createNo(); |
58: | } |
59: | if ($type instanceof CompoundType) { |
60: | return $type->isAcceptedBy($this, $strictTypes); |
61: | } |
62: | |
63: | return $type->isLiteralString(); |
64: | } |
65: | |
66: | public function isSuperTypeOf(Type $type): TrinaryLogic |
67: | { |
68: | if ($type instanceof CompoundType) { |
69: | return $type->isSubTypeOf($this); |
70: | } |
71: | |
72: | if ($this->equals($type)) { |
73: | return TrinaryLogic::createYes(); |
74: | } |
75: | |
76: | return $type->isLiteralString(); |
77: | } |
78: | |
79: | public function isSubTypeOf(Type $otherType): TrinaryLogic |
80: | { |
81: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
82: | return $otherType->isSuperTypeOf($this); |
83: | } |
84: | |
85: | return $otherType->isLiteralString() |
86: | ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()); |
87: | } |
88: | |
89: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic |
90: | { |
91: | return $this->isSubTypeOf($acceptingType); |
92: | } |
93: | |
94: | public function equals(Type $type): bool |
95: | { |
96: | return $type instanceof self; |
97: | } |
98: | |
99: | public function describe(VerbosityLevel $level): string |
100: | { |
101: | return 'literal-string'; |
102: | } |
103: | |
104: | public function isOffsetAccessible(): TrinaryLogic |
105: | { |
106: | return TrinaryLogic::createYes(); |
107: | } |
108: | |
109: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
110: | { |
111: | return (new IntegerType())->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe()); |
112: | } |
113: | |
114: | public function getOffsetValueType(Type $offsetType): Type |
115: | { |
116: | if ($this->hasOffsetValueType($offsetType)->no()) { |
117: | return new ErrorType(); |
118: | } |
119: | |
120: | return new StringType(); |
121: | } |
122: | |
123: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
124: | { |
125: | return $this; |
126: | } |
127: | |
128: | public function unsetOffset(Type $offsetType): Type |
129: | { |
130: | return new ErrorType(); |
131: | } |
132: | |
133: | public function toNumber(): Type |
134: | { |
135: | return new ErrorType(); |
136: | } |
137: | |
138: | public function toInteger(): Type |
139: | { |
140: | return new IntegerType(); |
141: | } |
142: | |
143: | public function toFloat(): Type |
144: | { |
145: | return new FloatType(); |
146: | } |
147: | |
148: | public function toString(): Type |
149: | { |
150: | return $this; |
151: | } |
152: | |
153: | public function toBoolean(): BooleanType |
154: | { |
155: | return new BooleanType(); |
156: | } |
157: | |
158: | public function toArray(): Type |
159: | { |
160: | return new ConstantArrayType( |
161: | [new ConstantIntegerType(0)], |
162: | [$this], |
163: | [1], |
164: | [], |
165: | true, |
166: | ); |
167: | } |
168: | |
169: | public function toArrayKey(): Type |
170: | { |
171: | return $this; |
172: | } |
173: | |
174: | public function isNull(): TrinaryLogic |
175: | { |
176: | return TrinaryLogic::createNo(); |
177: | } |
178: | |
179: | public function isTrue(): TrinaryLogic |
180: | { |
181: | return TrinaryLogic::createNo(); |
182: | } |
183: | |
184: | public function isFalse(): TrinaryLogic |
185: | { |
186: | return TrinaryLogic::createNo(); |
187: | } |
188: | |
189: | public function isBoolean(): TrinaryLogic |
190: | { |
191: | return TrinaryLogic::createNo(); |
192: | } |
193: | |
194: | public function isFloat(): TrinaryLogic |
195: | { |
196: | return TrinaryLogic::createNo(); |
197: | } |
198: | |
199: | public function isInteger(): TrinaryLogic |
200: | { |
201: | return TrinaryLogic::createNo(); |
202: | } |
203: | |
204: | public function isString(): TrinaryLogic |
205: | { |
206: | return TrinaryLogic::createYes(); |
207: | } |
208: | |
209: | public function isNumericString(): TrinaryLogic |
210: | { |
211: | return TrinaryLogic::createMaybe(); |
212: | } |
213: | |
214: | public function isNonEmptyString(): TrinaryLogic |
215: | { |
216: | return TrinaryLogic::createMaybe(); |
217: | } |
218: | |
219: | public function isNonFalsyString(): TrinaryLogic |
220: | { |
221: | return TrinaryLogic::createMaybe(); |
222: | } |
223: | |
224: | public function isLiteralString(): TrinaryLogic |
225: | { |
226: | return TrinaryLogic::createYes(); |
227: | } |
228: | |
229: | public function isClassStringType(): TrinaryLogic |
230: | { |
231: | return TrinaryLogic::createMaybe(); |
232: | } |
233: | |
234: | public function isVoid(): TrinaryLogic |
235: | { |
236: | return TrinaryLogic::createNo(); |
237: | } |
238: | |
239: | public function isScalar(): TrinaryLogic |
240: | { |
241: | return TrinaryLogic::createYes(); |
242: | } |
243: | |
244: | public function traverse(callable $cb): Type |
245: | { |
246: | return $this; |
247: | } |
248: | |
249: | public function generalize(GeneralizePrecision $precision): Type |
250: | { |
251: | return new StringType(); |
252: | } |
253: | |
254: | public static function __set_state(array $properties): Type |
255: | { |
256: | return new self(); |
257: | } |
258: | |
259: | } |
260: | |