Skip to content

Commit 5483304

Browse files
zonuexeclaude
andcommitted
Keep every combination of range() arguments in the return type
A range longer than RANGE_LENGTH_THRESHOLD returned its type right away and dropped the other combinations, so range($f ? 'A' : 1, 'z') lost the ints of range(1, 'z') and range(1, $f ? 2 : 300, 5) lost the false it returns on PHP 7. Such a range now adds its type like any other. When a combination is skipped or undecided, the general type covers it and joins the types of the decided combinations, so a false from PHP 7 stays in the union. Before PHP 8.3 a float argument produced floats even for two strings, so range('A', 'z', 1.0) is a list of floats there instead of the characters the runtime returned. The test no longer asserts a non-empty list for range(1, 10, NAN) before PHP 8.3, which returns an empty array. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 89cb77e commit 5483304

4 files changed

Lines changed: 86 additions & 50 deletions

File tree

‎src/Type/Php/RangeFunctionReturnTypeExtension.php‎

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
109109
}
110110
}
111111

112-
return self::getLongRangeType($hasStricterRange, $startConstant, $endConstant, $stepConstant, $stepType, null);
112+
$constantReturnTypes[] = self::getLongRangeType($hasStricterRange, $startConstant, $endConstant, $stepConstant, $stepType, null);
113+
continue;
113114
}
114115

115116
$rangeValues = RangeFunctionArgumentsHelper::callRange($startConstant->getValue(), $endConstant->getValue(), $stepConstant->getValue());
@@ -127,8 +128,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
127128
}
128129

129130
if (count($rangeValues) > self::RANGE_LENGTH_THRESHOLD) {
130-
return self::getLongRangeType($hasStricterRange, $startConstant, $endConstant, $stepConstant, $stepType, $rangeValues);
131+
$constantReturnTypes[] = self::getLongRangeType($hasStricterRange, $startConstant, $endConstant, $stepConstant, $stepType, $rangeValues);
132+
continue;
131133
}
134+
132135
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
133136
foreach ($rangeValues as $value) {
134137
$arrayBuilder->setOffsetValueType(null, $scope->getTypeFromValue($value));
@@ -139,26 +142,29 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
139142
}
140143
}
141144

142-
if ($hasUnknownCombination) {
143-
$constantReturnTypes = [];
144-
}
145+
if (!$hasUnknownCombination && !$hasSkippedCombination) {
146+
if (count($constantReturnTypes) > 0) {
147+
return TypeCombinator::union(...$constantReturnTypes);
148+
}
145149

146-
if (count($constantReturnTypes) > 0) {
147-
return TypeCombinator::union(...$constantReturnTypes);
150+
// nothing is returned when every combination of the constant arguments throws
151+
if (
152+
$constantCombinations > 0
153+
&& $throwingCombinations === $constantCombinations
154+
&& $startType->isConstantScalarValue()->yes()
155+
&& $endType->isConstantScalarValue()->yes()
156+
&& $stepType->isConstantScalarValue()->yes()
157+
) {
158+
return new NeverType();
159+
}
148160
}
149161

150-
// nothing is returned when every combination of the constant arguments throws
151-
if (
152-
$constantCombinations > 0
153-
&& $throwingCombinations === $constantCombinations
154-
&& !$hasSkippedCombination
155-
&& $startType->isConstantScalarValue()->yes()
156-
&& $endType->isConstantScalarValue()->yes()
157-
&& $stepType->isConstantScalarValue()->yes()
158-
) {
159-
return new NeverType();
160-
}
162+
// the general type covers the combinations that could not be decided, the rest keep their own types
163+
return TypeCombinator::union(self::getGeneralType($startType, $endType, $stepType), ...$constantReturnTypes);
164+
}
161165

