Skip to content

Commit ec03c84

Browse files
zonuexeclaude
andauthored
Improve range() return type for invalid steps and long ranges (#6492)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent aa986e8 commit ec03c84

9 files changed

Lines changed: 619 additions & 33 deletions

‎src/Php/PhpVersions.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,13 @@ public function throwsValueErrorForInvalidRoundingMode(): TrinaryLogic
405405
return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
406406
}
407407

408+
/**
409+
* PHP 8.3 rejects a negative step on an increasing range and a NAN step, builds a character
410+
* range from two single bytes and no longer turns an integral float step into floats.
411+
*/
412+
public function hasStricterRangeFunction(): TrinaryLogic
413+
{
414+
return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
415+
}
416+
408417
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PHPStan\TrinaryLogic;
6+
use ValueError;
7+
use function abs;
8+
use function floor;
9+
use function is_finite;
10+
use function is_float;
11+
use function is_int;
12+
use function is_nan;
13+
use function is_numeric;
14+
use function is_string;
15+
use function ord;
16+
use function range;
17+
use function rtrim;
18+
use function strlen;
19+
use const PHP_INT_MIN;
20+
use const PHP_VERSION_ID;
21+
22+
/**
23+
* Tells whether range() rejects its arguments, which PHP 8 reports with a ValueError
24+
* and PHP 7 with a warning and false. A range with more items than an array can hold
25+
* is not covered here.
26+
*/
27+
final class RangeFunctionArgumentsHelper
28+
{
29+
30+
/** 2 ** 53, above which a float no longer holds every integer */
31+
private const PRECISE_INTEGER_LIMIT = 9007199254740992;
32+
33+
/** 2 ** 63, from which PHP 8.3 keeps an integral float step as a float */
34+
private const INTEGER_STEP_LIMIT = 9.2233720368547758E18;
35+
36+
/**
37+
* Calls range() on the PHP version PHPStan itself runs on.
38+
*
39+
* @return list<int|float|string>|false false when range() rejects the arguments
40+
*/
41+
public static function callRange(int|float|string $start, int|float|string $end, int|float $step): array|false
42+
{
43+
try {
44+
return @range($start, $end, $step);
45+
} catch (ValueError) {
46+
return false;
47+
}
48+
}
49+
50+
/**
51+
* @param TrinaryLogic $hasStricterRange whether the analysed PHP versions are 8.3 or newer
52+
* @param bool|null $runtimeRejects what calling range() on the runtime told, null when it was not called
53+
*/
54+
public static function rejects(
55+
TrinaryLogic $hasStricterRange,
56+
int|float|string $start,
57+
int|float|string $end,
58+
int|float $step,
59+
?bool $runtimeRejects,
60+
): TrinaryLogic
61+
{
62+
$results = [];
63+
if (!$hasStricterRange->no()) {
64+
$results[] = self::rejectsSincePhp83($start, $end, $step, $runtimeRejects);
65+
}
66+
if (!$hasStricterRange->yes()) {
67+
$results[] = self::rejectsBeforePhp83($start, $end, $step);
68+
}
69+
70+
return TrinaryLogic::extremeIdentity(...$results);
71+
}
72+
73+
private static function rejectsSincePhp83(int|float|string $start, int|float|string $end, int|float $step, ?bool $runtimeRejects): TrinaryLogic
74+
{
75+
// PHP 8.3 checks the step on its own before looking at the boundaries
76+
if (!is_finite((float) $step) || (float) $step === 0.0 || $step === PHP_INT_MIN) {
77+
return TrinaryLogic::createYes();
78+
}
79+
80+
// calling range() only tells about PHP 8.3 when PHPStan itself runs on PHP 8.3 or newer
81+
if (PHP_VERSION_ID < 80300) {
82+
$runtimeRejects = null;
83+
}
84+
85+
if (self::isNegativeStepOnIncreasingRange($start, $end, $step)) {
86+
return TrinaryLogic::createYes();
87+
}
88+
89+
if ($runtimeRejects !== null) {
90+
return TrinaryLogic::createFromBoolean($runtimeRejects);
91+
}
92+
93+
// the older rules stand in, but they compare floats where PHP 8.3 compares an integral float step exactly
94+
if (self::isImprecise($start) || self::isImprecise($end) || self::isImprecise($step)) {
95+
return TrinaryLogic::createMaybe();
96+
}
97+
98+
return self::rejectsBeforePhp83($start, $end, $step);
99+
}
100+
101+
/**
102+
* For numeric boundaries PHP 7 and 8.0-8.2 ignore the sign of the step and reject
103+
* one that is 0 or wider than the range itself.
104+
*/
105+
private static function rejectsBeforePhp83(int|float|string $start, int|float|string $end, int|float $step): TrinaryLogic
106+
{
107+
if (is_string($start) || is_string($end)) {
108+
// how a string boundary was coerced before PHP 8.3 is not modelled here
109+
return TrinaryLogic::createMaybe();
110+
}
111+
112+
if (
113+
is_int($start) && is_int($end) && is_int($step)
114+
&& (abs($start) > self::PRECISE_INTEGER_LIMIT || abs($end) > self::PRECISE_INTEGER_LIMIT || abs($step) > self::PRECISE_INTEGER_LIMIT)
115+
) {
116+
// range() compares integers exactly, which the floats below cannot do anymore
117+
return TrinaryLogic::createMaybe();
118+
}
119+
120+
$start = (float) $start;
121+
$end = (float) $end;
122+
$step = abs((float) $step);
123+
if (!is_finite($start) || !is_finite($end) || is_nan($step)) {
124+
return TrinaryLogic::createMaybe();
125+
}
126+
127+
if ($start === $end) {
128+
// a step of 0 was only rejected for integer boundaries in this case
129+
return TrinaryLogic::createMaybe();
130+
}
131+
132+
return TrinaryLogic::createFromBoolean($step === 0.0 || abs($end - $start) < $step);
133+
}
134+
135+
/**
136+
* PHP 8.3 rejects a negative step on an increasing range, while earlier versions ignored its sign.
137+
*/
138+
public static function isNegativeStepOnIncreasingRange(int|float|string $start, int|float|string $end, int|float $step): bool
139+
{
140+
if ($step >= 0) {
141+
return false;
142+
}
143+
144+
// with a float step PHP 8.3 compares numbers, in which only a digit keeps its value
145+
if (!self::isFloatStep($step) && self::isCharacter($start) && self::isCharacter($end)) {
146+
return ord($start[0]) < ord($end[0]);
147+
}
148+
149+
// an empty string or a character next to a number counts as 0
150+
return self::toNumber($start) < self::toNumber($end);
151+
}
152+
153+
/**
154+
* PHP 8.3 keeps a step with a fractional part or beyond the integer range as a float.
155+
*/
156+
private static function isFloatStep(int|float $step): bool
157+
{
158+
if (!is_float($step)) {
159+
return false;
160+
}
161+
162+
return floor($step) !== $step || abs($step) >= self::INTEGER_STEP_LIMIT;
163+
}
164+
165+
/**
166+
* PHP 8.3 builds a character range from two single bytes, which includes a digit,
167+
* and takes the first byte of a longer non-numeric string.
168+
*
169+
* @phpstan-assert-if-true non-empty-string $value
170+
*/
171+
private static function isCharacter(int|float|string $value): bool
172+
{
173+
return is_string($value) && $value !== '' && (strlen($value) === 1 || !self::isNumeric($value));
174+
}
175+
176+
/**
177+
* PHP 8 accepts whitespace after a numeric string, which is_numeric() on PHP 7.4 does not.
178+
*/
179+
private static function isNumeric(int|float|string $value): bool
180+
{
181+
return is_numeric($value) || is_numeric(rtrim($value, " \t\n\r\v\f"));
182+
}
183+
184+
private static function isImprecise(int|float|string $value): bool
185+
{
186+
return !is_string($value) && abs($value) > self::PRECISE_INTEGER_LIMIT;
187+
}
188+
189+
private static function toNumber(int|float|string $value): float
190+
{
191+
return self::isNumeric($value) ? (float) $value : 0.0;
192+
}
193+
194+
}

0 commit comments

Comments
 (0)