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