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