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 isIterableAtLeastOnce(): TrinaryLogic |
218: | { |
219: | return TrinaryLogic::createYes(); |
220: | } |
221: | |
222: | public function isList(): TrinaryLogic |
223: | { |
224: | if ($this->offsetType->isString()->yes()) { |
225: | return TrinaryLogic::createNo(); |
226: | } |
227: | |
228: | return TrinaryLogic::createMaybe(); |
229: | } |
230: | |
231: | public function isNull(): TrinaryLogic |
232: | { |
233: | return TrinaryLogic::createNo(); |
234: | } |
235: | |
236: | public function isConstantValue(): TrinaryLogic |
237: | { |
238: | return TrinaryLogic::createNo(); |
239: | } |
240: | |
241: | public function isConstantScalarValue(): TrinaryLogic |
242: | { |
243: | return TrinaryLogic::createNo(); |
244: | } |
245: | |
246: | public function getConstantScalarTypes(): array |
247: | { |
248: | return []; |
249: | } |
250: | |
251: | public function getConstantScalarValues(): array |
252: | { |
253: | return []; |
254: | } |
255: | |
256: | public function isTrue(): TrinaryLogic |
257: | { |
258: | return TrinaryLogic::createNo(); |
259: | } |
260: | |
261: | public function isFalse(): TrinaryLogic |
262: | { |
263: | return TrinaryLogic::createNo(); |
264: | } |
265: | |
266: | public function isBoolean(): TrinaryLogic |
267: | { |
268: | return TrinaryLogic::createNo(); |
269: | } |
270: | |
271: | public function isFloat(): TrinaryLogic |
272: | { |
273: | return TrinaryLogic::createNo(); |
274: | } |
275: | |
276: | public function isInteger(): TrinaryLogic |
277: | { |
278: | return TrinaryLogic::createNo(); |
279: | } |
280: | |
281: | public function isString(): TrinaryLogic |
282: | { |
283: | return TrinaryLogic::createMaybe(); |
284: | } |
285: | |
286: | public function isNumericString(): TrinaryLogic |
287: | { |
288: | return TrinaryLogic::createMaybe(); |
289: | } |
290: | |
291: | public function isNonEmptyString(): TrinaryLogic |
292: | { |
293: | return TrinaryLogic::createMaybe(); |
294: | } |
295: | |
296: | public function isNonFalsyString(): TrinaryLogic |
297: | { |
298: | return TrinaryLogic::createMaybe(); |
299: | } |
300: | |
301: | public function isLiteralString(): TrinaryLogic |
302: | { |
303: | return TrinaryLogic::createMaybe(); |
304: | } |
305: | |
306: | public function isLowercaseString(): TrinaryLogic |
307: | { |
308: | return TrinaryLogic::createMaybe(); |
309: | } |
310: | |
311: | public function isClassString(): TrinaryLogic |
312: | { |
313: | return TrinaryLogic::createMaybe(); |
314: | } |
315: | |
316: | public function isUppercaseString(): TrinaryLogic |
317: | { |
318: | return TrinaryLogic::createMaybe(); |
319: | } |
320: | |
321: | public function getClassStringObjectType(): Type |
322: | { |
323: | return new ObjectWithoutClassType(); |
324: | } |
325: | |
326: | public function getObjectTypeOrClassStringObjectType(): Type |
327: | { |
328: | return new ObjectWithoutClassType(); |
329: | } |
330: | |
331: | public function isVoid(): TrinaryLogic |
332: | { |
333: | return TrinaryLogic::createNo(); |
334: | } |
335: | |
336: | public function isScalar(): TrinaryLogic |
337: | { |
338: | return TrinaryLogic::createMaybe(); |
339: | } |
340: | |
341: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
342: | { |
343: | return new BooleanType(); |
344: | } |
345: | |
346: | public function getKeysArray(): Type |
347: | { |
348: | return new NonEmptyArrayType(); |
349: | } |
350: | |
351: | public function getValuesArray(): Type |
352: | { |
353: | return new NonEmptyArrayType(); |
354: | } |
355: | |
356: | public function toNumber(): Type |
357: | { |
358: | return new ErrorType(); |
359: | } |
360: | |
361: | public function toAbsoluteNumber(): Type |
362: | { |
363: | return new ErrorType(); |
364: | } |
365: | |
366: | public function toInteger(): Type |
367: | { |
368: | return new ErrorType(); |
369: | } |
370: | |
371: | public function toFloat(): Type |
372: | { |
373: | return new ErrorType(); |
374: | } |
375: | |
376: | public function toString(): Type |
377: | { |
378: | return new ErrorType(); |
379: | } |
380: | |
381: | public function toArray(): Type |
382: | { |
383: | return new MixedType(); |
384: | } |
385: | |
386: | public function toArrayKey(): Type |
387: | { |
388: | return new ErrorType(); |
389: | } |
390: | |
391: | public function getEnumCases(): array |
392: | { |
393: | return []; |
394: | } |
395: | |
396: | public function traverse(callable $cb): Type |
397: | { |
398: | return $this; |
399: | } |
400: | |
401: | public function traverseSimultaneously(Type $right, callable $cb): Type |
402: | { |
403: | return $this; |
404: | } |
405: | |
406: | public function exponentiate(Type $exponent): Type |
407: | { |
408: | return new ErrorType(); |
409: | } |
410: | |
411: | public function getFiniteTypes(): array |
412: | { |
413: | return []; |
414: | } |
415: | |
416: | public function toPhpDocNode(): TypeNode |
417: | { |
418: | return new IdentifierTypeNode(''); |
419: | } |
420: | |
421: | } |
422: | |