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