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\BooleanType; |
11: | use PHPStan\Type\CompoundType; |
12: | use PHPStan\Type\Constant\ConstantIntegerType; |
13: | use PHPStan\Type\Constant\ConstantStringType; |
14: | use PHPStan\Type\ErrorType; |
15: | use PHPStan\Type\IntegerRangeType; |
16: | use PHPStan\Type\IntersectionType; |
17: | use PHPStan\Type\IsSuperTypeOfResult; |
18: | use PHPStan\Type\MixedType; |
19: | use PHPStan\Type\ObjectWithoutClassType; |
20: | use PHPStan\Type\Traits\MaybeArrayTypeTrait; |
21: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
22: | use PHPStan\Type\Traits\MaybeIterableTypeTrait; |
23: | use PHPStan\Type\Traits\MaybeObjectTypeTrait; |
24: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
25: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
26: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
27: | use PHPStan\Type\Traits\TruthyBooleanTypeTrait; |
28: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
29: | use PHPStan\Type\Type; |
30: | use PHPStan\Type\TypeCombinator; |
31: | use PHPStan\Type\UnionType; |
32: | use PHPStan\Type\VerbosityLevel; |
33: | use function sprintf; |
34: | |
35: | class HasOffsetType implements CompoundType, AccessoryType |
36: | { |
37: | |
38: | use MaybeArrayTypeTrait; |
39: | use MaybeCallableTypeTrait; |
40: | use MaybeIterableTypeTrait; |
41: | use MaybeObjectTypeTrait; |
42: | use TruthyBooleanTypeTrait; |
43: | use NonGenericTypeTrait; |
44: | use UndecidedComparisonCompoundTypeTrait; |
45: | use NonRemoveableTypeTrait; |
46: | use NonGeneralizableTypeTrait; |
47: | |
48: | |
49: | |
50: | |
51: | public function __construct(private ConstantStringType|ConstantIntegerType $offsetType) |
52: | { |
53: | } |
54: | |
55: | |
56: | |
57: | |
58: | public function getOffsetType(): Type |
59: | { |
60: | return $this->offsetType; |
61: | } |
62: | |
63: | public function getReferencedClasses(): array |
64: | { |
65: | return []; |
66: | } |
67: | |
68: | public function getObjectClassNames(): array |
69: | { |
70: | return []; |
71: | } |
72: | |
73: | public function getObjectClassReflections(): array |
74: | { |
75: | return []; |
76: | } |
77: | |
78: | public function getConstantStrings(): array |
79: | { |
80: | return []; |
81: | } |
82: | |
83: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
84: | { |
85: | if ($type instanceof CompoundType) { |
86: | return $type->isAcceptedBy($this, $strictTypes); |
87: | } |
88: | |
89: | return new AcceptsResult($type->isOffsetAccessible()->and($type->hasOffsetValueType($this->offsetType)), []); |
90: | } |
91: | |
92: | public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
93: | { |
94: | if ($this->equals($type)) { |
95: | return IsSuperTypeOfResult::createYes(); |
96: | } |
97: | return new IsSuperTypeOfResult($type->isOffsetAccessible()->and($type->hasOffsetValueType($this->offsetType)), []); |
98: | } |
99: | |
100: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
101: | { |
102: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
103: | return $otherType->isSuperTypeOf($this); |
104: | } |
105: | |
106: | $result = new IsSuperTypeOfResult($otherType->isOffsetAccessible()->and($otherType->hasOffsetValueType($this->offsetType)), []); |
107: | |
108: | return $result |
109: | ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe()); |
110: | } |
111: | |
112: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
113: | { |
114: | return $this->isSubTypeOf($acceptingType)->toAcceptsResult(); |
115: | } |
116: | |
117: | public function equals(Type $type): bool |
118: | { |
119: | return $type instanceof self |
120: | && $this->offsetType->equals($type->offsetType); |
121: | } |
122: | |
123: | public function describe(VerbosityLevel $level): string |
124: | { |
125: | return sprintf('hasOffset(%s)', $this->offsetType->describe($level)); |
126: | } |
127: | |
128: | public function isOffsetAccessible(): TrinaryLogic |
129: | { |
130: | return TrinaryLogic::createYes(); |
131: | } |
132: | |
133: | public function isOffsetAccessLegal(): TrinaryLogic |
134: | { |
135: | return TrinaryLogic::createYes(); |
136: | } |
137: | |
138: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
139: | { |
140: | if ($offsetType->isConstantScalarValue()->yes() && $offsetType->equals($this->offsetType)) { |
141: | return TrinaryLogic::createYes(); |
142: | } |
143: | |
144: | return TrinaryLogic::createMaybe(); |
145: | } |
146: | |
147: | public function getOffsetValueType(Type $offsetType): Type |
148: | { |
149: | return new MixedType(); |
150: | } |
151: | |
152: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
153: | { |
154: | return $this; |
155: | } |
156: | |
157: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
158: | { |
159: | return $this; |
160: | } |
161: | |
162: | public function unsetOffset(Type $offsetType): Type |
163: | { |
164: | if ($this->offsetType->isSuperTypeOf($offsetType)->yes()) { |
165: | return new ErrorType(); |
166: | } |
167: | return $this; |
168: | } |
169: | |
170: | public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type |
171: | { |
172: | return new NonEmptyArrayType(); |
173: | } |
174: | |
175: | public function fillKeysArray(Type $valueType): Type |
176: | { |
177: | return new NonEmptyArrayType(); |
178: | } |
179: | |
180: | public function intersectKeyArray(Type $otherArraysType): Type |
181: | { |
182: | if ($otherArraysType->hasOffsetValueType($this->offsetType)->yes()) { |
183: | return $this; |
184: | } |
185: | |
186: | return new MixedType(); |
187: | } |
188: | |
189: | public function reverseArray(TrinaryLogic $preserveKeys): Type |
190: | { |
191: | if ($preserveKeys->yes()) { |
192: | return $this; |
193: | } |
194: | |
195: | return new NonEmptyArrayType(); |
196: | } |
197: | |
198: | public function shuffleArray(): Type |
199: | { |
200: | return new NonEmptyArrayType(); |
201: | } |
202: | |
203: | public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type |
204: | { |
205: | if ( |
206: | $this->offsetType->isSuperTypeOf($offsetType)->yes() |
207: | && ($lengthType->isNull()->yes() || IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($lengthType)->yes()) |
208: | ) { |
209: | return $preserveKeys->yes() |
210: | ? TypeCombinator::intersect($this, new NonEmptyArrayType()) |
211: | : new NonEmptyArrayType(); |
212: | } |
213: | |
214: | return new MixedType(); |
215: | } |
216: | |
217: | public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type |
218: | { |
219: | if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) { |
220: | return $this; |
221: | } |
222: | |
223: | return new MixedType(); |
224: | } |
225: | |
226: | public function isIterableAtLeastOnce(): TrinaryLogic |
227: | { |
228: | return TrinaryLogic::createYes(); |
229: | } |
230: | |
231: | public function isList(): TrinaryLogic |
232: | { |
233: | if ($this->offsetType->isString()->yes()) { |
234: | return TrinaryLogic::createNo(); |
235: | } |
236: | |
237: | return TrinaryLogic::createMaybe(); |
238: | } |
239: | |
240: | public function isNull(): TrinaryLogic |
241: | { |
242: | return TrinaryLogic::createNo(); |
243: | } |
244: | |
245: | public function isConstantValue(): TrinaryLogic |
246: | { |
247: | return TrinaryLogic::createNo(); |
248: | } |
249: | |
250: | public function isConstantScalarValue(): TrinaryLogic |
251: | { |
252: | return TrinaryLogic::createNo(); |
253: | } |
254: | |
255: | public function getConstantScalarTypes(): array |
256: | { |
257: | return []; |
258: | } |
259: | |
260: | public function getConstantScalarValues(): array |
261: | { |
262: | return []; |
263: | } |
264: | |
265: | public function isTrue(): TrinaryLogic |
266: | { |
267: | return TrinaryLogic::createNo(); |
268: | } |
269: | |
270: | public function isFalse(): TrinaryLogic |
271: | { |
272: | return TrinaryLogic::createNo(); |
273: | } |
274: | |
275: | public function isBoolean(): TrinaryLogic |
276: | { |
277: | return TrinaryLogic::createNo(); |
278: | } |
279: | |
280: | public function isFloat(): TrinaryLogic |
281: | { |
282: | return TrinaryLogic::createNo(); |
283: | } |
284: | |
285: | public function isInteger(): TrinaryLogic |
286: | { |
287: | return TrinaryLogic::createNo(); |
288: | } |
289: | |
290: | public function isString(): TrinaryLogic |
291: | { |
292: | return TrinaryLogic::createMaybe(); |
293: | } |
294: | |
295: | public function isNumericString(): TrinaryLogic |
296: | { |
297: | return TrinaryLogic::createMaybe(); |
298: | } |
299: | |
300: | public function isNonEmptyString(): TrinaryLogic |
301: | { |
302: | return TrinaryLogic::createMaybe(); |
303: | } |
304: | |
305: | public function isNonFalsyString(): TrinaryLogic |
306: | { |
307: | return TrinaryLogic::createMaybe(); |
308: | } |
309: | |
310: | public function isLiteralString(): TrinaryLogic |
311: | { |
312: | return TrinaryLogic::createMaybe(); |
313: | } |
314: | |
315: | public function isLowercaseString(): TrinaryLogic |
316: | { |
317: | return TrinaryLogic::createMaybe(); |
318: | } |
319: | |
320: | public function isClassString(): TrinaryLogic |
321: | { |
322: | return TrinaryLogic::createMaybe(); |
323: | } |
324: | |
325: | public function isUppercaseString(): TrinaryLogic |
326: | { |
327: | return TrinaryLogic::createMaybe(); |
328: | } |
329: | |
330: | public function getClassStringObjectType(): Type |
331: | { |
332: | return new ObjectWithoutClassType(); |
333: | } |
334: | |
335: | public function getObjectTypeOrClassStringObjectType(): Type |
336: | { |
337: | return new ObjectWithoutClassType(); |
338: | } |
339: | |
340: | public function isVoid(): TrinaryLogic |
341: | { |
342: | return TrinaryLogic::createNo(); |
343: | } |
344: | |
345: | public function isScalar(): TrinaryLogic |
346: | { |
347: | return TrinaryLogic::createMaybe(); |
348: | } |
349: | |
350: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
351: | { |
352: | return new BooleanType(); |
353: | } |
354: | |
355: | public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type |
356: | { |
357: | return $this->getKeysArray(); |
358: | } |
359: | |
360: | public function getKeysArray(): Type |
361: | { |
362: | return new NonEmptyArrayType(); |
363: | } |
364: | |
365: | public function getValuesArray(): Type |
366: | { |
367: | return new NonEmptyArrayType(); |
368: | } |
369: | |
370: | public function toNumber(): Type |
371: | { |
372: | return new ErrorType(); |
373: | } |
374: | |
375: | public function toAbsoluteNumber(): Type |
376: | { |
377: | return new ErrorType(); |
378: | } |
379: | |
380: | public function toInteger(): Type |
381: | { |
382: | return new ErrorType(); |
383: | } |
384: | |
385: | public function toFloat(): Type |
386: | { |
387: | return new ErrorType(); |
388: | } |
389: | |
390: | public function toString(): Type |
391: | { |
392: | return new ErrorType(); |
393: | } |
394: | |
395: | public function toArray(): Type |
396: | { |
397: | return new MixedType(); |
398: | } |
399: | |
400: | public function toArrayKey(): Type |
401: | { |
402: | return new ErrorType(); |
403: | } |
404: | |
405: | public function toCoercedArgumentType(bool $strictTypes): Type |
406: | { |
407: | return $this; |
408: | } |
409: | |
410: | public function getEnumCases(): array |
411: | { |
412: | return []; |
413: | } |
414: | |
415: | public function traverse(callable $cb): Type |
416: | { |
417: | return $this; |
418: | } |
419: | |
420: | public function traverseSimultaneously(Type $right, callable $cb): Type |
421: | { |
422: | return $this; |
423: | } |
424: | |
425: | public function exponentiate(Type $exponent): Type |
426: | { |
427: | return new ErrorType(); |
428: | } |
429: | |
430: | public function getFiniteTypes(): array |
431: | { |
432: | return []; |
433: | } |
434: | |
435: | public function toPhpDocNode(): TypeNode |
436: | { |
437: | return new IdentifierTypeNode(''); |
438: | } |
439: | |
440: | } |
441: | |