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