|
11 | 11 | use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
|
12 | 12 | use PHPStan\Type\Accessory\NonEmptyArrayType;
|
13 | 13 | use PHPStan\Type\ArrayType;
|
| 14 | +use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
14 | 15 | use PHPStan\Type\Constant\ConstantBooleanType;
|
| 16 | +use PHPStan\Type\Constant\ConstantIntegerType; |
15 | 17 | use PHPStan\Type\Constant\ConstantStringType;
|
16 | 18 | use PHPStan\Type\DynamicFunctionReturnTypeExtension;
|
17 | 19 | use PHPStan\Type\IntegerRangeType;
|
|
24 | 26 | use PHPStan\Type\TypeCombinator;
|
25 | 27 | use PHPStan\Type\TypeUtils;
|
26 | 28 | use function count;
|
| 29 | +use const PHP_INT_MAX; |
27 | 30 |
|
28 | 31 | final class ExplodeFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
|
29 | 32 | {
|
30 | 33 |
|
| 34 | + private const CONST_ARRAY_LIMIT = 8; |
| 35 | + |
31 | 36 | public function __construct(private PhpVersion $phpVersion)
|
32 | 37 | {
|
33 | 38 | }
|
@@ -74,11 +79,21 @@ public function getTypeFromFunctionCall(
|
74 | 79 |
|
75 | 80 | $returnType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $returnValueType));
|
76 | 81 |
|
77 |
| - if ( |
78 |
| - !isset($args[2]) |
79 |
| - || IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($scope->getType($args[2]->value))->yes() |
80 |
| - ) { |
81 |
| - $returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType()); |
| 82 | + $limitType = isset($args[2]) ? $scope->getType($args[2]->value) : new ConstantIntegerType(PHP_INT_MAX); |
| 83 | + if (IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($limitType)->yes()) { |
| 84 | + $constantScalarTypes = $limitType->getConstantScalarTypes(); |
| 85 | + if (count($constantScalarTypes) === 1 && IntegerRangeType::fromInterval(0, self::CONST_ARRAY_LIMIT)->isSuperTypeOf($limitType)->yes()) { |
| 86 | + $limit = (int) $constantScalarTypes[0]->getValue() ?: 1; // 0 is treated as 1 |
| 87 | + |
| 88 | + $builder = ConstantArrayTypeBuilder::createEmpty(); |
| 89 | + for ($i = 0; $i < $limit; $i++) { |
| 90 | + $builder->setOffsetValueType(null, $returnValueType, $i !== 0); |
| 91 | + } |
| 92 | + |
| 93 | + $returnType = $builder->getArray(); |
| 94 | + } else { |
| 95 | + $returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType()); |
| 96 | + } |
82 | 97 | }
|
83 | 98 |
|
84 | 99 | if (!$this->phpVersion->throwsValueErrorForInternalFunctions() && $isEmptyString->maybe()) {
|
|
0 commit comments