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\BenevolentUnionType;
11: use PHPStan\Type\BooleanType;
12: use PHPStan\Type\CompoundType;
13: use PHPStan\Type\Constant\ConstantArrayType;
14: use PHPStan\Type\Constant\ConstantBooleanType;
15: use PHPStan\Type\Constant\ConstantIntegerType;
16: use PHPStan\Type\Constant\ConstantStringType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\FloatType;
19: use PHPStan\Type\GeneralizePrecision;
20: use PHPStan\Type\IntegerType;
21: use PHPStan\Type\IntersectionType;
22: use PHPStan\Type\IsSuperTypeOfResult;
23: use PHPStan\Type\StringType;
24: use PHPStan\Type\Traits\NonArrayTypeTrait;
25: use PHPStan\Type\Traits\NonCallableTypeTrait;
26: use PHPStan\Type\Traits\NonGenericTypeTrait;
27: use PHPStan\Type\Traits\NonIterableTypeTrait;
28: use PHPStan\Type\Traits\NonObjectTypeTrait;
29: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
30: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
31: use PHPStan\Type\Type;
32: use PHPStan\Type\TypeCombinator;
33: use PHPStan\Type\UnionType;
34: use PHPStan\Type\VerbosityLevel;
35:
36: class AccessoryNumericStringType implements CompoundType, AccessoryType
37: {
38:
39: use NonArrayTypeTrait;
40: use NonCallableTypeTrait;
41: use NonObjectTypeTrait;
42: use NonIterableTypeTrait;
43: use UndecidedBooleanTypeTrait;
44: use UndecidedComparisonCompoundTypeTrait;
45: use NonGenericTypeTrait;
46:
47: /** @api */
48: public function __construct()
49: {
50: }
51:
52: public function getReferencedClasses(): array
53: {
54: return [];
55: }
56:
57: public function getObjectClassNames(): array
58: {
59: return [];
60: }
61:
62: public function getObjectClassReflections(): array
63: {
64: return [];
65: }
66:
67: public function getConstantStrings(): array
68: {
69: return [];
70: }
71:
72: public function accepts(Type $type, bool $strictTypes): AcceptsResult
73: {
74: $isNumericString = $type->isNumericString();
75:
76: if ($isNumericString->yes()) {
77: return AcceptsResult::createYes();
78: }
79:
80: if ($type instanceof CompoundType) {
81: return $type->isAcceptedBy($this, $strictTypes);
82: }
83:
84: return new AcceptsResult($isNumericString, []);
85: }
86:
87: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
88: {
89: if ($type instanceof CompoundType) {
90: return $type->isSubTypeOf($this);
91: }
92:
93: if ($this->equals($type)) {
94: return IsSuperTypeOfResult::createYes();
95: }
96:
97: return new IsSuperTypeOfResult($type->isNumericString(), []);
98: }
99:
100: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
101: {
102: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
103: return $otherType->isSuperTypeOf($this);
104: }
105:
106: return (new IsSuperTypeOfResult($otherType->isNumericString(), []))
107: ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
108: }
109:
110: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
111: {
112: if ($acceptingType->isNonFalsyString()->yes()) {
113: return AcceptsResult::createMaybe();
114: }
115:
116: if ($acceptingType->isNonEmptyString()->yes()) {
117: return AcceptsResult::createYes();
118: }
119:
120: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
121: }
122:
123: public function equals(Type $type): bool
124: {
125: return $type instanceof self;
126: }
127:
128: public function describe(VerbosityLevel $level): string
129: {
130: return 'numeric-string';
131: }
132:
133: public function isOffsetAccessible(): TrinaryLogic
134: {
135: return TrinaryLogic::createYes();
136: }
137:
138: public function isOffsetAccessLegal(): TrinaryLogic
139: {
140: return TrinaryLogic::createYes();
141: }
142:
143: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
144: {
145: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
146: }
147:
148: public function getOffsetValueType(Type $offsetType): Type
149: {
150: if ($this->hasOffsetValueType($offsetType)->no()) {
151: return new ErrorType();
152: }
153:
154: return new StringType();
155: }
156:
157: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
158: {
159: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
160:
161: if ($stringOffset instanceof ErrorType) {
162: return $stringOffset;
163: }
164:
165: return $this;
166: }
167:
168: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
169: {
170: return $this;
171: }
172:
173: public function unsetOffset(Type $offsetType): Type
174: {
175: return new ErrorType();
176: }
177:
178: public function toNumber(): Type
179: {
180: return new UnionType([
181: $this->toInteger(),
182: $this->toFloat(),
183: ]);
184: }
185:
186: public function toAbsoluteNumber(): Type
187: {
188: return $this->toNumber()->toAbsoluteNumber();
189: }
190:
191: public function toInteger(): Type
192: {
193: return new IntegerType();
194: }
195:
196: public function toFloat(): Type
197: {
198: return new FloatType();
199: }
200:
201: public function toString(): Type
202: {
203: return $this;
204: }
205:
206: public function toArray(): Type
207: {
208: return new ConstantArrayType(
209: [new ConstantIntegerType(0)],
210: [$this],
211: [1],
212: isList: TrinaryLogic::createYes(),
213: );
214: }
215:
216: public function toArrayKey(): Type
217: {
218: return new UnionType([
219: new IntegerType(),
220: new IntersectionType([
221: new StringType(),
222: new AccessoryNumericStringType(),
223: ]),
224: ]);
225: }
226:
227: public function toCoercedArgumentType(bool $strictTypes): Type
228: {
229: if (!$strictTypes) {
230: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
231: }
232:
233: return $this;
234: }
235:
236: public function isNull(): TrinaryLogic
237: {
238: return TrinaryLogic::createNo();
239: }
240:
241: public function isConstantValue(): TrinaryLogic
242: {
243: return TrinaryLogic::createMaybe();
244: }
245:
246: public function isConstantScalarValue(): TrinaryLogic
247: {
248: return TrinaryLogic::createMaybe();
249: }
250:
251: public function getConstantScalarTypes(): array
252: {
253: return [];
254: }
255:
256: public function getConstantScalarValues(): array
257: {
258: return [];
259: }
260:
261: public function isTrue(): TrinaryLogic
262: {
263: return TrinaryLogic::createNo();
264: }
265:
266: public function isFalse(): TrinaryLogic
267: {
268: return TrinaryLogic::createNo();
269: }
270:
271: public function isBoolean(): TrinaryLogic
272: {
273: return TrinaryLogic::createNo();
274: }
275:
276: public function isFloat(): TrinaryLogic
277: {
278: return TrinaryLogic::createNo();
279: }
280:
281: public function isInteger(): TrinaryLogic
282: {
283: return TrinaryLogic::createNo();
284: }
285:
286: public function isString(): TrinaryLogic
287: {
288: return TrinaryLogic::createYes();
289: }
290:
291: public function isNumericString(): TrinaryLogic
292: {
293: return TrinaryLogic::createYes();
294: }
295:
296: public function isNonEmptyString(): TrinaryLogic
297: {
298: return TrinaryLogic::createYes();
299: }
300:
301: public function isNonFalsyString(): TrinaryLogic
302: {
303: return TrinaryLogic::createMaybe();
304: }
305:
306: public function isLiteralString(): TrinaryLogic
307: {
308: return TrinaryLogic::createMaybe();
309: }
310:
311: public function isLowercaseString(): TrinaryLogic
312: {
313: return TrinaryLogic::createMaybe();
314: }
315:
316: public function isUppercaseString(): TrinaryLogic
317: {
318: return TrinaryLogic::createMaybe();
319: }
320:
321: public function isClassString(): TrinaryLogic
322: {
323: return TrinaryLogic::createNo();
324: }
325:
326: public function getClassStringObjectType(): Type
327: {
328: return new ErrorType();
329: }
330:
331: public function getObjectTypeOrClassStringObjectType(): Type
332: {
333: return new ErrorType();
334: }
335:
336: public function isVoid(): TrinaryLogic
337: {
338: return TrinaryLogic::createNo();
339: }
340:
341: public function isScalar(): TrinaryLogic
342: {
343: return TrinaryLogic::createYes();
344: }
345:
346: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
347: {
348: if ($type->isNull()->yes()) {
349: return new ConstantBooleanType(false);
350: }
351:
352: if ($type->isString()->yes() && $type->isNumericString()->no()) {
353: return new ConstantBooleanType(false);
354: }
355:
356: return new BooleanType();
357: }
358:
359: public function traverse(callable $cb): Type
360: {
361: return $this;
362: }
363:
364: public function traverseSimultaneously(Type $right, callable $cb): Type
365: {
366: return $this;
367: }
368:
369: public function generalize(GeneralizePrecision $precision): Type
370: {
371: return new StringType();
372: }
373:
374: public function tryRemove(Type $typeToRemove): ?Type
375: {
376: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
377: return new IntersectionType([$this, new AccessoryNonFalsyStringType()]);
378: }
379:
380: return null;
381: }
382:
383: public function exponentiate(Type $exponent): Type
384: {
385: return new BenevolentUnionType([
386: new FloatType(),
387: new IntegerType(),
388: ]);
389: }
390:
391: public function getFiniteTypes(): array
392: {
393: return [];
394: }
395:
396: public function toPhpDocNode(): TypeNode
397: {
398: return new IdentifierTypeNode('numeric-string');
399: }
400:
401: public function hasTemplateOrLateResolvableType(): bool
402: {
403: return false;
404: }
405:
406: }
407: