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