Skip to content

Commit d786000

Browse files
zonuexeclaude
andcommitted
Keep the range() throw point when the number of items does not fit a float
range(-1.0e308, 1.0e308) and range(0, 1, 1.0e-320) exceed the maximum array size by an infinite number of items. getRangeLength() returns null for them, and the extension then skipped the size check. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 82a83d9 commit d786000

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎src/Type/Php/RangeFunctionThrowTypeExtension.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function count;
1515
use function is_float;
1616
use function is_int;
17+
use function is_numeric;
1718
use function is_string;
1819

1920
/**
@@ -96,7 +97,12 @@ private static function getConstantValues(Type $type, bool $allowString): ?array
9697
private static function mightThrow(TrinaryLogic $hasStricterRange, int|float|string $start, int|float|string $end, int|float $step): bool
9798
{
9899
$length = RangeFunctionReturnTypeExtension::getRangeLength($start, $end, $step);
99-
if ($length !== null && $length >= self::RANGE_SIZE_LIMIT) {
100+
if ($length === null) {
101+
// only a character range has no number of items, any other one did not fit into a float
102+
if (!is_string($start) || !is_string($end) || is_numeric($start) || is_numeric($end)) {
103+
return true;
104+
}
105+
} elseif ($length >= self::RANGE_SIZE_LIMIT) {
100106
return true;
101107
}
102108

‎tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ public function testRangeThrowType(): void
15251525
],
15261526
[
15271527
'Dead catch - ValueError is never thrown in the try block.',
1528-
81,
1528+
88,
15291529
],
15301530
]);
15311531
}

‎tests/PHPStan/Rules/Exceptions/data/range-throw-type.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public function doFoo(int $i): void
7474
} catch (\ValueError $e) {
7575

7676
}
77+
78+
// PHP 8.3 compares an integral float step exactly, and 2 ** 60 exceeds the range up to 2 ** 60 - 1
79+
try {
80+
$a = range(0, 1152921504606846975, 1152921504606846976.0);
81+
} catch (\ValueError $e) {
82+
83+
}
7784
} else {
7885
// the sign of the step used to be ignored
7986
try {
@@ -95,6 +102,19 @@ public function doFoo(int $i): void
95102
} catch (\ValueError $e) {
96103

97104
}
105+
106+
// the number of items does not fit into a float, which exceeds every array size
107+
try {
108+
$a = range(-1.0e308, 1.0e308);
109+
} catch (\ValueError $e) {
110+
111+
}
112+
113+
try {
114+
$a = range(0, 1, 1.0e-320);
115+
} catch (\ValueError $e) {
116+
117+
}
98118
}
99119
}
100120

0 commit comments

Comments
 (0)