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