166+
private static function getGeneralType(Type $startType, Type $endType, Type $stepType): Type
167+
{
162168
$argType = TypeCombinator::union($startType, $endType);
163169
$isInteger = $argType->isInteger()->yes();
164170
$isStepInteger = $stepType->isInteger()->yes();
@@ -232,6 +238,41 @@ private static function getLongRangeType(
232238
Type $stepType,
233239
?array $rangeValues,
234240
): Type
241+
{
242+
$type = self::getLongRangeItemsType($startConstant, $endConstant, $stepConstant, $stepType, $rangeValues);
243+
if (
244+
$hasStricterRange->yes()
245+
|| (
246+
!$startConstant instanceof ConstantFloatType
247+
&& !$endConstant instanceof ConstantFloatType
248+
&& !$stepConstant instanceof ConstantFloatType
249+
)
250+
) {
251+
return $type;
252+
}
253+
254+
// before PHP 8.3 a float argument produced floats even when none of the arguments had a fractional
255+
// part, and even for two strings, which then counted as 0
256+
$floatListType = self::getNonEmptyListOfType(new FloatType());
257+
if ($hasStricterRange->no()) {
258+
return $floatListType;
259+
}
260+
261+
return TypeCombinator::union($type, $floatListType);
262+
}
263+
264+
/**
265+
* The type of the items the runtime returned, or without them what PHP 8.3 returns.
266+
*
267+
* @param non-empty-list<int|float|string>|null $rangeValues
268+
*/
269+
private static function getLongRangeItemsType(
270+
ConstantIntegerType|ConstantFloatType|ConstantStringType $startConstant,
271+
ConstantIntegerType|ConstantFloatType|ConstantStringType $endConstant,
272+
ConstantIntegerType|ConstantFloatType $stepConstant,
273+
Type $stepType,
274+
?array $rangeValues,
275+
): Type
235276
{
236277
$floatListType = self::getNonEmptyListOfType(new FloatType());
237278

@@ -252,6 +293,10 @@ private static function getLongRangeType(
252293
if (is_float($firstValue) || is_float($lastValue)) {
253294
return $floatListType;
254295
}
296+
297+
$bounds = $startConstant instanceof ConstantIntegerType && $endConstant instanceof ConstantIntegerType
298+
? [$startConstant->getValue(), $endConstant->getValue()]
299+
: [$firstValue, $lastValue];
255300
} elseif ($startConstant instanceof ConstantFloatType || $endConstant instanceof ConstantFloatType) {
256301
return $floatListType;
257302
} elseif (!$startConstant instanceof ConstantIntegerType || !$endConstant instanceof ConstantIntegerType) {
@@ -266,40 +311,11 @@ private static function getLongRangeType(
266311
// a step with a fractional part produces floats
267312
return $floatListType;
268313
} else {
269-
$firstValue = $startConstant->getValue();
270-
$lastValue = $endConstant->getValue();
271-
}
272-
273-
if ($startConstant instanceof ConstantIntegerType && $endConstant instanceof ConstantIntegerType) {
274-
$firstValue = $startConstant->getValue();
275-
$lastValue = $endConstant->getValue();
314+
$bounds = [$startConstant->getValue(), $endConstant->getValue()];
276315
}
277316

278317
// the sequence is monotonic, so the first and the last value are its bounds
279-
$intListType = self::getNonEmptyListOfType(IntegerRangeType::fromInterval(
280-
min($firstValue, $lastValue),
281-
max($firstValue, $lastValue),
282-
));
283-
284-
if (
285-
!$startConstant instanceof ConstantFloatType
286-
&& !$endConstant instanceof ConstantFloatType
287-
&& !$stepConstant instanceof ConstantFloatType
288-
) {
289-
return $intListType;
290-
}
291-
292-
// before PHP 8.3 a float argument produced floats even when none of
293-
// the arguments had a fractional part
294-
if ($hasStricterRange->yes()) {
295-
return $intListType;
296-
}
297-
298-
if ($hasStricterRange->no()) {
299-
return $floatListType;
300-
}
301-
302-
return TypeCombinator::union($intListType, $floatListType);
318+
return self::getNonEmptyListOfType(IntegerRangeType::fromInterval(min($bounds), max($bounds)));
303319
}
304320

305321
private static function getNonEmptyListOfType(Type $type): IntersectionType

‎tests/PHPStan/Analyser/nsrt/bug-10022.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ function doFoo(bool $flag): void
1818

1919
// an integral float step produces ints since PHP 8.3
2020
assertType('non-empty-list<int<1, 200>>', range(1, 200, 1.0));
21+
assertType('non-empty-list<literal-string&non-empty-string>', range('A', 'z', 1.0));
2122
} elseif (PHP_VERSION_ID >= 80000) {
2223
// the sign of the step used to be ignored
2324
assertType('non-empty-list<int>', range(2, 5, -1));
2425
assertType('non-empty-list<string>', range('a', 'z', -1));
25-
assertType('non-empty-list<float|int>', range(1, 10, NAN));
2626

2727
// a float argument used to produce floats even without a fractional part
2828
assertType('non-empty-list<float>', range(1, 200, 1.0));
29+
assertType('non-empty-list<float>', range('A', 'z', 1.0));
30+
} else {
31+
// PHP 7 returns false for an invalid step, next to what the other combinations return
32+
assertType('non-empty-list<int>|false', range(2, 5, $flag ? -1 : 0));
33+
assertType('non-empty-list<float|int>|false', range(2, 5, $flag ? 0 : true));
2934
}
3035

36+
// every combination of the constant arguments contributes to the type
37+
assertType('non-empty-list<0|1|(literal-string&non-empty-string)>', range($flag ? 'A' : 1, 'z'));
38+
assertType('non-empty-list<float|int<1, 100>>', range($flag ? 1.0 : 1, 100));
39+
assertType('non-empty-list<float|int<1, 100>>', range(1, 100, $flag ? 0.5 : 1));
40+
3141
// ranges longer than the threshold are generalized from the returned values
3242
assertType('non-empty-list<float>', range(1.0, 100.0));
3343
assertType('non-empty-list<literal-string&non-empty-string>', range('A', 'z'));

‎tests/PHPStan/Analyser/nsrt/range-array-count-limit.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ function doBar(): void
3333
assertType('non-empty-list<float>', range(1.0, 1000.0));
3434
assertType('non-empty-list<float>', range(0, 1000, 0.5));
3535
}
36+
37+
function doBaz(bool $flag): void
38+
{
39+
if (PHP_VERSION_ID >= 80000) {
40+
// PHP 8.2 accepts the first combination and PHP 8.3 rejects it
41+
assertType('non-empty-list<int>', range($flag ? 1 : 2000, 1000, -1));
42+
}
43+
}

‎tests/PHPStan/Analyser/nsrt/range-invalid-step.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ function doFoo(bool $flag): void
1212
assertType('*NEVER*', range(5, 6, 3));
1313
assertType('*NEVER*', range(1, 10, INF));
1414
assertType('array{6}', range($flag ? 5 : 6, 6, 3));
15+
assertType('non-empty-list<int<1, 300>>', range(1, $flag ? 2 : 300, 5));
1516
} else {
1617
// PHP 7 reports an invalid step with a warning and returns false instead
1718
assertType('false', range(2, 5, 0));
1819
assertType('false', range(5, 6, 3));
1920
assertType('false', range(1, 10, INF));
2021
assertType('array{6}|false', range($flag ? 5 : 6, 6, 3));
22+
assertType('non-empty-list<int<1, 300>>|false', range(1, $flag ? 2 : 300, 5));
2123
}
2224
}

0 commit comments

Comments
 (0)