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