Skip to content

Commit 89cb77e

Browse files
zonuexeclaude
andcommitted
Use RangeFunctionArgumentsHelper in the range() return type extension
The return type extension and the throw type extension share the checks of which steps range() rejects on which PHP versions. The inferred types stay the same. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 968f5f5 commit 89cb77e

1 file changed

Lines changed: 3 additions & 62 deletions

File tree

‎src/Type/Php/RangeFunctionReturnTypeExtension.php‎

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use PHPStan\Type\Type;
3030
use PHPStan\Type\TypeCombinator;
3131
use PHPStan\Type\UnionType;
32-
use ValueError;
3332
use function abs;
3433
use function count;
3534
use function floor;
@@ -39,7 +38,6 @@
3938
use function is_string;
4039
use function max;
4140
use function min;
42-
use function range;
4341

4442
#[AutowiredService]
4543
final class RangeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -100,7 +98,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
10098
$rangeLength = self::getRangeLength($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue());
10199
if ($rangeLength !== null && $rangeLength > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
102100
// without calling range() nothing rejects a negative step on an increasing range, which PHP 8.3 does
103-
if (self::isNegativeStepOnIncreasingRange($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue())) {
101+
if (RangeFunctionArgumentsHelper::isNegativeStepOnIncreasingRange($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue())) {
104102
if ($hasStricterRange->yes()) {
105103
$throwingCombinations++;
106104
continue;
@@ -114,15 +112,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
114112
return self::getLongRangeType($hasStricterRange, $startConstant, $endConstant, $stepConstant, $stepType, null);
115113
}
116114

117-
try {
118-
$rangeValues = @range($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue());
119-
} catch (ValueError) {
120-
$rangeValues = false;
121-
}
122-
123-
// PHP 7 reports an invalid step by returning false instead of throwing a ValueError
115+
$rangeValues = RangeFunctionArgumentsHelper::callRange($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue());
124116
if ($rangeValues === false) {
125-
$fails = self::failsOnAnalysedVersion($hasStricterRange, $startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue());
117+
$fails = RangeFunctionArgumentsHelper::rejects($hasStricterRange, $startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue(), true);
126118
if (!$fails->yes()) {
127119
// the analysed PHP version might accept the step, but only the runtime's verdict is known
128120
$hasUnknownCombination = true;
@@ -202,48 +194,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
202194
);
203195
}
204196

205-
/**
206-
* The constant arguments are folded by calling range() on the PHP version PHPStan
207-
* itself runs on, so a failure only says that the runtime rejects the step.
208-
* This checks whether the analysed PHP versions reject it as well.
209-
*/
210-
private static function failsOnAnalysedVersion(TrinaryLogic $hasStricterRange, int|float|string $start, int|float|string $end, int|float $step): TrinaryLogic
211-
{
212-
if (self::failsBeforePhp83($start, $end, $step)) {
213-
return TrinaryLogic::createYes();
214-
}
215-
216-
return $hasStricterRange;
217-
}
218-
219-
/**
220-
* PHP 7.x reported these steps with a warning and false, PHP 8.0-8.2 with a ValueError.
221-
*/
222-
private static function failsBeforePhp83(int|float|string $start, int|float|string $end, int|float $step): bool
223-
{
224-
if (is_string($start) || is_string($end)) {
225-
// how a string boundary was coerced before PHP 8.3 is not modelled here
226-
return false;
227-
}
228-
229-
$start = (float) $start;
230-
$end = (float) $end;
231-
if (!is_finite($start) || !is_finite($end)) {
232-
return false;
233-
}
234-
235-
if ($start === $end) {
236-
// a step of 0 was only rejected for integer boundaries in this case
237-
return false;
238-
}
239-
240-
// the sign of the step used to be ignored, so the step was only rejected
241-
// when it was 0 or wider than the range itself
242-
$step = abs((float) $step);
243-
244-
return $step === 0.0 || abs($end - $start) < $step;
245-
}
246-
247197
/**
248198
* The number of items range() creates for numeric arguments, or null when
249199
* only calling it tells - for a character range, which has at most 256
@@ -271,15 +221,6 @@ private static function getRangeLength(int|float|string $start, int|float|string
271221
return floor($length) + 1;
272222
}
273223

274-
private static function isNegativeStepOnIncreasingRange(int|float|string $start, int|float|string $end, int|float $step): bool
275-
{
276-
// a non-numeric string next to a number is 0
277-
$startNumber = is_numeric($start) ? (float) $start : 0.0;
278-
$endNumber = is_numeric($end) ? (float) $end : 0.0;
279-
280-
return $step < 0 && $startNumber < $endNumber;
281-
}
282-
283224
/**
284225
* @param non-empty-list<int|float|string>|null $rangeValues null when range() was not called
285226
*/

0 commit comments

Comments
 (0)