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