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\InstanceofDeprecated;
21: use PHPStan\Type\IntegerType;
22: use PHPStan\Type\IntersectionType;
23: use PHPStan\Type\IsSuperTypeOfResult;
24: use PHPStan\Type\NullType;
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\NonRemoveableTypeTrait;
33: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
34: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
35: use PHPStan\Type\Type;
36: use PHPStan\Type\TypeCombinator;
37: use PHPStan\Type\UnionType;
38: use PHPStan\Type\VerbosityLevel;
39:
40: #[InstanceofDeprecated(insteadUse: 'Type::isNonFalsyString()')]
41: class AccessoryNonFalsyStringType implements CompoundType, AccessoryType
42: {
43:
44: use MaybeCallableTypeTrait;
45: use NonArrayTypeTrait;
46: use NonObjectTypeTrait;
47: use NonIterableTypeTrait;
48: use TruthyBooleanTypeTrait;
49: use UndecidedComparisonCompoundTypeTrait;
50: use NonGenericTypeTrait;
51: use NonRemoveableTypeTrait;
52:
53: /** @api */
54: public function __construct()
55: {
56: }
57:
58: public function getReferencedClasses(): array
59: {
60: return [];
61: }
62:
63: public function getObjectClassNames(): array
64: {
65: return [];
66: }
67:
68: public function getObjectClassReflections(): array
69: {
70: return [];
71: }
72:
73: public function getConstantStrings(): array
74: {
75: return [];
76: }
77:
78: public function accepts(Type $type, bool $strictTypes): AcceptsResult
79: {
80: $isNonFalsyString = $type->isNonFalsyString();
81:
82: if ($isNonFalsyString->yes()) {
83: return AcceptsResult::createYes();
84: }
85:
86: if ($type instanceof CompoundType) {
87: return $type->isAcceptedBy($this, $strictTypes);
88: }
89:
90: return new AcceptsResult($isNonFalsyString, []);
91: }
92:
93: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
94: {
95: if ($type instanceof CompoundType) {
96: return $type->isSubTypeOf($this);
97: }
98:
99: if ($this->equals($type)) {
100: return IsSuperTypeOfResult::createYes();
101: }
102:
103: return new IsSuperTypeOfResult($type->isNonFalsyString(), []);
104: }
105:
106: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
107: {
108: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
109: return $otherType->isSuperTypeOf($this);
110: }
111:
112: if ($otherType instanceof AccessoryNonEmptyStringType) {
113: return IsSuperTypeOfResult::createYes();
114: }
115:
116: return new IsSuperTypeOfResult(
117: $otherType->isNonFalsyString()->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
118: [],
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-falsy-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: return new StringType();
159: }
160:
161: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
162: {
163: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
164:
165: if ($stringOffset instanceof ErrorType) {
166: return $stringOffset;
167: }
168:
169: if ($valueType->isNonFalsyString()->yes()) {
170: return $this;
171: }
172:
173: return new StringType();
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 AccessoryNonEmptyStringType()]);
194: }
195:
196: public function toAbsoluteNumber(): Type
197: {
198: return new ErrorType();
199: }
200:
201: public function toInteger(): Type
202: {
203: // Do not remove `0` since `(int) '00'` is still `0`.
204: return new IntegerType();
205: }
206:
207: public function toFloat(): Type
208: {
209: return new FloatType();
210: }
211:
212: public function toString(): Type
213: {
214: return $this;
215: }
216:
217: public function toArray(): Type
218: {
219: return new ConstantArrayType(
220: [new ConstantIntegerType(0)],
221: [$this],
222: [1],
223: isList: TrinaryLogic::createYes(),
224: );
225: }
226:
227: public function toArrayKey(): Type
228: {
229: return $this;
230: }
231:
232: public function toCoercedArgumentType(bool $strictTypes): Type
233: {
234: if (!$strictTypes) {
235: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
236: }
237:
238: return $this;
239: }
240:
241: public function isNull(): TrinaryLogic
242: {
243: return TrinaryLogic::createNo();
244: }
245:
246: public function isConstantValue(): TrinaryLogic
247: {
248: return TrinaryLogic::createMaybe();
249: }
250:
251: public function isConstantScalarValue(): TrinaryLogic
252: {
253: return TrinaryLogic::createMaybe();
254: }
255:
256: public function getConstantScalarTypes(): array
257: {
258: return [];
259: }
260:
261: public function getConstantScalarValues(): array
262: {
263: return [];
264: }
265:
266: public function isTrue(): TrinaryLogic
267: {
268: return TrinaryLogic::createNo();
269: }
270:
271: public function isFalse(): TrinaryLogic
272: {
273: return TrinaryLogic::createNo();
274: }
275:
276: public function isBoolean(): TrinaryLogic
277: {
278: return TrinaryLogic::createNo();
279: }
280:
281: public function isFloat(): TrinaryLogic
282: {
283: return TrinaryLogic::createNo();
284: }
285:
286: public function isInteger(): TrinaryLogic
287: {
288: return TrinaryLogic::createNo();
289: }
290:
291: public function isString(): TrinaryLogic
292: {
293: return TrinaryLogic::createYes();
294: }
295:
296: public function isNumericString(): TrinaryLogic
297: {
298: return TrinaryLogic::createMaybe();
299: }
300:
301: public function isDecimalIntegerString(): TrinaryLogic
302: {
303: return TrinaryLogic::createMaybe();
304: }
305:
306: public function isNonEmptyString(): TrinaryLogic
307: {
308: return TrinaryLogic::createYes();
309: }
310:
311: public function isNonFalsyString(): TrinaryLogic
312: {
313: return TrinaryLogic::createYes();
314: }
315:
316: public function isLiteralString(): TrinaryLogic
317: {
318: return TrinaryLogic::createMaybe();
319: }
320:
321: public function isLowercaseString(): TrinaryLogic
322: {
323: return TrinaryLogic::createMaybe();
324: }
325:
326: public function isClassString(): TrinaryLogic
327: {
328: return TrinaryLogic::createMaybe();
329: }
330:
331: public function isUppercaseString(): TrinaryLogic
332: {
333: return TrinaryLogic::createMaybe();
334: }
335:
336: public function getClassStringObjectType(): Type
337: {
338: return new ObjectWithoutClassType();
339: }
340:
341: public function getObjectTypeOrClassStringObjectType(): Type
342: {
343: return new ObjectWithoutClassType();
344: }
345:
346: public function isVoid(): TrinaryLogic
347: {
348: return TrinaryLogic::createNo();
349: }
350:
351: public function isScalar(): TrinaryLogic
352: {
353: return TrinaryLogic::createYes();
354: }
355:
356: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
357: {
358: $dominated = TypeCombinator::union(
359: new NullType(),
360: new ConstantBooleanType(false),
361: new ConstantStringType(''),
362: new ConstantArrayType([], []),
363: );
364: if ($dominated->isSuperTypeOf($type)->yes()) {
365: return new ConstantBooleanType(false);
366: }
367:
368: return new BooleanType();
369: }
370:
371: public function traverse(callable $cb): Type
372: {
373: return $this;
374: }
375:
376: public function traverseSimultaneously(Type $right, callable $cb): Type
377: {
378: return $this;
379: }
380:
381: public function generalize(GeneralizePrecision $precision): Type
382: {
383: return new StringType();
384: }
385:
386: public function exponentiate(Type $exponent): Type
387: {
388: return new BenevolentUnionType([
389: new FloatType(),
390: new IntegerType(),
391: ]);
392: }
393:
394: public function getFiniteTypes(): array
395: {
396: return [];
397: }
398:
399: public function getDefaultBaseType(): Type
400: {
401: return new StringType();
402: }
403:
404: public function toPhpDocNode(): TypeNode
405: {
406: return new IdentifierTypeNode('non-falsy-string');
407: }
408:
409: public function hasTemplateOrLateResolvableType(): bool
410: {
411: return false;
412: }
413:
414: }
415: