1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type; |
4: | |
5: | use PHPStan\Php\PhpVersion; |
6: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
7: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
8: | use PHPStan\Reflection\ReflectionProviderStaticAccessor; |
9: | use PHPStan\ShouldNotHappenException; |
10: | use PHPStan\TrinaryLogic; |
11: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
12: | use PHPStan\Type\Constant\ConstantArrayType; |
13: | use PHPStan\Type\Constant\ConstantBooleanType; |
14: | use PHPStan\Type\Constant\ConstantIntegerType; |
15: | use PHPStan\Type\Constant\ConstantStringType; |
16: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
17: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
18: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
19: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
20: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
21: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
22: | use PHPStan\Type\Traits\UndecidedBooleanTypeTrait; |
23: | use PHPStan\Type\Traits\UndecidedComparisonTypeTrait; |
24: | use function count; |
25: | |
26: | |
27: | class StringType implements Type |
28: | { |
29: | |
30: | use JustNullableTypeTrait; |
31: | use MaybeCallableTypeTrait; |
32: | use NonArrayTypeTrait; |
33: | use NonIterableTypeTrait; |
34: | use NonObjectTypeTrait; |
35: | use UndecidedBooleanTypeTrait; |
36: | use UndecidedComparisonTypeTrait; |
37: | use NonGenericTypeTrait; |
38: | use NonGeneralizableTypeTrait; |
39: | |
40: | |
41: | public function __construct() |
42: | { |
43: | } |
44: | |
45: | public function describe(VerbosityLevel $level): string |
46: | { |
47: | return 'string'; |
48: | } |
49: | |
50: | public function getConstantStrings(): array |
51: | { |
52: | return []; |
53: | } |
54: | |
55: | public function isOffsetAccessible(): TrinaryLogic |
56: | { |
57: | return TrinaryLogic::createYes(); |
58: | } |
59: | |
60: | public function isOffsetAccessLegal(): TrinaryLogic |
61: | { |
62: | return TrinaryLogic::createYes(); |
63: | } |
64: | |
65: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
66: | { |
67: | return $offsetType->isInteger()->and(TrinaryLogic::createMaybe()); |
68: | } |
69: | |
70: | public function getOffsetValueType(Type $offsetType): Type |
71: | { |
72: | if ($this->hasOffsetValueType($offsetType)->no()) { |
73: | return new ErrorType(); |
74: | } |
75: | |
76: | return new IntersectionType([ |
77: | new StringType(), |
78: | new AccessoryNonEmptyStringType(), |
79: | ]); |
80: | } |
81: | |
82: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
83: | { |
84: | if ($offsetType === null) { |
85: | return new ErrorType(); |
86: | } |
87: | |
88: | $valueStringType = $valueType->toString(); |
89: | if ($valueStringType instanceof ErrorType) { |
90: | return new ErrorType(); |
91: | } |
92: | |
93: | if ($offsetType->isInteger()->yes() || $offsetType instanceof MixedType) { |
94: | return new IntersectionType([ |
95: | new StringType(), |
96: | new AccessoryNonEmptyStringType(), |
97: | ]); |
98: | } |
99: | |
100: | return new ErrorType(); |
101: | } |
102: | |
103: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
104: | { |
105: | return $this; |
106: | } |
107: | |
108: | public function unsetOffset(Type $offsetType): Type |
109: | { |
110: | return new ErrorType(); |
111: | } |
112: | |
113: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
114: | { |
115: | if ($type instanceof self) { |
116: | return AcceptsResult::createYes(); |
117: | } |
118: | |
119: | if ($type instanceof CompoundType) { |
120: | return $type->isAcceptedBy($this, $strictTypes); |
121: | } |
122: | |
123: | $thatClassNames = $type->getObjectClassNames(); |
124: | if (count($thatClassNames) > 1) { |
125: | throw new ShouldNotHappenException(); |
126: | } |
127: | |
128: | if ($thatClassNames === [] || $strictTypes) { |
129: | return AcceptsResult::createNo(); |
130: | } |
131: | |
132: | $reflectionProvider = ReflectionProviderStaticAccessor::getInstance(); |
133: | if (!$reflectionProvider->hasClass($thatClassNames[0])) { |
134: | return AcceptsResult::createNo(); |
135: | } |
136: | |
137: | $typeClass = $reflectionProvider->getClass($thatClassNames[0]); |
138: | return AcceptsResult::createFromBoolean( |
139: | $typeClass->hasNativeMethod('__toString'), |
140: | ); |
141: | } |
142: | |
143: | public function toNumber(): Type |
144: | { |
145: | return new ErrorType(); |
146: | } |
147: | |
148: | public function toAbsoluteNumber(): Type |
149: | { |
150: | return new ErrorType(); |
151: | } |
152: | |
153: | public function toInteger(): Type |
154: | { |
155: | return new IntegerType(); |
156: | } |
157: | |
158: | public function toFloat(): Type |
159: | { |
160: | return new FloatType(); |
161: | } |
162: | |
163: | public function toString(): Type |
164: | { |
165: | return $this; |
166: | } |
167: | |
168: | public function toArray(): Type |
169: | { |
170: | return new ConstantArrayType( |
171: | [new ConstantIntegerType(0)], |
172: | [$this], |
173: | [1], |
174: | isList: TrinaryLogic::createYes(), |
175: | ); |
176: | } |
177: | |
178: | public function toArrayKey(): Type |
179: | { |
180: | return $this; |
181: | } |
182: | |
183: | public function toCoercedArgumentType(bool $strictTypes): Type |
184: | { |
185: | if (!$strictTypes) { |
186: | if ($this->isNumericString()->no()) { |
187: | return TypeCombinator::union($this, $this->toBoolean()); |
188: | } |
189: | return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean()); |
190: | } |
191: | |
192: | return $this; |
193: | } |
194: | |
195: | public function isNull(): TrinaryLogic |
196: | { |
197: | return TrinaryLogic::createNo(); |
198: | } |
199: | |
200: | public function isTrue(): TrinaryLogic |
201: | { |
202: | return TrinaryLogic::createNo(); |
203: | } |
204: | |
205: | public function isFalse(): TrinaryLogic |
206: | { |
207: | return TrinaryLogic::createNo(); |
208: | } |
209: | |
210: | public function isBoolean(): TrinaryLogic |
211: | { |
212: | return TrinaryLogic::createNo(); |
213: | } |
214: | |
215: | public function isFloat(): TrinaryLogic |
216: | { |
217: | return TrinaryLogic::createNo(); |
218: | } |
219: | |
220: | public function isInteger(): TrinaryLogic |
221: | { |
222: | return TrinaryLogic::createNo(); |
223: | } |
224: | |
225: | public function isString(): TrinaryLogic |
226: | { |
227: | return TrinaryLogic::createYes(); |
228: | } |
229: | |
230: | public function isNumericString(): TrinaryLogic |
231: | { |
232: | return TrinaryLogic::createMaybe(); |
233: | } |
234: | |
235: | public function isNonEmptyString(): TrinaryLogic |
236: | { |
237: | return TrinaryLogic::createMaybe(); |
238: | } |
239: | |
240: | public function isNonFalsyString(): TrinaryLogic |
241: | { |
242: | return TrinaryLogic::createMaybe(); |
243: | } |
244: | |
245: | public function isLiteralString(): TrinaryLogic |
246: | { |
247: | return TrinaryLogic::createMaybe(); |
248: | } |
249: | |
250: | public function isLowercaseString(): TrinaryLogic |
251: | { |
252: | return TrinaryLogic::createMaybe(); |
253: | } |
254: | |
255: | public function isUppercaseString(): TrinaryLogic |
256: | { |
257: | return TrinaryLogic::createMaybe(); |
258: | } |
259: | |
260: | public function isClassString(): TrinaryLogic |
261: | { |
262: | return TrinaryLogic::createMaybe(); |
263: | } |
264: | |
265: | public function getClassStringObjectType(): Type |
266: | { |
267: | return new ObjectWithoutClassType(); |
268: | } |
269: | |
270: | public function getObjectTypeOrClassStringObjectType(): Type |
271: | { |
272: | return new ObjectWithoutClassType(); |
273: | } |
274: | |
275: | public function isScalar(): TrinaryLogic |
276: | { |
277: | return TrinaryLogic::createYes(); |
278: | } |
279: | |
280: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
281: | { |
282: | if ($type->isArray()->yes()) { |
283: | return new ConstantBooleanType(false); |
284: | } |
285: | |
286: | return new BooleanType(); |
287: | } |
288: | |
289: | public function hasMethod(string $methodName): TrinaryLogic |
290: | { |
291: | if ($this->isClassString()->yes()) { |
292: | return TrinaryLogic::createMaybe(); |
293: | } |
294: | return TrinaryLogic::createNo(); |
295: | } |
296: | |
297: | public function tryRemove(Type $typeToRemove): ?Type |
298: | { |
299: | if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '') { |
300: | return TypeCombinator::intersect($this, new AccessoryNonEmptyStringType()); |
301: | } |
302: | |
303: | if ($typeToRemove instanceof AccessoryNonEmptyStringType) { |
304: | return new ConstantStringType(''); |
305: | } |
306: | |
307: | return null; |
308: | } |
309: | |
310: | public function getFiniteTypes(): array |
311: | { |
312: | return []; |
313: | } |
314: | |
315: | public function exponentiate(Type $exponent): Type |
316: | { |
317: | return ExponentiateHelper::exponentiate($this, $exponent); |
318: | } |
319: | |
320: | public function toPhpDocNode(): TypeNode |
321: | { |
322: | return new IdentifierTypeNode('string'); |
323: | } |
324: | |
325: | } |
326: | |