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: | [], |
175: | TrinaryLogic::createYes(), |
176: | ); |
177: | } |
178: | |
179: | public function toArrayKey(): Type |
180: | { |
181: | return $this; |
182: | } |
183: | |
184: | public function isNull(): TrinaryLogic |
185: | { |
186: | return TrinaryLogic::createNo(); |
187: | } |
188: | |
189: | public function isTrue(): TrinaryLogic |
190: | { |
191: | return TrinaryLogic::createNo(); |
192: | } |
193: | |
194: | public function isFalse(): TrinaryLogic |
195: | { |
196: | return TrinaryLogic::createNo(); |
197: | } |
198: | |
199: | public function isBoolean(): TrinaryLogic |
200: | { |
201: | return TrinaryLogic::createNo(); |
202: | } |
203: | |
204: | public function isFloat(): TrinaryLogic |
205: | { |
206: | return TrinaryLogic::createNo(); |
207: | } |
208: | |
209: | public function isInteger(): TrinaryLogic |
210: | { |
211: | return TrinaryLogic::createNo(); |
212: | } |
213: | |
214: | public function isString(): TrinaryLogic |
215: | { |
216: | return TrinaryLogic::createYes(); |
217: | } |
218: | |
219: | public function isNumericString(): TrinaryLogic |
220: | { |
221: | return TrinaryLogic::createMaybe(); |
222: | } |
223: | |
224: | public function isNonEmptyString(): TrinaryLogic |
225: | { |
226: | return TrinaryLogic::createMaybe(); |
227: | } |
228: | |
229: | public function isNonFalsyString(): TrinaryLogic |
230: | { |
231: | return TrinaryLogic::createMaybe(); |
232: | } |
233: | |
234: | public function isLiteralString(): TrinaryLogic |
235: | { |
236: | return TrinaryLogic::createMaybe(); |
237: | } |
238: | |
239: | public function isLowercaseString(): TrinaryLogic |
240: | { |
241: | return TrinaryLogic::createMaybe(); |
242: | } |
243: | |
244: | public function isUppercaseString(): TrinaryLogic |
245: | { |
246: | return TrinaryLogic::createMaybe(); |
247: | } |
248: | |
249: | public function isClassString(): TrinaryLogic |
250: | { |
251: | return TrinaryLogic::createMaybe(); |
252: | } |
253: | |
254: | public function getClassStringObjectType(): Type |
255: | { |
256: | return new ObjectWithoutClassType(); |
257: | } |
258: | |
259: | public function getObjectTypeOrClassStringObjectType(): Type |
260: | { |
261: | return new ObjectWithoutClassType(); |
262: | } |
263: | |
264: | public function isScalar(): TrinaryLogic |
265: | { |
266: | return TrinaryLogic::createYes(); |
267: | } |
268: | |
269: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
270: | { |
271: | if ($type->isArray()->yes()) { |
272: | return new ConstantBooleanType(false); |
273: | } |
274: | |
275: | return new BooleanType(); |
276: | } |
277: | |
278: | public function hasMethod(string $methodName): TrinaryLogic |
279: | { |
280: | if ($this->isClassString()->yes()) { |
281: | return TrinaryLogic::createMaybe(); |
282: | } |
283: | return TrinaryLogic::createNo(); |
284: | } |
285: | |
286: | public function tryRemove(Type $typeToRemove): ?Type |
287: | { |
288: | if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '') { |
289: | return TypeCombinator::intersect($this, new AccessoryNonEmptyStringType()); |
290: | } |
291: | |
292: | if ($typeToRemove instanceof AccessoryNonEmptyStringType) { |
293: | return new ConstantStringType(''); |
294: | } |
295: | |
296: | return null; |
297: | } |
298: | |
299: | public function getFiniteTypes(): array |
300: | { |
301: | return []; |
302: | } |
303: | |
304: | public function exponentiate(Type $exponent): Type |
305: | { |
306: | return ExponentiateHelper::exponentiate($this, $exponent); |
307: | } |
308: | |
309: | public function toPhpDocNode(): TypeNode |
310: | { |
311: | return new IdentifierTypeNode('string'); |
312: | } |
313: | |
314: | } |
315: | |