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