Skip to content

Commit 256fcb2

Browse files
committed
Bound the floats of a long range() by its boundaries
range() stops before it passes the end, so a list of floats too long to be folded into a constant array becomes list<float<min, max>> instead of list<float>. A string boundary keeps the floats unbounded.
1 parent 4566b0b commit 256fcb2

5 files changed

Lines changed: 38 additions & 17 deletions

File tree

‎src/Type/Php/RangeFunctionReturnTypeExtension.php‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Type\Constant\ConstantIntegerType;
2020
use PHPStan\Type\Constant\ConstantStringType;
2121
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
22+
use PHPStan\Type\FloatRangeType;
2223
use PHPStan\Type\FloatType;
2324
use PHPStan\Type\GeneralizePrecision;
2425
use PHPStan\Type\IntegerRangeType;
@@ -250,7 +251,7 @@ private static function getLongRangeType(
250251

251252
// before PHP 8.3 a float argument produced floats even when none of the arguments had a fractional
252253
// part, and even for two strings, of which only a numeric one kept its value
253-
$floatListType = self::getNonEmptyListOfType(new FloatType());
254+
$floatListType = self::getFloatListType($startConstant->getValue(), $endConstant->getValue());
254255
if ($hasStricterRange->no()) {
255256
return $floatListType;
256257
}
@@ -272,8 +273,6 @@ private static function getLongRangeItemsType(
272273
?array $rangeValues,
273274
): Type
274275
{
275-
$floatListType = self::getNonEmptyListOfType(new FloatType());
276-
277276
if ($rangeValues !== null) {
278277
// range() only ever returns values of a single type
279278
$firstValue = $rangeValues[0];
@@ -289,14 +288,14 @@ private static function getLongRangeItemsType(
289288
}
290289

291290
if (is_float($firstValue) || is_float($lastValue)) {
292-
return $floatListType;
291+
return self::getFloatListType($firstValue, $lastValue);
293292
}
294293

295294
$bounds = $startConstant instanceof ConstantIntegerType && $endConstant instanceof ConstantIntegerType
296295
? [$startConstant->getValue(), $endConstant->getValue()]
297296
: [$firstValue, $lastValue];
298297
} elseif ($startConstant instanceof ConstantFloatType || $endConstant instanceof ConstantFloatType) {
299-
return $floatListType;
298+
return self::getFloatListType($startConstant->getValue(), $endConstant->getValue());
300299
} elseif (!$startConstant instanceof ConstantIntegerType || !$endConstant instanceof ConstantIntegerType) {
301300
return self::getNonEmptyListOfType(
302301
TypeCombinator::union(
@@ -307,7 +306,7 @@ private static function getLongRangeItemsType(
307306
);
308307
} elseif (floor($stepConstant->getValue()) !== (float) $stepConstant->getValue()) {
309308
// a step with a fractional part produces floats
310-
return $floatListType;
309+
return self::getFloatListType($startConstant->getValue(), $endConstant->getValue());
311310
} else {
312311
$bounds = [$startConstant->getValue(), $endConstant->getValue()];
313312
}
@@ -329,6 +328,22 @@ private static function getLongRangeItemsType(
329328
return self::getNonEmptyListOfType(IntegerRangeType::fromInterval($low, $high), $maxKey);
330329
}
331330

331+
/**
332+
* range() stops before it passes the end, so the floats lie between the boundaries,
333+
* which a finite range length keeps finite. A string boundary leaves them unbounded.
334+
*/
335+
private static function getFloatListType(int|float|string $start, int|float|string $end): IntersectionType
336+
{
337+
if (is_string($start) || is_string($end)) {
338+
return self::getNonEmptyListOfType(new FloatType());
339+
}
340+
341+
return self::getNonEmptyListOfType(FloatRangeType::fromInterval(
342+
min((float) $start, (float) $end),
343+
max((float) $start, (float) $end),
344+
));
345+
}
346+
332347
private static function getNonEmptyListOfType(Type $type, ?int $maxKey = null): IntersectionType
333348
{
334349
return new IntersectionType([

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
assertType('array{2, 3, 4, 5}', range(2, 5, -1));
1212

1313
// a float step with no fractional part used to produce floats
14-
assertType('non-empty-list<float>', range(1, 200, 1.0));
14+
assertType('non-empty-list<float<1.0, 200.0>>', range(1, 200, 1.0));

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function doFoo(bool $flag): void
2525
assertType('non-empty-list<string>', range('a', 'z', -1));
2626

2727
// a float argument used to produce floats even without a fractional part
28-
assertType('non-empty-list<float>', range(1, 200, 1.0));
28+
assertType('non-empty-list<float<1.0, 200.0>>', range(1, 200, 1.0));
2929
assertType('non-empty-list<float>', range('A', 'z', 1.0));
3030
} else {
3131
// PHP 7 returns false for an invalid step, next to what the other combinations return
@@ -35,11 +35,14 @@ function doFoo(bool $flag): void
3535

3636
// every combination of the constant arguments contributes to the type
3737
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));
38+
assertType('non-empty-list<float<1.0, 100.0>|int<1, 100>>', range($flag ? 1.0 : 1, 100));
39+
assertType('non-empty-list<float<1.0, 100.0>|int<1, 100>>', range(1, 100, $flag ? 0.5 : 1));
4040

4141
// ranges longer than the threshold are generalized from the returned values
42-
assertType('non-empty-list<float>', range(1.0, 100.0));
42+
assertType('non-empty-list<float<1.0, 100.0>>', range(1.0, 100.0));
43+
// the floats stop at the last one before the end
44+
assertType('non-empty-list<float<-1.0, 100.0>>', range(100.0, -1.5));
45+
assertType('non-empty-list<float<0.0, 30.9>>', range(0, 31, 0.3));
4346
assertType('non-empty-list<literal-string&non-empty-string>', range('A', 'z'));
4447

4548
// a step that is neither an int nor a float is not folded at all, so nothing

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
/** @param int|float $floatOrInt */
88
function doFoo(int $i, float $f, $floatOrInt): void {
9-
assertType('non-empty-list<float>', range(1, 9, .01));
9+
assertType('non-empty-list<float<1.0, 9.0>>', range(1, 9, .01));
1010
assertType('array{1, 4, 7}', range(1, 9, 3));
1111

12-
assertType('non-empty-list<float>', range(1, 9999, .01));
12+
assertType('non-empty-list<float<1.0, 9999.0>>', range(1, 9999, .01));
1313
assertType('non-empty-list<int<1, 9999>>', range(1, 9999, 3));
1414

1515
assertType('non-empty-list<float|int>', range(1, 9999, $floatOrInt));

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function doFoo(): void
1010
assertType('non-empty-list<int<0, 100000000>>', range(0, 100000000));
1111
assertType('non-empty-list<int<-100000000, 0>>', range(0, -100000000));
1212
assertType('non-empty-list<int<0, 2000000000>>', range(0, 2000000000, 3));
13-
assertType('non-empty-list<float>', range(0, 100000000, 0.5));
13+
assertType('non-empty-list<float<0.0, 100000000.0>>', range(0, 100000000, 0.5));
1414
assertType('non-empty-list<int<0, 300>>', range(0, 300));
1515
}
1616

@@ -49,11 +49,14 @@ function doBar(): void
4949
assertType('non-empty-list<int<1, 1000>>', range(1, 1000, -1));
5050

5151
// a float argument used to produce floats even without a fractional part
52-
assertType('non-empty-list<float>', range(1, 1000, 1.0));
52+
assertType('non-empty-list<float<1.0, 1000.0>>', range(1, 1000, 1.0));
5353
}
5454

55-
assertType('non-empty-list<float>', range(1.0, 1000.0));
56-
assertType('non-empty-list<float>', range(0, 1000, 0.5));
55+
assertType('non-empty-list<float<1.0, 1000.0>>', range(1.0, 1000.0));
56+
assertType('non-empty-list<float<0.0, 1000.0>>', range(0, 1000, 0.5));
57+
assertType('non-empty-list<float<-1000.0, 1.5>>', range(1.5, -1000));
58+
// a string boundary leaves the floats unbounded
59+
assertType('non-empty-list<float>', range(1.5, '1000'));
5760
}
5861

5962
function doBaz(bool $flag): void

0 commit comments

Comments
 (0)