Skip to content

Commit 0b4bd65

Browse files
[PHP 8.4] Add bcdivmod
1 parent d139525 commit 0b4bd65

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/Php84/Php84.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,36 @@ private static function mb_internal_trim(string $regex, string $string, ?string
174174

175175
return mb_convert_encoding($string, $encoding, 'UTF-8');
176176
}
177+
178+
public static function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array
179+
{
180+
if (PHP_FLOAT_EPSILON < abs((float) $num2)) {
181+
throw new \DivisionByZeroError('Division by zero');
182+
}
183+
184+
if (!is_numeric($num1)) {
185+
if (class_exists(\ValueError::class)) {
186+
throw new \ValueError('Argument #1 ($num1) is not well-formed');
187+
}
188+
189+
trigger_error('Argument #1 ($num1) is not well-formed', E_USER_WARNING);
190+
191+
return null;
192+
}
193+
194+
if (!is_numeric($num2)) {
195+
if (class_exists(\ValueError::class)) {
196+
throw new \ValueError('Argument #2 ($num2) is not well-formed');
197+
}
198+
199+
trigger_error('Argument #2 ($num2) is not well-formed', E_USER_WARNING);
200+
201+
return null;
202+
}
203+
204+
return [
205+
\bcdiv($num1, $num2),
206+
\bcmod($num1, $num2, $scale ?? \bcscale() ?? ini_get('bcmath.scale') ?: 0),
207+
];
208+
}
177209
}

src/Php84/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ function mb_ltrim(string $string, ?string $characters = null, ?string $encoding
6666
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
6767
}
6868
}
69+
70+
if (extension_loaded('bcmath')) {
71+
if (!function_exists('bcdivmod')) {
72+
function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array { return p\Php84::bcdivmod($num1, $num2, $scale); }
73+
}
74+
}

tests/Php84/Php84Test.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,4 +663,71 @@ public static function fpowProvider(): iterable
663663
yield [NAN, -INF, NAN];
664664
yield [NAN, NAN, NAN];
665665
}
666+
667+
/**
668+
* @requires extension bcmath
669+
*
670+
* @covers \Symfony\Polyfill\Php84\Php84::bcdivmod
671+
*
672+
* @dataProvider bcDivModProvider
673+
*/
674+
public function testBcDivMod(string $num1, string $num2, ?int $scale, array $expected)
675+
{
676+
$this->assertSame($expected, bcdivmod($num1, $num2, $scale));
677+
}
678+
679+
/**
680+
* @requires extension bcmath
681+
*/
682+
public function testBcDivModDivideByZero()
683+
{
684+
$this->expectException(\DivisionByZeroError::class);
685+
686+
bcdivmod('1', '0');
687+
}
688+
689+
/**
690+
* @requires extension bcmath
691+
*/
692+
public function testBcDivModDivideByFloatingZero()
693+
{
694+
$this->expectException(\DivisionByZeroError::class);
695+
696+
bcdivmod('1', '0.00');
697+
}
698+
699+
/**
700+
* @requires PHP 8.0
701+
* @requires extension bcmath
702+
*/
703+
public function testBcDivModMalformedNumber()
704+
{
705+
$this->expectException(\ValueError::class);
706+
$this->expectExceptionMessage('Argument #1 ($num1) is not well-formed');
707+
708+
bcdivmod('a', '1');
709+
}
710+
711+
/**
712+
* @requires PHP 8.0
713+
* @requires extension bcmath
714+
*/
715+
public function testBcDivModMalformedNumber2()
716+
{
717+
$this->expectException(\ValueError::class);
718+
$this->expectExceptionMessage('Argument #2 ($num2) is not well-formed');
719+
720+
bcdivmod('1', 'a');
721+
}
722+
723+
public static function bcDivModProvider(): iterable
724+
{
725+
yield ['1', '1', null, ['1', '0']];
726+
yield ['1', '2', null, ['0', '1']];
727+
yield ['5', '2', null, ['2', '1']];
728+
yield ['5', '2', 0, ['2', '1']];
729+
yield ['5', '2', 1, ['2', '1.0']];
730+
yield ['5', '2', 2, ['2', '1.00']];
731+
yield ['7.2', '3', 2, ['2', '1.20']];
732+
}
666733
}

0 commit comments

Comments
 (0)