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