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\IntersectionType;
16: use PHPStan\Type\MixedType;
17: use PHPStan\Type\ObjectWithoutClassType;
18: use PHPStan\Type\Traits\MaybeArrayTypeTrait;
19: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
20: use PHPStan\Type\Traits\MaybeIterableTypeTrait;
21: use PHPStan\Type\Traits\MaybeObjectTypeTrait;
22: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
23: use PHPStan\Type\Traits\NonGenericTypeTrait;
24: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
25: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
26: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
27: use PHPStan\Type\Type;
28: use PHPStan\Type\UnionType;
29: use PHPStan\Type\VerbosityLevel;
30: use function sprintf;
31:
32: class HasOffsetType implements CompoundType, AccessoryType
33: {
34:
35: use MaybeArrayTypeTrait;
36: use MaybeCallableTypeTrait;
37: use MaybeIterableTypeTrait;
38: use MaybeObjectTypeTrait;
39: use TruthyBooleanTypeTrait;
40: use NonGenericTypeTrait;
41: use UndecidedComparisonCompoundTypeTrait;
42: use NonRemoveableTypeTrait;
43: use NonGeneralizableTypeTrait;
44:
45: /**
46: * @api
47: * @param ConstantStringType|ConstantIntegerType $offsetType
48: */
49: public function __construct(private Type $offsetType)
50: {
51: }
52:
53: /**
54: * @return ConstantStringType|ConstantIntegerType
55: */
56: public function getOffsetType(): Type
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): TrinaryLogic
82: {
83: return $this->acceptsWithReason($type, $strictTypes)->result;
84: }
85:
86: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
87: {
88: if ($type instanceof CompoundType) {
89: return $type->isAcceptedWithReasonBy($this, $strictTypes);
90: }
91:
92: return new AcceptsResult($type->isOffsetAccessible()->and($type->hasOffsetValueType($this->offsetType)), []);
93: }
94:
95: public function isSuperTypeOf(Type $type): TrinaryLogic
96: {
97: if ($this->equals($type)) {
98: return TrinaryLogic::createYes();
99: }
100: return $type->isOffsetAccessible()
101: ->and($type->hasOffsetValueType($this->offsetType));
102: }
103:
104: public function isSubTypeOf(Type $otherType): TrinaryLogic
105: {
106: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
107: return $otherType->isSuperTypeOf($this);
108: }
109:
110: return $otherType->isOffsetAccessible()
111: ->and($otherType->hasOffsetValueType($this->offsetType))
112: ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe());
113: }
114:
115: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
116: {
117: return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result;
118: }
119:
120: public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult
121: {
122: return new AcceptsResult($this->isSubTypeOf($acceptingType), []);
123: }
124:
125: public function equals(Type $type): bool
126: {
127: return $type instanceof self
128: && $this->offsetType->equals($type->offsetType);
129: }
130:
131: public function describe(VerbosityLevel $level): string
132: {
133: return sprintf('hasOffset(%s)', $this->offsetType->describe($level));
134: }
135:
136: public function isOffsetAccessible(): TrinaryLogic
137: {
138: return TrinaryLogic::createYes();
139: }
140:
141: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
142: {
143: if ($offsetType->isConstantScalarValue()->yes() && $offsetType->equals($this->offsetType)) {
144: return TrinaryLogic::createYes();
145: }
146:
147: return TrinaryLogic::createMaybe();
148: }
149:
150: public function getOffsetValueType(Type $offsetType): Type
151: {
152: return new MixedType();
153: }
154:
155: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
156: {
157: return $this;
158: }
159:
160: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
161: {
162: return $this;
163: }
164:
165: public function unsetOffset(Type $offsetType): Type
166: {
167: if ($this->offsetType->isSuperTypeOf($offsetType)->yes()) {
168: return new ErrorType();
169: }
170: return $this;
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 shuffleArray(): Type
188: {
189: return new NonEmptyArrayType();
190: }
191:
192: public function isIterableAtLeastOnce(): TrinaryLogic
193: {
194: return TrinaryLogic::createYes();
195: }
196:
197: public function isList(): TrinaryLogic
198: {
199: if ($this->offsetType->isString()->yes()) {
200: return TrinaryLogic::createNo();
201: }
202:
203: return TrinaryLogic::createMaybe();
204: }
205:
206: public function isNull(): TrinaryLogic
207: {
208: return TrinaryLogic::createNo();
209: }
210:
211: public function isConstantValue(): TrinaryLogic
212: {
213: return TrinaryLogic::createNo();
214: }
215:
216: public function isConstantScalarValue(): TrinaryLogic
217: {
218: return TrinaryLogic::createNo();
219: }
220:
221: public function getConstantScalarTypes(): array
222: {
223: return [];
224: }
225:
226: public function getConstantScalarValues(): array
227: {
228: return [];
229: }
230:
231: public function isTrue(): TrinaryLogic
232: {
233: return TrinaryLogic::createNo();
234: }
235:
236: public function isFalse(): TrinaryLogic
237: {
238: return TrinaryLogic::createNo();
239: }
240:
241: public function isBoolean(): TrinaryLogic
242: {
243: return TrinaryLogic::createNo();
244: }
245:
246: public function isFloat(): TrinaryLogic
247: {
248: return TrinaryLogic::createNo();
249: }
250:
251: public function isInteger(): TrinaryLogic
252: {
253: return TrinaryLogic::createNo();
254: }
255:
256: public function isString(): TrinaryLogic
257: {
258: return TrinaryLogic::createMaybe();
259: }
260:
261: public function isNumericString(): TrinaryLogic
262: {
263: return TrinaryLogic::createMaybe();
264: }
265:
266: public function isNonEmptyString(): TrinaryLogic
267: {
268: return TrinaryLogic::createMaybe();
269: }
270:
271: public function isNonFalsyString(): TrinaryLogic
272: {
273: return TrinaryLogic::createMaybe();
274: }
275:
276: public function isLiteralString(): TrinaryLogic
277: {
278: return TrinaryLogic::createMaybe();
279: }
280:
281: public function isClassStringType(): TrinaryLogic
282: {
283: return TrinaryLogic::createMaybe();
284: }
285:
286: public function getClassStringObjectType(): Type
287: {
288: return new ObjectWithoutClassType();
289: }
290:
291: public function getObjectTypeOrClassStringObjectType(): Type
292: {
293: return new ObjectWithoutClassType();
294: }
295:
296: public function isVoid(): TrinaryLogic
297: {
298: return TrinaryLogic::createNo();
299: }
300:
301: public function isScalar(): TrinaryLogic
302: {
303: return TrinaryLogic::createMaybe();
304: }
305:
306: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
307: {
308: return new BooleanType();
309: }
310:
311: public function getKeysArray(): Type
312: {
313: return new NonEmptyArrayType();
314: }
315:
316: public function getValuesArray(): Type
317: {
318: return new NonEmptyArrayType();
319: }
320:
321: public function toNumber(): Type
322: {
323: return new ErrorType();
324: }
325:
326: public function toInteger(): Type
327: {
328: return new ErrorType();
329: }
330:
331: public function toFloat(): Type
332: {
333: return new ErrorType();
334: }
335:
336: public function toString(): Type
337: {
338: return new ErrorType();
339: }
340:
341: public function toArray(): Type
342: {
343: return new MixedType();
344: }
345:
346: public function toArrayKey(): Type
347: {
348: return new ErrorType();
349: }
350:
351: public function getEnumCases(): array
352: {
353: return [];
354: }
355:
356: public function traverse(callable $cb): Type
357: {
358: return $this;
359: }
360:
361: public function traverseSimultaneously(Type $right, callable $cb): Type
362: {
363: return $this;
364: }
365:
366: public function exponentiate(Type $exponent): Type
367: {
368: return new ErrorType();
369: }
370:
371: public function getFiniteTypes(): array
372: {
373: return [];
374: }
375:
376: public static function __set_state(array $properties): Type
377: {
378: return new self($properties['offsetType']);
379: }
380:
381: public function toPhpDocNode(): TypeNode
382: {
383: return new IdentifierTypeNode(''); // no PHPDoc representation
384: }
385:
386: }
387: