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