Skip to content

Commit 6e917db

Browse files
committed
PHP 8.5: Fix casting warnings in BigInteger::toInt() and BigNumber::of()
1 parent 66aa9e0 commit 6e917db

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/BigInteger.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function assert;
1919
use function bin2hex;
2020
use function chr;
21+
use function filter_var;
2122
use function hex2bin;
2223
use function in_array;
2324
use function intdiv;
@@ -32,6 +33,8 @@
3233
use function strtolower;
3334
use function substr;
3435

36+
use const FILTER_VALIDATE_INT;
37+
3538
/**
3639
* An arbitrary-size integer.
3740
*
@@ -979,9 +982,9 @@ public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::U
979982
#[Override]
980983
public function toInt(): int
981984
{
982-
$intValue = (int) $this->value;
985+
$intValue = filter_var($this->value, FILTER_VALIDATE_INT);
983986

984-
if ($this->value !== (string) $intValue) {
987+
if ($intValue === false) {
985988
throw IntegerOverflowException::toIntOverflow($this);
986989
}
987990

src/BigNumber.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
use function assert;
1818
use function is_float;
1919
use function is_int;
20+
use function is_nan;
2021
use function ltrim;
2122
use function preg_match;
2223
use function str_contains;
2324
use function str_repeat;
24-
use function strlen;
2525

26+
use function strlen;
2627
use const PHP_INT_MAX;
2728
use const PHP_INT_MIN;
29+
2830
use const PREG_UNMATCHED_AS_NULL;
2931

3032
/**
@@ -456,7 +458,11 @@ private static function _of(BigNumber|int|float|string $value): BigNumber
456458
}
457459

458460
if (is_float($value)) {
459-
$value = (string) $value;
461+
if (is_nan($value)) {
462+
$value = 'NAN';
463+
} else {
464+
$value = (string) $value;
465+
}
460466
}
461467

462468
if (str_contains($value, '/')) {

0 commit comments

Comments
 (0)