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: [],
205: TrinaryLogic::createYes(),
206: );
207: }
208:
209: public function toArrayKey(): Type
210: {
211: return $this;
212: }
213:
214: public function toCoercedArgumentType(bool $strictTypes): Type
215: {
216: if (!$strictTypes) {
217: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
218: }
219:
220: return $this;
221: }
222:
223: public function isNull(): TrinaryLogic
224: {
225: return TrinaryLogic::createNo();
226: }
227:
228: public function isConstantValue(): TrinaryLogic
229: {
230: return TrinaryLogic::createMaybe();
231: }
232:
233: public function isConstantScalarValue(): TrinaryLogic
234: {
235: return TrinaryLogic::createMaybe();
236: }
237:
238: public function getConstantScalarTypes(): array
239: {
240: return [];
241: }
242:
243: public function getConstantScalarValues(): array
244: {
245: return [];
246: }
247:
248: public function isTrue(): TrinaryLogic
249: {
250: return TrinaryLogic::createNo();
251: }
252:
253: public function isFalse(): TrinaryLogic
254: {
255: return TrinaryLogic::createNo();
256: }
257:
258: public function isBoolean(): TrinaryLogic
259: {
260: return TrinaryLogic::createNo();
261: }
262:
263: public function isFloat(): TrinaryLogic
264: {
265: return TrinaryLogic::createNo();
266: }
267:
268: public function isInteger(): TrinaryLogic
269: {
270: return TrinaryLogic::createNo();
271: }
272:
273: public function isString(): TrinaryLogic
274: {
275: return TrinaryLogic::createYes();
276: }
277:
278: public function isNumericString(): TrinaryLogic
279: {
280: return TrinaryLogic::createMaybe();
281: }
282:
283: public function isNonEmptyString(): TrinaryLogic
284: {
285: return TrinaryLogic::createMaybe();
286: }
287:
288: public function isNonFalsyString(): TrinaryLogic
289: {
290: return TrinaryLogic::createMaybe();
291: }
292:
293: public function isLiteralString(): TrinaryLogic
294: {
295: return TrinaryLogic::createMaybe();
296: }
297:
298: public function isLowercaseString(): TrinaryLogic
299: {
300: return TrinaryLogic::createMaybe();
301: }
302:
303: public function isUppercaseString(): TrinaryLogic
304: {
305: return TrinaryLogic::createYes();
306: }
307:
308: public function isClassString(): TrinaryLogic
309: {
310: return TrinaryLogic::createMaybe();
311: }
312:
313: public function getClassStringObjectType(): Type
314: {
315: return new ObjectWithoutClassType();
316: }
317:
318: public function getObjectTypeOrClassStringObjectType(): Type
319: {
320: return new ObjectWithoutClassType();
321: }
322:
323: public function hasMethod(string $methodName): TrinaryLogic
324: {
325: return TrinaryLogic::createMaybe();
326: }
327:
328: public function isVoid(): TrinaryLogic
329: {
330: return TrinaryLogic::createNo();
331: }
332:
333: public function isScalar(): TrinaryLogic
334: {
335: return TrinaryLogic::createYes();
336: }
337:
338: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
339: {
340: if (
341: $type->isString()->yes()
342: && $type->isUppercaseString()->no()
343: && ($type->isNumericString()->no() || $this->isNumericString()->no())
344: ) {
345: return new ConstantBooleanType(false);
346: }
347:
348: return new BooleanType();
349: }
350:
351: public function traverse(callable $cb): Type
352: {
353: return $this;
354: }
355:
356: public function traverseSimultaneously(Type $right, callable $cb): Type
357: {
358: return $this;
359: }
360:
361: public function generalize(GeneralizePrecision $precision): Type
362: {
363: return new StringType();
364: }
365:
366: public function exponentiate(Type $exponent): Type
367: {
368: return new BenevolentUnionType([
369: new FloatType(),
370: new IntegerType(),
371: ]);
372: }
373:
374: public function getFiniteTypes(): array
375: {
376: return [];
377: }
378:
379: public function toPhpDocNode(): TypeNode
380: {
381: return new IdentifierTypeNode('uppercase-string');
382: }
383:
384: }
385: