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