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: if ($type instanceof CompoundType) {
75: return $type->isAcceptedBy($this, $strictTypes);
76: }
77:
78: return new AcceptsResult($type->isNumericString(), []);
79: }
80:
81: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
82: {
83: if ($type instanceof CompoundType) {
84: return $type->isSubTypeOf($this);
85: }
86:
87: if ($this->equals($type)) {
88: return IsSuperTypeOfResult::createYes();
89: }
90:
91: return new IsSuperTypeOfResult($type->isNumericString(), []);
92: }
93:
94: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
95: {
96: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
97: return $otherType->isSuperTypeOf($this);
98: }
99:
100: return (new IsSuperTypeOfResult($otherType->isNumericString(), []))
101: ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe());
102: }
103:
104: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
105: {
106: if ($acceptingType->isNonFalsyString()->yes()) {
107: return AcceptsResult::createMaybe();
108: }
109:
110: if ($acceptingType->isNonEmptyString()->yes()) {
111: return AcceptsResult::createYes();
112: }
113:
114: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
115: }
116:
117: public function equals(Type $type): bool
118: {
119: return $type instanceof self;
120: }
121:
122: public function describe(VerbosityLevel $level): string
123: {
124: return 'numeric-string';
125: }
126:
127: public function isOffsetAccessible(): TrinaryLogic
128: {
129: return TrinaryLogic::createYes();
130: }
131:
132: public function isOffsetAccessLegal(): TrinaryLogic
133: {
134: return TrinaryLogic::createYes();
135: }
136:
137: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
138: {
139: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
140: }
141:
142: public function getOffsetValueType(Type $offsetType): Type
143: {
144: if ($this->hasOffsetValueType($offsetType)->no()) {
145: return new ErrorType();
146: }
147:
148: return new StringType();
149: }
150:
151: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
152: {
153: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
154:
155: if ($stringOffset instanceof ErrorType) {
156: return $stringOffset;
157: }
158:
159: return $this;
160: }
161:
162: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
163: {
164: return $this;
165: }
166:
167: public function unsetOffset(Type $offsetType): Type
168: {
169: return new ErrorType();
170: }
171:
172: public function toNumber(): Type
173: {
174: return new UnionType([
175: $this->toInteger(),
176: $this->toFloat(),
177: ]);
178: }
179:
180: public function toAbsoluteNumber(): Type
181: {
182: return $this->toNumber()->toAbsoluteNumber();
183: }
184:
185: public function toInteger(): Type
186: {
187: return new IntegerType();
188: }
189:
190: public function toFloat(): Type
191: {
192: return new FloatType();
193: }
194:
195: public function toString(): Type
196: {
197: return $this;
198: }
199:
200: public function toArray(): Type
201: {
202: return new ConstantArrayType(
203: [new ConstantIntegerType(0)],
204: [$this],
205: [1],
206: [],
207: TrinaryLogic::createYes(),
208: );
209: }
210:
211: public function toArrayKey(): Type
212: {
213: return new IntegerType();
214: }
215:
216: public function isNull(): TrinaryLogic
217: {
218: return TrinaryLogic::createNo();
219: }
220:
221: public function isConstantValue(): TrinaryLogic
222: {
223: return TrinaryLogic::createMaybe();
224: }
225:
226: public function isConstantScalarValue(): TrinaryLogic
227: {
228: return TrinaryLogic::createMaybe();
229: }
230:
231: public function getConstantScalarTypes(): array
232: {
233: return [];
234: }
235:
236: public function getConstantScalarValues(): array
237: {
238: return [];
239: }
240:
241: public function isTrue(): TrinaryLogic
242: {
243: return TrinaryLogic::createNo();
244: }
245:
246: public function isFalse(): TrinaryLogic
247: {
248: return TrinaryLogic::createNo();
249: }
250:
251: public function isBoolean(): TrinaryLogic
252: {
253: return TrinaryLogic::createNo();
254: }
255:
256: public function isFloat(): TrinaryLogic
257: {
258: return TrinaryLogic::createNo();
259: }
260:
261: public function isInteger(): TrinaryLogic
262: {
263: return TrinaryLogic::createNo();
264: }
265:
266: public function isString(): TrinaryLogic
267: {
268: return TrinaryLogic::createYes();
269: }
270:
271: public function isNumericString(): TrinaryLogic
272: {
273: return TrinaryLogic::createYes();
274: }
275:
276: public function isNonEmptyString(): TrinaryLogic
277: {
278: return TrinaryLogic::createYes();
279: }
280:
281: public function isNonFalsyString(): TrinaryLogic
282: {
283: return TrinaryLogic::createMaybe();
284: }
285:
286: public function isLiteralString(): TrinaryLogic
287: {
288: return TrinaryLogic::createMaybe();
289: }
290:
291: public function isLowercaseString(): TrinaryLogic
292: {
293: return TrinaryLogic::createMaybe();
294: }
295:
296: public function isUppercaseString(): TrinaryLogic
297: {
298: return TrinaryLogic::createMaybe();
299: }
300:
301: public function isClassString(): TrinaryLogic
302: {
303: return TrinaryLogic::createNo();
304: }
305:
306: public function getClassStringObjectType(): Type
307: {
308: return new ErrorType();
309: }
310:
311: public function getObjectTypeOrClassStringObjectType(): Type
312: {
313: return new ErrorType();
314: }
315:
316: public function isVoid(): TrinaryLogic
317: {
318: return TrinaryLogic::createNo();
319: }
320:
321: public function isScalar(): TrinaryLogic
322: {
323: return TrinaryLogic::createYes();
324: }
325:
326: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
327: {
328: if ($type->isNull()->yes()) {
329: return new ConstantBooleanType(false);
330: }
331:
332: if ($type->isString()->yes() && $type->isNumericString()->no()) {
333: return new ConstantBooleanType(false);
334: }
335:
336: return new BooleanType();
337: }
338:
339: public function traverse(callable $cb): Type
340: {
341: return $this;
342: }
343:
344: public function traverseSimultaneously(Type $right, callable $cb): Type
345: {
346: return $this;
347: }
348:
349: public function generalize(GeneralizePrecision $precision): Type
350: {
351: return new StringType();
352: }
353:
354: public function tryRemove(Type $typeToRemove): ?Type
355: {
356: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
357: return TypeCombinator::intersect($this, new AccessoryNonFalsyStringType());
358: }
359:
360: return null;
361: }
362:
363: public function exponentiate(Type $exponent): Type
364: {
365: return new BenevolentUnionType([
366: new FloatType(),
367: new IntegerType(),
368: ]);
369: }
370:
371: public function getFiniteTypes(): array
372: {
373: return [];
374: }
375:
376: public function toPhpDocNode(): TypeNode
377: {
378: return new IdentifierTypeNode('numeric-string');
379: }
380:
381: }
382: