Skip to content

Commit 68463a5

Browse files
committed
PHP 8.5: Refactor exponent handling in BigNumber to prevent warning
1 parent 66aa9e0 commit 68463a5

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/BigNumber.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515

1616
use function array_shift;
1717
use function assert;
18+
use function filter_var;
1819
use function is_float;
1920
use function is_int;
2021
use function ltrim;
2122
use function preg_match;
2223
use function str_contains;
2324
use function str_repeat;
2425
use function strlen;
26+
use function substr;
27+
28+
use const FILTER_VALIDATE_INT;
2529

2630
use const PHP_INT_MAX;
2731
use const PHP_INT_MIN;
@@ -503,9 +507,23 @@ private static function _of(BigNumber|int|float|string $value): BigNumber
503507

504508
if ($point !== null || $exponent !== null) {
505509
$fractional ??= '';
506-
$exponent = ($exponent !== null) ? (int) $exponent : 0;
507510

508-
if ($exponent === PHP_INT_MIN || $exponent === PHP_INT_MAX) {
511+
if ($exponent !== null) {
512+
if (substr($exponent, 0, 1) === '-') {
513+
$exponent = ltrim(substr($exponent, 1), '0') ?: '0';
514+
$exponent = filter_var($exponent, FILTER_VALIDATE_INT);
515+
if ($exponent !== false) {
516+
$exponent = -$exponent;
517+
}
518+
} else {
519+
$exponent = ltrim($exponent, '+0') ?: '0';
520+
$exponent = filter_var($exponent, FILTER_VALIDATE_INT);
521+
}
522+
} else {
523+
$exponent = 0;
524+
}
525+
526+
if ($exponent === false || $exponent === PHP_INT_MIN || $exponent === PHP_INT_MAX) {
509527
throw new NumberFormatException('Exponent too large.');
510528
}
511529

0 commit comments

Comments
 (0)