Skip to content

Commit 06683c6

Browse files
committed
Compare float bounds against PHP_INT_MAX + 1.0 in IntegerRangeType
The previous commit made createAllSmallerThan() and createAllGreaterThanOrEqualTo() treat a float bound `>= PHP_INT_MAX` as past the int range. That holds on 64-bit builds, where comparing with PHP_INT_MAX converts it to float and rounds it up to 2**63, but on 32-bit builds PHP_INT_MAX is exact as a float: createAllGreaterThanOrEqualTo( 2147483647.0) would return never although the int PHP_INT_MAX satisfies it, and createAllSmallerThan(2147483647.0) would return int instead of int<min, 2147483646>. Decide on the ceil() that is about to be cast instead, compared against PHP_INT_MAX + 1.0, the first float above PHP_INT_MAX, which is exact on both widths (2**63 and 2**31). On 64-bit builds every float at or above 2**52 is integral, so the result there is the same as with `>=`. This is the same code as proposed for 2.3.x in #6546. testCreateFromFloat's literals are 64-bit int bounds, so it is skipped on 32-bit hosts; testFloatBounds covers both widths.
1 parent 76aae52 commit 06683c6

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎src/Type/IntegerRangeType.php‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
class IntegerRangeType extends IntegerType implements CompoundType
3434
{
3535

36+
/** The first float above PHP_INT_MAX: 2^63 on 64-bit builds, 2^31 on 32-bit ones, exact on both. */
37+
private const FLOAT_ABOVE_INT_MAX = PHP_INT_MAX + 1.0;
38+
3639
private function __construct(private ?int $min, private ?int $max)
3740
{
3841
parent::__construct();
@@ -90,15 +93,19 @@ public static function createAllSmallerThan($value): Type
9093
return self::fromInterval(null, $value, -1);
9194
}
9295

93-
if ($value >= PHP_INT_MAX) {
96+
// decided on the ceil() about to be cast: comparing $value with PHP_INT_MAX
97+
// converts PHP_INT_MAX to float, which rounds it up to 2^63 on 64-bit builds
98+
// but keeps it exact on 32-bit ones, where 2147483647.0 is still in range
99+
$ceil = ceil($value);
100+
if ($ceil >= self::FLOAT_ABOVE_INT_MAX) {
94101
return new IntegerType();
95102
}
96103

97104
if ($value <= PHP_INT_MIN) {
98105
return new NeverType();
99106
}
100107

101-
return self::fromInterval(null, (int) ceil($value), -1);
108+
return self::fromInterval(null, (int) $ceil, -1);
102109
}
103110

104111
/**
@@ -160,11 +167,13 @@ public static function createAllGreaterThanOrEqualTo($value): Type
160167
return new IntegerType();
161168
}
162169

163-
if ($value >= PHP_INT_MAX) {
170+
// decided on the ceil() about to be cast, like createAllSmallerThan()
171+
$ceil = ceil($value);
172+
if ($ceil >= self::FLOAT_ABOVE_INT_MAX) {
164173
return new NeverType();
165174
}
166175

167-
return self::fromInterval((int) ceil($value), null);
176+
return self::fromInterval((int) $ceil, null);
168177
}
169178

170179
public function getMin(): ?int

‎tests/PHPStan/Type/IntegerRangeTypeTest.php‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
use PHPStan\Testing\PHPStanTestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7+
use RuntimeException;
8+
use function restore_error_handler;
9+
use function set_error_handler;
10+
use const PHP_INT_MAX;
11+
use const PHP_INT_MIN;
12+
use const PHP_INT_SIZE;
713

814
class IntegerRangeTypeTest extends PHPStanTestCase
915
{
@@ -56,6 +62,10 @@ public function testCreateFromFloat(
5662
string $expectedGreaterThanOrEqualTo,
5763
): void
5864
{
65+
if (PHP_INT_SIZE !== 8) {
66+
$this->markTestSkipped('The bounds are those of a 64-bit int.');
67+
}
68+
5969
$this->assertSame([
6070
'createAllSmallerThan' => $expectedSmallerThan,
6171
'createAllSmallerThanOrEqualTo' => $expectedSmallerThanOrEqualTo,
@@ -69,4 +79,58 @@ public function testCreateFromFloat(
6979
]);
7080
}
7181

82+
public static function dataFloatBounds(): iterable
83+
{
84+
$smallerThan = static fn (float $value): Type => IntegerRangeType::createAllSmallerThan($value);
85+
$smallerThanOrEqualTo = static fn (float $value): Type => IntegerRangeType::createAllSmallerThanOrEqualTo($value);
86+
$greaterThan = static fn (float $value): Type => IntegerRangeType::createAllGreaterThan($value);
87+
$greaterThanOrEqualTo = static fn (float $value): Type => IntegerRangeType::createAllGreaterThanOrEqualTo($value);
88+
89+
// PHP_INT_MAX + 1.0 is the first float above PHP_INT_MAX on any int width, and
90+
// (float) PHP_INT_MIN is exact on any int width
91+
$aboveMax = PHP_INT_MAX + 1.0;
92+
yield [$smallerThan, $aboveMax, 'int'];
93+
yield [$smallerThanOrEqualTo, $aboveMax, 'int'];
94+
yield [$greaterThan, $aboveMax, '*NEVER*'];
95+
yield [$greaterThanOrEqualTo, $aboveMax, '*NEVER*'];
96+
yield [$smallerThan, (float) PHP_INT_MIN, '*NEVER*'];
97+
yield [$smallerThanOrEqualTo, (float) PHP_INT_MIN, (string) PHP_INT_MIN];
98+
yield [$greaterThan, (float) PHP_INT_MIN, 'int<' . (PHP_INT_MIN + 1) . ', max>'];
99+
yield [$greaterThanOrEqualTo, (float) PHP_INT_MIN, 'int'];
100+
101+
// (float) PHP_INT_MAX is 2^63 on 64-bit builds (above every int) and exact on 32-bit
102+
// ones; so is PHP_INT_MAX - 0.5, which is PHP_INT_MAX - 1 + 0.5 on 32-bit builds
103+
$maxIsExact = PHP_INT_SIZE < 8;
104+
yield [$smallerThan, (float) PHP_INT_MAX, $maxIsExact ? 'int<min, ' . (PHP_INT_MAX - 1) . '>' : 'int'];
105+
yield [$smallerThanOrEqualTo, (float) PHP_INT_MAX, 'int'];
106+
yield [$greaterThan, (float) PHP_INT_MAX, '*NEVER*'];
107+
yield [$greaterThanOrEqualTo, (float) PHP_INT_MAX, $maxIsExact ? (string) PHP_INT_MAX : '*NEVER*'];
108+
yield [$smallerThan, PHP_INT_MAX - 0.5, $maxIsExact ? 'int<min, ' . (PHP_INT_MAX - 1) . '>' : 'int'];
109+
yield [$greaterThanOrEqualTo, PHP_INT_MAX - 0.5, $maxIsExact ? (string) PHP_INT_MAX : '*NEVER*'];
110+
111+
// between PHP_INT_MAX and the next int on 32-bit builds, 2^63 on 64-bit ones: its
112+
// ceil() is past the int range either way
113+
yield [$smallerThan, PHP_INT_MAX + 0.5, 'int'];
114+
yield [$greaterThanOrEqualTo, PHP_INT_MAX + 0.5, '*NEVER*'];
115+
116+
yield [$smallerThan, 2.5, 'int<min, 2>'];
117+
yield [$greaterThan, 2.5, 'int<3, max>'];
118+
}
119+
120+
/**
121+
* @param callable(float): Type $factory
122+
*/
123+
#[DataProvider('dataFloatBounds')]
124+
public function testFloatBounds(callable $factory, float $value, string $expected): void
125+
{
126+
set_error_handler(static function (int $errno, string $errstr): bool {
127+
throw new RuntimeException($errstr);
128+
});
129+
try {
130+
$this->assertSame($expected, $factory($value)->describe(VerbosityLevel::precise()));
131+
} finally {
132+
restore_error_handler();
133+
}
134+
}
135+
72136
}

0 commit comments

Comments
 (0)