Skip to content

Commit d91c7df

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

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-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 ('0' === $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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,4 +663,61 @@ 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 PHP 8.0
691+
* @requires extension bcmath
692+
*/
693+
public function testBcDivModMalformedNumber()
694+
{
695+
$this->expectException(\ValueError::class);
696+
$this->expectExceptionMessage('Argument #1 ($num1) is not well-formed');
697+
698+
bcdivmod('a', '1');
699+
}
700+
701+
/**
702+
* @requires PHP 8.0
703+
* @requires extension bcmath
704+
*/
705+
public function testBcDivModMalformedNumber2()
706+
{
707+
$this->expectException(\ValueError::class);
708+
$this->expectExceptionMessage('Argument #2 ($num2) is not well-formed');
709+
710+
bcdivmod('1', 'a');
711+
}
712+
713+
public static function bcDivModProvider(): iterable
714+
{
715+
yield ['1', '1', null, ['1', '0']];
716+
yield ['1', '2', null, ['0', '1']];
717+
yield ['5', '2', null, ['2', '1']];
718+
yield ['5', '2', 0, ['2', '1']];
719+
yield ['5', '2', 1, ['2', '1.0']];
720+
yield ['5', '2', 2, ['2', '1.00']];
721+
yield ['7.2', '3', 2, ['2', '1.20']];
722+
}
666723
}

0 commit comments

Comments
 (0)