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