Skip to content

[PHP 8.4] Add bcdivmod #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Php84/Php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,36 @@ private static function mb_internal_trim(string $regex, string $string, ?string

return mb_convert_encoding($string, $encoding, 'UTF-8');
}

public static function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array
{
if (PHP_FLOAT_EPSILON < abs((float) $num2)) {
throw new \DivisionByZeroError('Division by zero');
}

if (!is_numeric($num1)) {
if (class_exists(\ValueError::class)) {
throw new \ValueError('Argument #1 ($num1) is not well-formed');
}

trigger_error('Argument #1 ($num1) is not well-formed', E_USER_WARNING);

return null;
}

if (!is_numeric($num2)) {
if (class_exists(\ValueError::class)) {
throw new \ValueError('Argument #2 ($num2) is not well-formed');
}

trigger_error('Argument #2 ($num2) is not well-formed', E_USER_WARNING);

return null;
}

return [
\bcdiv($num1, $num2),
\bcmod($num1, $num2, $scale ?? \bcscale() ?? ini_get('bcmath.scale') ?: 0),
];
}
}
6 changes: 6 additions & 0 deletions src/Php84/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ function mb_ltrim(string $string, ?string $characters = null, ?string $encoding
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Php84::mb_rtrim($string, $characters, $encoding); }
}
}

if (extension_loaded('bcmath')) {
if (!function_exists('bcdivmod')) {
function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array { return p\Php84::bcdivmod($num1, $num2, $scale); }
}
}
67 changes: 67 additions & 0 deletions tests/Php84/Php84Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,71 @@ public static function fpowProvider(): iterable
yield [NAN, -INF, NAN];
yield [NAN, NAN, NAN];
}

/**
* @requires extension bcmath
*
* @covers \Symfony\Polyfill\Php84\Php84::bcdivmod
*
* @dataProvider bcDivModProvider
*/
public function testBcDivMod(string $num1, string $num2, ?int $scale, array $expected)
{
$this->assertSame($expected, bcdivmod($num1, $num2, $scale));
}

/**
* @requires extension bcmath
*/
public function testBcDivModDivideByZero()
{
$this->expectException(\DivisionByZeroError::class);

bcdivmod('1', '0');
}

/**
* @requires extension bcmath
*/
public function testBcDivModDivideByFloatingZero()
{
$this->expectException(\DivisionByZeroError::class);

bcdivmod('1', '0.00');
}

/**
* @requires PHP 8.0
* @requires extension bcmath
*/
public function testBcDivModMalformedNumber()
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('Argument #1 ($num1) is not well-formed');

bcdivmod('a', '1');
}

/**
* @requires PHP 8.0
* @requires extension bcmath
*/
public function testBcDivModMalformedNumber2()
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('Argument #2 ($num2) is not well-formed');

bcdivmod('1', 'a');
}

public static function bcDivModProvider(): iterable
{
yield ['1', '1', null, ['1', '0']];
yield ['1', '2', null, ['0', '1']];
yield ['5', '2', null, ['2', '1']];
yield ['5', '2', 0, ['2', '1']];
yield ['5', '2', 1, ['2', '1.0']];
yield ['5', '2', 2, ['2', '1.00']];
yield ['7.2', '3', 2, ['2', '1.20']];
}
}
Loading