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