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