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