|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Php\PhpVersion; |
| 8 | +use PHPStan\Reflection\FunctionReflection; |
| 9 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 10 | +use PHPStan\ShouldNotHappenException; |
| 11 | +use PHPStan\Type\BooleanType; |
| 12 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 13 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 14 | +use PHPStan\Type\Constant\ConstantStringType; |
| 15 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 16 | +use PHPStan\Type\FloatType; |
| 17 | +use PHPStan\Type\IntegerRangeType; |
| 18 | +use PHPStan\Type\IntegerType; |
| 19 | +use PHPStan\Type\NeverType; |
| 20 | +use PHPStan\Type\StringType; |
| 21 | +use PHPStan\Type\Type; |
| 22 | +use PHPStan\Type\TypeCombinator; |
| 23 | +use PHPStan\Type\TypeUtils; |
| 24 | +use function array_map; |
| 25 | +use function array_merge; |
| 26 | +use function array_unique; |
| 27 | +use function count; |
| 28 | +use function in_array; |
| 29 | +use function max; |
| 30 | +use function mb_internal_encoding; |
| 31 | +use function mb_strlen; |
| 32 | +use function min; |
| 33 | +use function range; |
| 34 | +use function sort; |
| 35 | +use function sprintf; |
| 36 | +use function var_export; |
| 37 | + |
| 38 | +class MbStrlenFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 39 | +{ |
| 40 | + |
| 41 | + private const UNSUPPORTED_ENCODING = 'unsupported'; |
| 42 | + |
| 43 | + use MbFunctionsReturnTypeExtensionTrait; |
| 44 | + |
| 45 | + public function __construct(private PhpVersion $phpVersion) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 50 | + { |
| 51 | + return $functionReflection->getName() === 'mb_strlen'; |
| 52 | + } |
| 53 | + |
| 54 | + public function getTypeFromFunctionCall( |
| 55 | + FunctionReflection $functionReflection, |
| 56 | + FuncCall $functionCall, |
| 57 | + Scope $scope, |
| 58 | + ): Type |
| 59 | + { |
| 60 | + $args = $functionCall->getArgs(); |
| 61 | + $returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 62 | + if (count($args) === 0) { |
| 63 | + return $returnType; |
| 64 | + } |
| 65 | + |
| 66 | + $encodings = []; |
| 67 | + |
| 68 | + if (count($functionCall->getArgs()) === 1) { |
| 69 | + // there is a chance to get an unsupported encoding 'pass' or 'none' here on PHP 7.3-7.4 |
| 70 | + $encodings = [mb_internal_encoding()]; |
| 71 | + } elseif (count($functionCall->getArgs()) === 2) { // custom encoding is specified |
| 72 | + $encodings = array_map( |
| 73 | + static fn (ConstantStringType $t) => $t->getValue(), |
| 74 | + TypeUtils::getConstantStrings($scope->getType($functionCall->getArgs()[1]->value)), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + if (count($encodings) > 0) { |
| 79 | + for ($i = 0; $i < count($encodings); $i++) { |
| 80 | + if ($this->isSupportedEncoding($encodings[$i])) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + $encodings[$i] = self::UNSUPPORTED_ENCODING; |
| 84 | + } |
| 85 | + |
| 86 | + $encodings = array_unique($encodings); |
| 87 | + |
| 88 | + if (in_array(self::UNSUPPORTED_ENCODING, $encodings, true) && count($encodings) === 1) { |
| 89 | + if ($this->phpVersion->throwsOnInvalidMbStringEncoding()) { |
| 90 | + return new NeverType(); |
| 91 | + } |
| 92 | + return new ConstantBooleanType(false); |
| 93 | + } |
| 94 | + } else { // if there aren't encoding constants, use all available encodings |
| 95 | + $encodings = array_merge($this->getSupportedEncodings(), [self::UNSUPPORTED_ENCODING]); |
| 96 | + } |
| 97 | + |
| 98 | + $argType = $scope->getType($args[0]->value); |
| 99 | + |
| 100 | + if ($argType->isSuperTypeOf(new BooleanType())->yes()) { |
| 101 | + $constantScalars = TypeUtils::getConstantScalars(TypeCombinator::remove($argType, new BooleanType())); |
| 102 | + if (count($constantScalars) > 0) { |
| 103 | + $constantScalars[] = new ConstantBooleanType(true); |
| 104 | + $constantScalars[] = new ConstantBooleanType(false); |
| 105 | + } |
| 106 | + } else { |
| 107 | + $constantScalars = TypeUtils::getConstantScalars($argType); |
| 108 | + } |
| 109 | + |
| 110 | + $lengths = []; |
| 111 | + foreach ($constantScalars as $constantScalar) { |
| 112 | + $stringScalar = $constantScalar->toString(); |
| 113 | + if (!($stringScalar instanceof ConstantStringType)) { |
| 114 | + $lengths = []; |
| 115 | + break; |
| 116 | + } |
| 117 | + |
| 118 | + foreach ($encodings as $encoding) { |
| 119 | + if (!$this->isSupportedEncoding($encoding)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $length = mb_strlen($stringScalar->getValue(), $encoding); |
| 124 | + if ($length === false) { |
| 125 | + throw new ShouldNotHappenException(sprintf('Got false on a supported encoding %s and value %s', $encoding, var_export($stringScalar->getValue(), true))); |
| 126 | + } |
| 127 | + $lengths[] = $length; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $range = null; |
| 132 | + $isNonEmpty = $argType->isNonEmptyString(); |
| 133 | + $numeric = TypeCombinator::union(new IntegerType(), new FloatType()); |
| 134 | + if (count($lengths) > 0) { |
| 135 | + $lengths = array_unique($lengths); |
| 136 | + sort($lengths); |
| 137 | + if ($lengths === range(min($lengths), max($lengths))) { |
| 138 | + $range = IntegerRangeType::fromInterval(min($lengths), max($lengths)); |
| 139 | + } else { |
| 140 | + $range = TypeCombinator::union(...array_map(static fn ($l) => new ConstantIntegerType($l), $lengths)); |
| 141 | + } |
| 142 | + } elseif ((new BooleanType())->isSuperTypeOf($argType)->yes()) { |
| 143 | + $range = IntegerRangeType::fromInterval(0, 1); |
| 144 | + } elseif ( |
| 145 | + $isNonEmpty->yes() |
| 146 | + || $numeric->isSuperTypeOf($argType)->yes() |
| 147 | + || TypeCombinator::remove($argType, $numeric)->isNonEmptyString()->yes() |
| 148 | + ) { |
| 149 | + $range = IntegerRangeType::fromInterval(1, null); |
| 150 | + } elseif ((new StringType())->isSuperTypeOf($argType)->yes() && $isNonEmpty->no()) { |
| 151 | + $range = new ConstantIntegerType(0); |
| 152 | + } else { |
| 153 | + $range = TypeCombinator::remove( |
| 154 | + ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(), |
| 155 | + new ConstantBooleanType(false), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + if (!$this->phpVersion->throwsOnInvalidMbStringEncoding() && in_array(self::UNSUPPORTED_ENCODING, $encodings, true)) { |
| 160 | + return TypeCombinator::union($range, new ConstantBooleanType(false)); |
| 161 | + } |
| 162 | + return $range; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments