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