Skip to content

Commit 82a83d9

Browse files
zonuexeclaude
andcommitted
Decide character ranges and large numbers in RangeFunctionArgumentsHelper
PHP 8.3 builds a character range from two single bytes, a digit included, so range('a', '5', -1) counts down and does not reject its negative step. isNegativeStepOnIncreasingRange() now compares the first bytes of such a range, and lets a step with a fractional part pass, which turns both characters into 0. Without a verdict from the runtime the PHP 8.3 part fell back to the older rules, which compare an integral float step as a float where PHP 8.3 compares it exactly. Numbers beyond 2 ** 53 now leave that part undecided. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 90ec645 commit 82a83d9

3 files changed

Lines changed: 110 additions & 17 deletions

File tree

‎src/Php/PhpVersions.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function supportsMaxMemoryLimit(): TrinaryLogic
7272
}
7373

7474
/**
75-
* PHP 8.3 rejects a negative or non-finite step, always builds a character range
76-
* from two string boundaries and no longer turns an integral float step into floats.
75+
* PHP 8.3 rejects a negative step on an increasing range and a NAN step, builds a character
76+
* range from two single bytes and no longer turns an integral float step into floats.
7777
*/
7878
public function hasStricterRangeFunction(): TrinaryLogic
7979
{

‎src/Type/Php/RangeFunctionArgumentsHelper.php‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use PHPStan\TrinaryLogic;
66
use ValueError;
77
use function abs;
8+
use function floor;
89
use function is_finite;
10+
use function is_float;
911
use function is_int;
1012
use function is_nan;
1113
use function is_numeric;
1214
use function is_string;
15+
use function ord;
1316
use function range;
17+
use function strlen;
1418
use const PHP_INT_MIN;
1519
use const PHP_VERSION_ID;
1620

@@ -74,27 +78,25 @@ private static function rejectsSincePhp83(int|float|string $start, int|float|str
7478
$runtimeRejects = null;
7579
}
7680

77-
if ($step < 0) {
78-
if (self::isNonNumericString($start) && self::isNonNumericString($end)) {
79-
// a character range, only calling range() tells its direction
80-
return $runtimeRejects === true ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe();
81-
}
82-
83-
if (self::isNegativeStepOnIncreasingRange($start, $end, $step)) {
84-
return TrinaryLogic::createYes();
85-
}
81+
if (self::isNegativeStepOnIncreasingRange($start, $end, $step)) {
82+
return TrinaryLogic::createYes();
8683
}
8784

8885
if ($runtimeRejects !== null) {
8986
return TrinaryLogic::createFromBoolean($runtimeRejects);
9087
}
9188

89+
// the older rules stand in, but they compare floats where PHP 8.3 compares an integral float step exactly
90+
if (self::isImprecise($start) || self::isImprecise($end) || self::isImprecise($step)) {
91+
return TrinaryLogic::createMaybe();
92+
}
93+
9294
return self::rejectsBeforePhp83($start, $end, $step);
9395
}
9496

9597
/**
96-
* PHP 7 and 8.0-8.2 ignore the sign of the step and reject one that is 0
97-
* or wider than the range itself.
98+
* For numeric boundaries PHP 7 and 8.0-8.2 ignore the sign of the step and reject
99+
* one that is 0 or wider than the range itself.
98100
*/
99101
private static function rejectsBeforePhp83(int|float|string $start, int|float|string $end, int|float $step): TrinaryLogic
100102
{
@@ -128,16 +130,40 @@ private static function rejectsBeforePhp83(int|float|string $start, int|float|st
128130

129131
/**
130132
* PHP 8.3 rejects a negative step on an increasing range, while earlier versions ignored its sign.
131-
* A non-numeric string next to a number counts as 0.
132133
*/
133134
public static function isNegativeStepOnIncreasingRange(int|float|string $start, int|float|string $end, int|float $step): bool
134135
{
135-
return $step < 0 && self::toNumber($start) < self::toNumber($end);
136+
if ($step >= 0) {
137+
return false;
138+
}
139+
140+
if (self::isCharacter($start) && self::isCharacter($end)) {
141+
// a step with a fractional part turns both characters into 0
142+
if (is_float($step) && floor($step) !== $step) {
143+
return false;
144+
}
145+
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 builds a character range from two single bytes, which includes a digit,
155+
* and takes the first byte of a longer non-numeric string.
156+
*
157+
* @phpstan-assert-if-true non-empty-string $value
158+
*/
159+
private static function isCharacter(int|float|string $value): bool
160+
{
161+
return is_string($value) && $value !== '' && (strlen($value) === 1 || !is_numeric($value));
136162
}
137163

138-
private static function isNonNumericString(int|float|string $value): bool
164+
private static function isImprecise(int|float|string $value): bool
139165
{
140-
return is_string($value) && !is_numeric($value);
166+
return !is_string($value) && abs($value) > self::PRECISE_INTEGER_LIMIT;
141167
}
142168

143169
private static function toNumber(int|float|string $value): float
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PHPStan\TrinaryLogic;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class RangeFunctionArgumentsHelperTest extends TestCase
10+
{
11+
12+
/**
13+
* @return iterable<array{int|float|string, int|float|string, int|float, bool}>
14+
*/
15+
public static function dataIsNegativeStepOnIncreasingRange(): iterable
16+
{
17+
yield [1, 5, -1, true];
18+
yield [5, 1, -1, false];
19+
yield [5, 5, -1, false];
20+
yield [1, 5, 1, false];
21+
22+
// two single bytes form a character range
23+
yield ['a', 'z', -1, true];
24+
yield ['z', 'a', -1, false];
25+
yield ['a', '5', -1, false];
26+
yield ['5', 'a', -1, true];
27+
yield ['ab', 'z', -1, true];
28+
yield ['a', 'z', -1.0, true];
29+
30+
// a step with a fractional part turns both characters into 0
31+
yield ['a', 'z', -0.5, false];
32+
33+
// an empty string or a character next to a number is 0
34+
yield ['', 'a', -1, false];
35+
yield ['a', '12', -1, true];
36+
yield ['a', 5, -1, true];
37+
}
38+
39+
#[DataProvider('dataIsNegativeStepOnIncreasingRange')]
40+
public function testIsNegativeStepOnIncreasingRange(int|float|string $start, int|float|string $end, int|float $step, bool $expected): void
41+
{
42+
$this->assertSame($expected, RangeFunctionArgumentsHelper::isNegativeStepOnIncreasingRange($start, $end, $step));
43+
}
44+
45+
/**
46+
* @return iterable<array{int|float|string, int|float|string, int|float, TrinaryLogic}>
47+
*/
48+
public static function dataRejectsSincePhp83WithoutCallingRange(): iterable
49+
{
50+
yield [1, 10, 2, TrinaryLogic::createNo()];
51+
yield [1, 10, 20, TrinaryLogic::createYes()];
52+
53+
// PHP 8.3 compares an integral float step exactly, which a float cannot do above 2 ** 53
54+
yield [0, 1152921504606846975, 1152921504606846976.0, TrinaryLogic::createMaybe()];
55+
yield [1, 9007199254740993, 9007199254740992.0, TrinaryLogic::createMaybe()];
56+
}
57+
58+
#[DataProvider('dataRejectsSincePhp83WithoutCallingRange')]
59+
public function testRejectsSincePhp83WithoutCallingRange(int|float|string $start, int|float|string $end, int|float $step, TrinaryLogic $expected): void
60+
{
61+
$this->assertSame(
62+
$expected->describe(),
63+
RangeFunctionArgumentsHelper::rejects(TrinaryLogic::createYes(), $start, $end, $step, null)->describe(),
64+
);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)