Avoid int casts when exporting floats

Avoid runtime warnings for large float values by removing the
int-cast-based check and using a simpler string-based check instead.
This commit is contained in:
USAMI Kenta 2026-04-15 19:06:20 +09:00 committed by Sebastian Bergmann
parent 40801a527c
commit 4d141ea52d
2 changed files with 4 additions and 3 deletions

View File

@ -10,8 +10,6 @@
namespace SebastianBergmann\Exporter;
use const COUNT_RECURSIVE;
use const PHP_INT_MAX;
use const PHP_INT_MIN;
use function assert;
use function bin2hex;
use function count;
@ -35,6 +33,7 @@ use function spl_object_id;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strpbrk;
use function strtr;
use function var_export;
use BackedEnum;
@ -368,7 +367,8 @@ final readonly class Exporter
ini_set('precision', $precisionBackup);
if ($value >= PHP_INT_MIN && $value <= PHP_INT_MAX && (string) (int) $value === $valueAsString) {
// Add '.0' only if decimals and scientific notation are absent.
if (strpbrk($valueAsString, '.E') === false) {
return $valueAsString . '.0';
}

View File

@ -87,6 +87,7 @@ final class ExporterTest extends TestCase
'float 1 - 2 / 3' => [1 - 2 / 3, '0.33333333333333337', 0],
'float 5.5E+123' => [5.5E+123, '5.5E+123', 0],
'float 5.5E-123' => [5.5E-123, '5.5E-123', 0],
'float 9.2233720368547758E+18' => [9.2233720368547758E+18, '9.223372036854776E+18', 0],
'float NAN' => [NAN, 'NAN', 0],
'float INF' => [INF, 'INF', 0],
'float -INF' => [-INF, '-INF', 0],