1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use ArrayAccess;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Type\AcceptsResult;
11: use PHPStan\Type\ArrayType;
12: use PHPStan\Type\BooleanType;
13: use PHPStan\Type\CompoundType;
14: use PHPStan\Type\Constant\ConstantIntegerType;
15: use PHPStan\Type\Constant\ConstantStringType;
16: use PHPStan\Type\Enum\EnumCaseObjectType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\IntegerRangeType;
19: use PHPStan\Type\IntersectionType;
20: use PHPStan\Type\IsSuperTypeOfResult;
21: use PHPStan\Type\MixedType;
22: use PHPStan\Type\ObjectType;
23: use PHPStan\Type\ObjectWithoutClassType;
24: use PHPStan\Type\Traits\MaybeArrayTypeTrait;
25: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
26: use PHPStan\Type\Traits\MaybeIterableTypeTrait;
27: use PHPStan\Type\Traits\MaybeObjectTypeTrait;
28: use PHPStan\Type\Traits\MaybeStringTypeTrait;
29: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
30: use PHPStan\Type\Traits\NonGenericTypeTrait;
31: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
32: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
33: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
34: use PHPStan\Type\Type;
35: use PHPStan\Type\TypeCombinator;
36: use PHPStan\Type\UnionType;
37: use PHPStan\Type\VerbosityLevel;
38: use function sprintf;
39: use function strtolower;
40: use function strtoupper;
41: use const CASE_LOWER;
42: use const CASE_UPPER;
43:
44: class HasOffsetType implements CompoundType, AccessoryType
45: {
46:
47: use MaybeArrayTypeTrait;
48: use MaybeCallableTypeTrait;
49: use MaybeIterableTypeTrait;
50: use MaybeObjectTypeTrait;
51: use MaybeStringTypeTrait;
52: use TruthyBooleanTypeTrait;
53: use NonGenericTypeTrait;
54: use UndecidedComparisonCompoundTypeTrait;
55: use NonRemoveableTypeTrait;
56: use NonGeneralizableTypeTrait;
57:
58: /**
59: * @api
60: */
61: public function __construct(private ConstantStringType|ConstantIntegerType $offsetType)
62: {
63: }
64:
65: public function getOffsetType(): ConstantStringType|ConstantIntegerType
66: {
67: return $this->offsetType;
68: }
69:
70: public function getReferencedClasses(): array
71: {
72: return [];
73: }
74:
75: public function getObjectClassNames(): array
76: {
77: return [];
78: }
79:
80: public function getObjectClassReflections(): array
81: {
82: return [];
83: }
84:
85: public function accepts(Type $type, bool $strictTypes): AcceptsResult
86: {
87: if ($type instanceof CompoundType) {
88: return $type->isAcceptedBy($this, $strictTypes);
89: }
90:
91: return new AcceptsResult($type->isOffsetAccessible()->and($type->hasOffsetValueType($this->offsetType)), []);
92: }
93:
94: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
95: {
96: if ($this->equals($type)) {
97: return IsSuperTypeOfResult::createYes();
98: }
99: return new IsSuperTypeOfResult($type->isOffsetAccessible()->and($type->hasOffsetValueType($this->offsetType)), []);
100: }
101:
102: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
103: {
104: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
105: return $otherType->isSuperTypeOf($this);
106: }
107:
108: return new IsSuperTypeOfResult(
109: $otherType->isOffsetAccessible()->and($otherType->hasOffsetValueType($this->offsetType))->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
110: [],
111: );
112: }
113:
114: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
115: {
116: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
117: }
118:
119: public function equals(Type $type): bool
120: {
121: return $type instanceof self
122: && $this->offsetType->equals($type->offsetType);
123: }
124:
125: public function describe(VerbosityLevel $level): string
126: {
127: return sprintf('hasOffset(%s)', $this->offsetType->describe($level));
128: }
129:
130: public function isOffsetAccessible(): TrinaryLogic
131: {
132: return TrinaryLogic::createYes();
133: }
134:
135: public function isOffsetAccessLegal(): TrinaryLogic
136: {
137: return TrinaryLogic::createYes();
138: }
139:
140: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
141: {
142: if ($offsetType->isConstantScalarValue()->yes() && $offsetType->equals($this->offsetType)) {
143: return TrinaryLogic::createYes();
144: }
145:
146: return TrinaryLogic::createMaybe();
147: }
148:
149: public function getOffsetValueType(Type $offsetType): Type
150: {
151: return new MixedType();
152: }
153:
154: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
155: {
156: return $this;
157: }
158:
159: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
160: {
161: return $this;
162: }
163:
164: public function unsetOffset(Type $offsetType): Type
165: {
166: if ($this->offsetType->isSuperTypeOf($offsetType)->yes()) {
167: return new ErrorType();
168: }
169: return $this;
170: }
171:
172: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
173: {
174: return new NonEmptyArrayType();
175: }
176:
177: public function fillKeysArray(Type $valueType): Type
178: {
179: return new NonEmptyArrayType();
180: }
181:
182: public function intersectKeyArray(Type $otherArraysType): Type
183: {
184: if ($otherArraysType->hasOffsetValueType($this->offsetType)->yes()) {
185: return $this;
186: }
187:
188: return new MixedType();
189: }
190:
191: public function reverseArray(TrinaryLogic $preserveKeys): Type
192: {
193: if ($preserveKeys->yes()) {
194: return $this;
195: }
196:
197: return new NonEmptyArrayType();
198: }
199:
200: public function shuffleArray(): Type
201: {
202: return new NonEmptyArrayType();
203: }
204:
205: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
206: {
207: if (
208: $this->offsetType->isSuperTypeOf($offsetType)->yes()
209: && ($lengthType->isNull()->yes() || IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($lengthType)->yes())
210: ) {
211: return $preserveKeys->yes()
212: ? TypeCombinator::intersect($this, new NonEmptyArrayType())
213: : new NonEmptyArrayType();
214: }
215:
216: return new MixedType();
217: }
218:
219: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
220: {
221: if ((new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes()) {
222: return $this;
223: }
224:
225: return new MixedType();
226: }
227:
228: public function truncateListToSize(Type $sizeType): Type
229: {
230: // Having a specific offset is independent of the array's size bound.
231: return $this;
232: }
233:
234: public function makeListMaybe(): Type
235: {
236: // Having an offset doesn't conflict with list-being-maybe.
237: return $this;
238: }
239:
240: public function mapValueType(callable $cb): Type
241: {
242: // `HasOffsetType` only records that an offset exists, not its
243: // value; the assertion still holds after a value transformation.
244: return $this;
245: }
246:
247: public function mapKeyType(callable $cb): Type
248: {
249: // Match the prior `TypeTraverser`-based pattern that left
250: // accessories untouched while rewriting the array key type.
251: return $this;
252: }
253:
254: public function makeAllArrayKeysOptional(): Type
255: {
256: // "Has offset X" is no longer guaranteed when X is now optional.
257: return new MixedType();
258: }
259:
260: public function changeKeyCaseArray(?int $case): Type
261: {
262: // A string offset is itself case-folded; an int offset is unchanged.
263: if (!$this->offsetType instanceof ConstantStringType) {
264: return $this;
265: }
266:
267: $value = $this->offsetType->getValue();
268: if ($case === CASE_LOWER) {
269: return new self(new ConstantStringType(strtolower($value)));
270: }
271: if ($case === CASE_UPPER) {
272: return new self(new ConstantStringType(strtoupper($value)));
273: }
274:
275: // Unknown case → could be either fold; the accessory weakens to
276: // "no specific offset known".
277: return new MixedType();
278: }
279:
280: public function filterArrayRemovingFalsey(): Type
281: {
282: // We don't track the value at this offset, so we can't guarantee
283: // it survives a falsey filter. Drop the assertion.
284: return new MixedType();
285: }
286:
287: public function isIterableAtLeastOnce(): TrinaryLogic
288: {
289: return TrinaryLogic::createYes();
290: }
291:
292: public function isList(): TrinaryLogic
293: {
294: if ($this->offsetType->isString()->yes()) {
295: return TrinaryLogic::createNo();
296: }
297:
298: return TrinaryLogic::createMaybe();
299: }
300:
301: public function isNull(): TrinaryLogic
302: {
303: return TrinaryLogic::createNo();
304: }
305:
306: public function isConstantValue(): TrinaryLogic
307: {
308: return TrinaryLogic::createNo();
309: }
310:
311: public function isConstantScalarValue(): TrinaryLogic
312: {
313: return TrinaryLogic::createNo();
314: }
315:
316: public function getConstantScalarTypes(): array
317: {
318: return [];
319: }
320:
321: public function getConstantScalarValues(): array
322: {
323: return [];
324: }
325:
326: public function isTrue(): TrinaryLogic
327: {
328: return TrinaryLogic::createNo();
329: }
330:
331: public function isFalse(): TrinaryLogic
332: {
333: return TrinaryLogic::createNo();
334: }
335:
336: public function isBoolean(): TrinaryLogic
337: {
338: return TrinaryLogic::createNo();
339: }
340:
341: public function isFloat(): TrinaryLogic
342: {
343: return TrinaryLogic::createNo();
344: }
345:
346: public function isInteger(): TrinaryLogic
347: {
348: return TrinaryLogic::createNo();
349: }
350:
351: public function getClassStringObjectType(): Type
352: {
353: return new ObjectWithoutClassType();
354: }
355:
356: public function getObjectTypeOrClassStringObjectType(): Type
357: {
358: return new ObjectWithoutClassType();
359: }
360:
361: public function isVoid(): TrinaryLogic
362: {
363: return TrinaryLogic::createNo();
364: }
365:
366: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
367: {
368: return new BooleanType();
369: }
370:
371: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
372: {
373: return $this->getKeysArray();
374: }
375:
376: public function getKeysArray(): Type
377: {
378: return new NonEmptyArrayType();
379: }
380:
381: public function getValuesArray(): Type
382: {
383: return new NonEmptyArrayType();
384: }
385:
386: public function toNumber(): Type
387: {
388: return new ErrorType();
389: }
390:
391: public function toBitwiseNotType(): Type
392: {
393: return new ErrorType();
394: }
395:
396: public function toAbsoluteNumber(): Type
397: {
398: return new ErrorType();
399: }
400:
401: public function toInteger(): Type
402: {
403: return new ErrorType();
404: }
405:
406: public function toFloat(): Type
407: {
408: return new ErrorType();
409: }
410:
411: public function toString(): Type
412: {
413: return new ErrorType();
414: }
415:
416: public function toArray(): Type
417: {
418: return new MixedType();
419: }
420:
421: public function toArrayKey(): Type
422: {
423: return new ErrorType();
424: }
425:
426: public function toCoercedArgumentType(bool $strictTypes): Type
427: {
428: return $this;
429: }
430:
431: public function getEnumCases(): array
432: {
433: return [];
434: }
435:
436: public function getEnumCaseObject(): ?EnumCaseObjectType
437: {
438: return null;
439: }
440:
441: public function traverse(callable $cb): Type
442: {
443: return $this;
444: }
445:
446: public function traverseSimultaneously(Type $right, callable $cb): Type
447: {
448: return $this;
449: }
450:
451: public function exponentiate(Type $exponent): Type
452: {
453: return new ErrorType();
454: }
455:
456: public function getFiniteTypes(): array
457: {
458: return [];
459: }
460:
461: public function getDefaultBaseType(): Type
462: {
463: return new UnionType([
464: new ArrayType(new MixedType(), new MixedType()),
465: new ObjectType(ArrayAccess::class),
466: ]);
467: }
468:
469: public function toPhpDocNode(): TypeNode
470: {
471: return new IdentifierTypeNode(''); // no PHPDoc representation
472: }
473:
474: public function hasTemplateOrLateResolvableType(): bool
475: {
476: return $this->offsetType->hasTemplateOrLateResolvableType();
477: }
478:
479: }
480: