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: | |
47: | |
48: | |
49: | public function __construct(private Type $offsetType) |
50: | { |
51: | } |
52: | |
53: | |
54: | |
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 isOffsetAccessLegal(): TrinaryLogic |
142: | { |
143: | return TrinaryLogic::createYes(); |
144: | } |
145: | |
146: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
147: | { |
148: | if ($offsetType->isConstantScalarValue()->yes() && $offsetType->equals($this->offsetType)) { |
149: | return TrinaryLogic::createYes(); |
150: | } |
151: | |
152: | return TrinaryLogic::createMaybe(); |
153: | } |
154: | |
155: | public function getOffsetValueType(Type $offsetType): Type |
156: | { |
157: | return new MixedType(); |
158: | } |
159: | |
160: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
161: | { |
162: | return $this; |
163: | } |
164: | |
165: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
166: | { |
167: | return $this; |
168: | } |
169: | |
170: | public function unsetOffset(Type $offsetType): Type |
171: | { |
172: | if ($this->offsetType->isSuperTypeOf($offsetType)->yes()) { |
173: | return new ErrorType(); |
174: | } |
175: | return $this; |
176: | } |
177: | |
178: | public function fillKeysArray(Type $valueType): Type |
179: | { |
180: | return new NonEmptyArrayType(); |
181: | } |
182: | |
183: | public function intersectKeyArray(Type $otherArraysType): Type |
184: | { |
185: | if ($otherArraysType->hasOffsetValueType($this->offsetType)->yes()) { |
186: | return $this; |
187: | } |
188: | |
189: | return new MixedType(); |
190: | } |
191: | |
192: | public function shuffleArray(): Type |
193: | { |
194: | return new NonEmptyArrayType(); |
195: | } |
196: | |
197: | public function isIterableAtLeastOnce(): TrinaryLogic |
198: | { |
199: | return TrinaryLogic::createYes(); |
200: | } |
201: | |
202: | public function isList(): TrinaryLogic |
203: | { |
204: | if ($this->offsetType->isString()->yes()) { |
205: | return TrinaryLogic::createNo(); |
206: | } |
207: | |
208: | return TrinaryLogic::createMaybe(); |
209: | } |
210: | |
211: | public function isNull(): TrinaryLogic |
212: | { |
213: | return TrinaryLogic::createNo(); |
214: | } |
215: | |
216: | public function isConstantValue(): TrinaryLogic |
217: | { |
218: | return TrinaryLogic::createNo(); |
219: | } |
220: | |
221: | public function isConstantScalarValue(): TrinaryLogic |
222: | { |
223: | return TrinaryLogic::createNo(); |
224: | } |
225: | |
226: | public function getConstantScalarTypes(): array |
227: | { |
228: | return []; |
229: | } |
230: | |
231: | public function getConstantScalarValues(): array |
232: | { |
233: | return []; |
234: | } |
235: | |
236: | public function isTrue(): TrinaryLogic |
237: | { |
238: | return TrinaryLogic::createNo(); |
239: | } |
240: | |
241: | public function isFalse(): TrinaryLogic |
242: | { |
243: | return TrinaryLogic::createNo(); |
244: | } |
245: | |
246: | public function isBoolean(): TrinaryLogic |
247: | { |
248: | return TrinaryLogic::createNo(); |
249: | } |
250: | |
251: | public function isFloat(): TrinaryLogic |
252: | { |
253: | return TrinaryLogic::createNo(); |
254: | } |
255: | |
256: | public function isInteger(): TrinaryLogic |
257: | { |
258: | return TrinaryLogic::createNo(); |
259: | } |
260: | |
261: | public function isString(): TrinaryLogic |
262: | { |
263: | return TrinaryLogic::createMaybe(); |
264: | } |
265: | |
266: | public function isNumericString(): TrinaryLogic |
267: | { |
268: | return TrinaryLogic::createMaybe(); |
269: | } |
270: | |
271: | public function isNonEmptyString(): TrinaryLogic |
272: | { |
273: | return TrinaryLogic::createMaybe(); |
274: | } |
275: | |
276: | public function isNonFalsyString(): TrinaryLogic |
277: | { |
278: | return TrinaryLogic::createMaybe(); |
279: | } |
280: | |
281: | public function isLiteralString(): TrinaryLogic |
282: | { |
283: | return TrinaryLogic::createMaybe(); |
284: | } |
285: | |
286: | public function isClassStringType(): TrinaryLogic |
287: | { |
288: | return TrinaryLogic::createMaybe(); |
289: | } |
290: | |
291: | public function getClassStringObjectType(): Type |
292: | { |
293: | return new ObjectWithoutClassType(); |
294: | } |
295: | |
296: | public function getObjectTypeOrClassStringObjectType(): Type |
297: | { |
298: | return new ObjectWithoutClassType(); |
299: | } |
300: | |
301: | public function isVoid(): TrinaryLogic |
302: | { |
303: | return TrinaryLogic::createNo(); |
304: | } |
305: | |
306: | public function isScalar(): TrinaryLogic |
307: | { |
308: | return TrinaryLogic::createMaybe(); |
309: | } |
310: | |
311: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
312: | { |
313: | return new BooleanType(); |
314: | } |
315: | |
316: | public function getKeysArray(): Type |
317: | { |
318: | return new NonEmptyArrayType(); |
319: | } |
320: | |
321: | public function getValuesArray(): Type |
322: | { |
323: | return new NonEmptyArrayType(); |
324: | } |
325: | |
326: | public function toNumber(): Type |
327: | { |
328: | return new ErrorType(); |
329: | } |
330: | |
331: | public function toInteger(): Type |
332: | { |
333: | return new ErrorType(); |
334: | } |
335: | |
336: | public function toFloat(): Type |
337: | { |
338: | return new ErrorType(); |
339: | } |
340: | |
341: | public function toString(): Type |
342: | { |
343: | return new ErrorType(); |
344: | } |
345: | |
346: | public function toArray(): Type |
347: | { |
348: | return new MixedType(); |
349: | } |
350: | |
351: | public function toArrayKey(): Type |
352: | { |
353: | return new ErrorType(); |
354: | } |
355: | |
356: | public function getEnumCases(): array |
357: | { |
358: | return []; |
359: | } |
360: | |
361: | public function traverse(callable $cb): Type |
362: | { |
363: | return $this; |
364: | } |
365: | |
366: | public function traverseSimultaneously(Type $right, callable $cb): Type |
367: | { |
368: | return $this; |
369: | } |
370: | |
371: | public function exponentiate(Type $exponent): Type |
372: | { |
373: | return new ErrorType(); |
374: | } |
375: | |
376: | public function getFiniteTypes(): array |
377: | { |
378: | return []; |
379: | } |
380: | |
381: | public static function __set_state(array $properties): Type |
382: | { |
383: | return new self($properties['offsetType']); |
384: | } |
385: | |
386: | public function toPhpDocNode(): TypeNode |
387: | { |
388: | return new IdentifierTypeNode(''); |
389: | } |
390: | |
391: | } |
392: | |