| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type; |
| 4: | |
| 5: | use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType; |
| 6: | use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
| 7: | use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
| 8: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 9: | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
| 10: | use PHPStan\Type\Accessory\AccessoryNumericStringType; |
| 11: | use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
| 12: | use PHPStan\Type\Constant\ConstantStringType; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | final class StringRefinement |
| 30: | { |
| 31: | |
| 32: | |
| 33: | public const EMPTY = 1; |
| 34: | |
| 35: | public const ZERO = 2; |
| 36: | |
| 37: | public const DECIMAL = 4; |
| 38: | |
| 39: | public const NUMERIC = 8; |
| 40: | |
| 41: | public const NUMERIC_LOWER = 16; |
| 42: | |
| 43: | public const NUMERIC_UPPER = 32; |
| 44: | |
| 45: | public const TEXT = 64; |
| 46: | |
| 47: | public const TEXT_LOWER = 128; |
| 48: | |
| 49: | public const TEXT_UPPER = 256; |
| 50: | |
| 51: | public const TEXT_MIXED = 512; |
| 52: | |
| 53: | public const NON_LITERAL_SHIFT = 10; |
| 54: | |
| 55: | public const VALUES = 1023; |
| 56: | public const LITERAL = self::VALUES; |
| 57: | public const ALL = self::VALUES | (self::VALUES << self::NON_LITERAL_SHIFT); |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public static function of(int $values): int |
| 66: | { |
| 67: | return $values | ($values << self::NON_LITERAL_SHIFT); |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public static function ofConstant(ConstantStringType $string): int |
| 77: | { |
| 78: | $value = $string->getValue(); |
| 79: | if ($value === '') { |
| 80: | return self::EMPTY; |
| 81: | } |
| 82: | if ($value === '0') { |
| 83: | return self::ZERO; |
| 84: | } |
| 85: | if ($string->isDecimalIntegerString()->yes()) { |
| 86: | return self::DECIMAL; |
| 87: | } |
| 88: | |
| 89: | $lower = $string->isLowercaseString()->yes(); |
| 90: | $upper = $string->isUppercaseString()->yes(); |
| 91: | if ($string->isNumericString()->yes()) { |
| 92: | return $lower ? ($upper ? self::NUMERIC : self::NUMERIC_LOWER) : self::NUMERIC_UPPER; |
| 93: | } |
| 94: | if ($lower) { |
| 95: | return $upper ? self::TEXT : self::TEXT_LOWER; |
| 96: | } |
| 97: | |
| 98: | return $upper ? self::TEXT_UPPER : self::TEXT_MIXED; |
| 99: | } |
| 100: | |
| 101: | |
| 102: | public static function isSingleValue(int $cells): bool |
| 103: | { |
| 104: | return $cells === self::EMPTY || $cells === self::ZERO; |
| 105: | } |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public static function toType(int $cells): ?Type |
| 112: | { |
| 113: | $accessories = []; |
| 114: | $held = self::ALL; |
| 115: | foreach (self::getAccessories() as $accessory) { |
| 116: | $accessoryCells = $accessory->getStringRefinement(); |
| 117: | if ($accessoryCells === null || ($cells & ~$accessoryCells) !== 0) { |
| 118: | continue; |
| 119: | } |
| 120: | |
| 121: | $accessories[] = $accessory; |
| 122: | $held &= $accessoryCells; |
| 123: | } |
| 124: | |
| 125: | if ($held !== $cells) { |
| 126: | return null; |
| 127: | } |
| 128: | |
| 129: | |
| 130: | $minimal = []; |
| 131: | foreach ($accessories as $i => $accessory) { |
| 132: | $accessoryCells = $accessory->getStringRefinement() ?? self::ALL; |
| 133: | foreach ($accessories as $j => $other) { |
| 134: | $otherCells = $other->getStringRefinement() ?? self::ALL; |
| 135: | if ($i !== $j && $otherCells !== $accessoryCells && ($otherCells & ~$accessoryCells) === 0) { |
| 136: | continue 2; |
| 137: | } |
| 138: | } |
| 139: | $minimal[] = $accessory; |
| 140: | } |
| 141: | |
| 142: | return TypeCombinator::intersect(new StringType(), ...$minimal); |
| 143: | } |
| 144: | |
| 145: | |
| 146: | private static function getAccessories(): array |
| 147: | { |
| 148: | return [ |
| 149: | new AccessoryNonEmptyStringType(), |
| 150: | new AccessoryNonFalsyStringType(), |
| 151: | new AccessoryNumericStringType(), |
| 152: | new AccessoryDecimalIntegerStringType(), |
| 153: | new AccessoryDecimalIntegerStringType(inverse: true), |
| 154: | new AccessoryLowercaseStringType(), |
| 155: | new AccessoryUppercaseStringType(), |
| 156: | new AccessoryLiteralStringType(), |
| 157: | ]; |
| 158: | } |
| 159: | |
| 160: | } |
| 161: | |