|
| 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\Reflection\FunctionReflection; |
| 8 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 9 | +use PHPStan\Type\ArrayType; |
| 10 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 11 | +use PHPStan\Type\Constant\ConstantFloatType; |
| 12 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 13 | +use PHPStan\Type\FloatType; |
| 14 | +use PHPStan\Type\IntegerType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\TypeCombinator; |
| 17 | +use PHPStan\Type\TypeUtils; |
| 18 | +use PHPStan\Type\UnionType; |
| 19 | + |
| 20 | +class RangeFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension |
| 21 | +{ |
| 22 | + |
| 23 | +private const RANGE_LENGTH_THRESHOLD = 50; |
| 24 | + |
| 25 | +public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 26 | +{ |
| 27 | +return $functionReflection->getName() === 'range'; |
| 28 | +} |
| 29 | + |
| 30 | +public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type |
| 31 | +{ |
| 32 | +if (count($functionCall->args) < 2) { |
| 33 | +return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 34 | +} |
| 35 | + |
| 36 | +$startType = $scope->getType($functionCall->args[0]->value); |
| 37 | +$endType = $scope->getType($functionCall->args[1]->value); |
| 38 | +$stepType = count($functionCall->args) >= 3 ? $scope->getType($functionCall->args[2]->value) : new ConstantIntegerType(1); |
| 39 | + |
| 40 | +$constantReturnTypes = []; |
| 41 | + |
| 42 | +$startConstants = TypeUtils::getConstantScalars($startType); |
| 43 | +foreach ($startConstants as $startConstant) { |
| 44 | +if (!$startConstant instanceof ConstantIntegerType && !$startConstant instanceof ConstantFloatType) { |
| 45 | +continue; |
| 46 | +} |
| 47 | + |
| 48 | +$endConstants = TypeUtils::getConstantScalars($endType); |
| 49 | +foreach ($endConstants as $endConstant) { |
| 50 | +if (!$endConstant instanceof ConstantIntegerType && !$endConstant instanceof ConstantFloatType) { |
| 51 | +continue; |
| 52 | +} |
| 53 | + |
| 54 | +$stepConstants = TypeUtils::getConstantScalars($stepType); |
| 55 | +foreach ($stepConstants as $stepConstant) { |
| 56 | +if (!$stepConstant instanceof ConstantIntegerType && !$stepConstant instanceof ConstantFloatType) { |
| 57 | +continue; |
| 58 | +} |
| 59 | + |
| 60 | +$rangeLength = (int) ceil(abs($startConstant->getValue() - $endConstant->getValue()) / $stepConstant->getValue()) + 1; |
| 61 | +if ($rangeLength > self::RANGE_LENGTH_THRESHOLD) { |
| 62 | +continue; |
| 63 | +} |
| 64 | + |
| 65 | +$keyTypes = []; |
| 66 | +$valueTypes = []; |
| 67 | + |
| 68 | +$rangeValues = range($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue()); |
| 69 | +foreach ($rangeValues as $key => $value) { |
| 70 | +$keyTypes[] = new ConstantIntegerType($key); |
| 71 | +$valueTypes[] = $scope->getTypeFromValue($value); |
| 72 | +} |
| 73 | + |
| 74 | +$constantReturnTypes[] = new ConstantArrayType($keyTypes, $valueTypes, $rangeLength); |
| 75 | +} |
| 76 | +} |
| 77 | +} |
| 78 | + |
| 79 | +if (count($constantReturnTypes) > 0) { |
| 80 | +return TypeCombinator::union(...$constantReturnTypes); |
| 81 | +} |
| 82 | + |
| 83 | +$startType = TypeUtils::generalizeType($startType); |
| 84 | +$endType = TypeUtils::generalizeType($endType); |
| 85 | +$stepType = TypeUtils::generalizeType($stepType); |
| 86 | + |
| 87 | +if ( |
| 88 | +$startType instanceof IntegerType |
| 89 | +&& $endType instanceof IntegerType |
| 90 | +&& $stepType instanceof IntegerType |
| 91 | +) { |
| 92 | +return new ArrayType(new IntegerType(), new IntegerType()); |
| 93 | +} |
| 94 | + |
| 95 | +if ( |
| 96 | +$startType instanceof FloatType |
| 97 | +|| $endType instanceof FloatType |
| 98 | +|| $stepType instanceof FloatType |
| 99 | +) { |
| 100 | +return new ArrayType(new IntegerType(), new FloatType()); |
| 101 | +} |
| 102 | + |
| 103 | +return new ArrayType(new IntegerType(), new UnionType([new IntegerType(), new FloatType()])); |
| 104 | +} |
| 105 | + |
| 106 | +} |
0 commit comments