1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type\Accessory; |
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\AcceptsResult; |
10: | use PHPStan\Type\BenevolentUnionType; |
11: | use PHPStan\Type\BooleanType; |
12: | use PHPStan\Type\CompoundType; |
13: | use PHPStan\Type\Constant\ConstantArrayType; |
14: | use PHPStan\Type\Constant\ConstantIntegerType; |
15: | use PHPStan\Type\ErrorType; |
16: | use PHPStan\Type\FloatType; |
17: | use PHPStan\Type\GeneralizePrecision; |
18: | use PHPStan\Type\IntegerType; |
19: | use PHPStan\Type\IntersectionType; |
20: | use PHPStan\Type\IsSuperTypeOfResult; |
21: | use PHPStan\Type\ObjectWithoutClassType; |
22: | use PHPStan\Type\StringType; |
23: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
24: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
25: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
26: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
27: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
28: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
29: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
30: | use PHPStan\Type\Type; |
31: | use PHPStan\Type\UnionType; |
32: | use PHPStan\Type\VerbosityLevel; |
33: | |
34: | class AccessoryLowercaseStringType implements CompoundType, AccessoryType |
35: | { |
36: | |
37: | use MaybeCallableTypeTrait; |
38: | use NonArrayTypeTrait; |
39: | use NonObjectTypeTrait; |
40: | use NonIterableTypeTrait; |
41: | use UndecidedComparisonCompoundTypeTrait; |
42: | use NonGenericTypeTrait; |
43: | use NonRemoveableTypeTrait; |
44: | |
45: | |
46: | public function __construct() |
47: | { |
48: | } |
49: | |
50: | public function getReferencedClasses(): array |
51: | { |
52: | return []; |
53: | } |
54: | |
55: | public function getObjectClassNames(): array |
56: | { |
57: | return []; |
58: | } |
59: | |
60: | public function getObjectClassReflections(): array |
61: | { |
62: | return []; |
63: | } |
64: | |
65: | public function getConstantStrings(): array |
66: | { |
67: | return []; |
68: | } |
69: | |
70: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
71: | { |
72: | if ($type instanceof CompoundType) { |
73: | return $type->isAcceptedBy($this, $strictTypes); |
74: | } |
75: | |
76: | return new AcceptsResult($type->isLowercaseString(), []); |
77: | } |
78: | |
79: | public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
80: | { |
81: | if ($type instanceof CompoundType) { |
82: | return $type->isSubTypeOf($this); |
83: | } |
84: | |
85: | if ($this->equals($type)) { |
86: | return IsSuperTypeOfResult::createYes(); |
87: | } |
88: | |
89: | return new IsSuperTypeOfResult($type->isLowercaseString(), []); |
90: | } |
91: | |
92: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
93: | { |
94: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
95: | return $otherType->isSuperTypeOf($this); |
96: | } |
97: | |
98: | return (new IsSuperTypeOfResult($otherType->isLowercaseString(), [])) |
99: | ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe()); |
100: | } |
101: | |
102: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
103: | { |
104: | return $this->isSubTypeOf($acceptingType)->toAcceptsResult(); |
105: | } |
106: | |
107: | public function equals(Type $type): bool |
108: | { |
109: | return $type instanceof self; |
110: | } |
111: | |
112: | public function describe(VerbosityLevel $level): string |
113: | { |
114: | return 'lowercase-string'; |
115: | } |
116: | |
117: | public function isOffsetAccessible(): TrinaryLogic |
118: | { |
119: | return TrinaryLogic::createYes(); |
120: | } |
121: | |
122: | public function isOffsetAccessLegal(): TrinaryLogic |
123: | { |
124: | return TrinaryLogic::createYes(); |
125: | } |
126: | |
127: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
128: | { |
129: | return $offsetType->isInteger()->and(TrinaryLogic::createMaybe()); |
130: | } |
131: | |
132: | public function getOffsetValueType(Type $offsetType): Type |
133: | { |
134: | if ($this->hasOffsetValueType($offsetType)->no()) { |
135: | return new ErrorType(); |
136: | } |
137: | |
138: | return new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]); |
139: | } |
140: | |
141: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
142: | { |
143: | $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues); |
144: | |
145: | if ($stringOffset instanceof ErrorType) { |
146: | return $stringOffset; |
147: | } |
148: | |
149: | if ($valueType->isLowercaseString()->yes()) { |
150: | return $this; |
151: | } |
152: | |
153: | return new StringType(); |
154: | } |
155: | |
156: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
157: | { |
158: | return $this; |
159: | } |
160: | |
161: | public function unsetOffset(Type $offsetType): Type |
162: | { |
163: | return new ErrorType(); |
164: | } |
165: | |
166: | public function toNumber(): Type |
167: | { |
168: | return new ErrorType(); |
169: | } |
170: | |
171: | public function toAbsoluteNumber(): Type |
172: | { |
173: | return new ErrorType(); |
174: | } |
175: | |
176: | public function toInteger(): Type |
177: | { |
178: | return new IntegerType(); |
179: | } |
180: | |
181: | public function toFloat(): Type |
182: | { |
183: | return new FloatType(); |
184: | } |
185: | |
186: | public function toString(): Type |
187: | { |
188: | return $this; |
189: | } |
190: | |
191: | public function toBoolean(): BooleanType |
192: | { |
193: | return new BooleanType(); |
194: | } |
195: | |
196: | public function toArray(): Type |
197: | { |
198: | return new ConstantArrayType( |
199: | [new ConstantIntegerType(0)], |
200: | [$this], |
201: | [1], |
202: | [], |
203: | TrinaryLogic::createYes(), |
204: | ); |
205: | } |
206: | |
207: | public function toArrayKey(): Type |
208: | { |
209: | return $this; |
210: | } |
211: | |
212: | public function isNull(): TrinaryLogic |
213: | { |
214: | return TrinaryLogic::createNo(); |
215: | } |
216: | |
217: | public function isConstantValue(): TrinaryLogic |
218: | { |
219: | return TrinaryLogic::createMaybe(); |
220: | } |
221: | |
222: | public function isConstantScalarValue(): TrinaryLogic |
223: | { |
224: | return TrinaryLogic::createMaybe(); |
225: | } |
226: | |
227: | public function getConstantScalarTypes(): array |
228: | { |
229: | return []; |
230: | } |
231: | |
232: | public function getConstantScalarValues(): array |
233: | { |
234: | return []; |
235: | } |
236: | |
237: | public function isTrue(): TrinaryLogic |
238: | { |
239: | return TrinaryLogic::createNo(); |
240: | } |
241: | |
242: | public function isFalse(): TrinaryLogic |
243: | { |
244: | return TrinaryLogic::createNo(); |
245: | } |
246: | |
247: | public function isBoolean(): TrinaryLogic |
248: | { |
249: | return TrinaryLogic::createNo(); |
250: | } |
251: | |
252: | public function isFloat(): TrinaryLogic |
253: | { |
254: | return TrinaryLogic::createNo(); |
255: | } |
256: | |
257: | public function isInteger(): TrinaryLogic |
258: | { |
259: | return TrinaryLogic::createNo(); |
260: | } |
261: | |
262: | public function isString(): TrinaryLogic |
263: | { |
264: | return TrinaryLogic::createYes(); |
265: | } |
266: | |
267: | public function isNumericString(): TrinaryLogic |
268: | { |
269: | return TrinaryLogic::createMaybe(); |
270: | } |
271: | |
272: | public function isNonEmptyString(): TrinaryLogic |
273: | { |
274: | return TrinaryLogic::createMaybe(); |
275: | } |
276: | |
277: | public function isNonFalsyString(): TrinaryLogic |
278: | { |
279: | return TrinaryLogic::createMaybe(); |
280: | } |
281: | |
282: | public function isLiteralString(): TrinaryLogic |
283: | { |
284: | return TrinaryLogic::createMaybe(); |
285: | } |
286: | |
287: | public function isLowercaseString(): TrinaryLogic |
288: | { |
289: | return TrinaryLogic::createYes(); |
290: | } |
291: | |
292: | public function isClassString(): TrinaryLogic |
293: | { |
294: | return TrinaryLogic::createMaybe(); |
295: | } |
296: | |
297: | public function isUppercaseString(): TrinaryLogic |
298: | { |
299: | return TrinaryLogic::createMaybe(); |
300: | } |
301: | |
302: | public function getClassStringObjectType(): Type |
303: | { |
304: | return new ObjectWithoutClassType(); |
305: | } |
306: | |
307: | public function getObjectTypeOrClassStringObjectType(): Type |
308: | { |
309: | return new ObjectWithoutClassType(); |
310: | } |
311: | |
312: | public function hasMethod(string $methodName): TrinaryLogic |
313: | { |
314: | return TrinaryLogic::createMaybe(); |
315: | } |
316: | |
317: | public function isVoid(): TrinaryLogic |
318: | { |
319: | return TrinaryLogic::createNo(); |
320: | } |
321: | |
322: | public function isScalar(): TrinaryLogic |
323: | { |
324: | return TrinaryLogic::createYes(); |
325: | } |
326: | |
327: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
328: | { |
329: | return new BooleanType(); |
330: | } |
331: | |
332: | public function traverse(callable $cb): Type |
333: | { |
334: | return $this; |
335: | } |
336: | |
337: | public function traverseSimultaneously(Type $right, callable $cb): Type |
338: | { |
339: | return $this; |
340: | } |
341: | |
342: | public function generalize(GeneralizePrecision $precision): Type |
343: | { |
344: | return new StringType(); |
345: | } |
346: | |
347: | public function exponentiate(Type $exponent): Type |
348: | { |
349: | return new BenevolentUnionType([ |
350: | new FloatType(), |
351: | new IntegerType(), |
352: | ]); |
353: | } |
354: | |
355: | public function getFiniteTypes(): array |
356: | { |
357: | return []; |
358: | } |
359: | |
360: | public function toPhpDocNode(): TypeNode |
361: | { |
362: | return new IdentifierTypeNode('lowercase-string'); |
363: | } |
364: | |
365: | } |
366: | |