Skip to content

Commit be24db7

Browse files
committed
Merge branch '1.0' into 2.0
2 parents a080897 + dcdda00 commit be24db7

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

.travis.yml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
language: php
22

3-
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
7-
- 7.2
8-
- 7.3
9-
- 7.4
10-
- 8.0
11-
- nightly
12-
133
matrix:
4+
include:
5+
- php: 5.6
6+
dist: xenial
7+
- php: 7.0
8+
dist: xenial
9+
- php: 7.1
10+
dist: xenial
11+
- php: 7.2
12+
dist: xenial
13+
- php: 7.3
14+
dist: xenial
15+
- php: 7.4
16+
dist: xenial
17+
- php: 8.0
18+
dist: bionic
19+
- php: 8.1.0
20+
dist: bionic
21+
- php: nightly
22+
dist: bionic
1423
fast_finish: true
1524
allow_failures:
1625
- php: nightly

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phpseclib/bcmath_compat",
3-
"description": "PHP 5.x/7.x polyfill for bcmath extension",
3+
"description": "PHP 5.x-8.x polyfill for bcmath extension",
44
"keywords": [
55
"bcmath",
66
"math",
@@ -44,6 +44,6 @@
4444
"fix-style": "phpcbf src tests"
4545
},
4646
"provide": {
47-
"ext-bcmath": "8.0.0"
47+
"ext-bcmath": "8.1.0"
4848
}
4949
}

src/BCMath.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ private static function format($x, $scale, $pad)
7171
} elseif ($scale) {
7272
$temp[1] = str_repeat('0', $scale);
7373
}
74-
return $sign . rtrim(implode('.', $temp), '.');
74+
$result = rtrim(implode('.', $temp), '.');
75+
if ($sign == '-' && preg_match('#^0\.?0*$#', $result)) {
76+
$sign = '';
77+
}
78+
return $sign . $result;
7579
}
7680

7781
/**
@@ -133,9 +137,11 @@ private static function mul($x, $y, $scale, $pad)
133137
}
134138

135139
$z = $x->abs()->multiply($y->abs());
136-
$sign = (self::isNegative($x) ^ self::isNegative($y)) ? '-' : '';
140+
$result = self::format($z, $scale, 2 * $pad);
141+
142+
$sign = (self::isNegative($x) ^ self::isNegative($y)) && !preg_match('#^0\.?0*$#', $result) ? '-' : '';
137143

138-
return $sign . self::format($z, $scale, 2 * $pad);
144+
return $sign . $result;
139145
}
140146

141147
/**
@@ -402,19 +408,25 @@ public static function __callStatic($name, $arguments)
402408
}
403409
}
404410
foreach ($numbers as $i => $arg) {
411+
$num = $i + 1;
405412
switch (true) {
406413
case is_bool($arg):
407414
case is_numeric($arg):
408415
case is_string($arg):
409416
case is_object($arg) && method_exists($arg, '__toString'):
410-
case is_null($arg):
411-
if (!is_bool($arg) && !is_null($arg) && !is_numeric("$arg")) {
412-
trigger_error("bc$name: bcmath function argument is not well-formed", E_USER_WARNING);
417+
if (!is_bool($arg) && !is_numeric("$arg")) {
418+
throw new \ValueError("bc$name: bcmath function argument is not well-formed");
413419
}
414420
break;
421+
// PHP >= 8.1 has deprecated the passing of nulls to string parameters
422+
case is_null($arg):
423+
$error = "bc$name(): Passing null to parameter #$num (\$$names[$i]) of type string is deprecated";
424+
trigger_error($error, E_USER_DEPRECATED);
425+
break;
415426
default:
416427
$type = is_object($arg) ? get_class($arg) : gettype($arg);
417-
throw new \TypeError("bc$name(): Argument #$i (\$$names[$i]) must be of type string, $type given");
428+
$error = "bc$name(): Argument #$num (\$$names[$i]) must be of type string, $type given";
429+
throw new \TypeError($error);
418430
}
419431
}
420432
if (!isset(self::$scale)) {

tests/BCMathTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,10 @@ public function generateTwoParams()
3535
['2', '190', 3],
3636
['9', '0'],
3737
['0', '9'],
38-
[null, '9'],
38+
// this became deprecated in PHP 8.1
39+
//[null, '9'],
3940
['-0.0000005', '0', 3],
40-
/*
41-
there is some wonkyness with bcmul() in PHP 7.3 that this shim doesn't emulate:
42-
https://bugs.php.net/78071
43-
ie. instead of 0.000 you get -0.000 or vice versa. once a non-zero digit appears
44-
the outputs become consistent
45-
*/
46-
//['-0.0000005', '0.0000001', 3],
41+
['-0.0000005', '0.0000001', 3],
4742
['-0', '0'],
4843
['-0', '-0', 4]
4944
];
@@ -57,6 +52,11 @@ public function testAdd(...$params)
5752
{
5853
$a = bcadd(...$params);
5954
$b = BCMath::add(...$params);
55+
56+
if (version_compare(PHP_VERSION, '8.0.10') < 0 && preg_match('#^-0\.?0*$#', $a)) {
57+
$this->markTestSkipped('< PHP 8.0.10 made it so that you can\'t have -0 per http://bugs.php.net/78238');
58+
}
59+
6060
$this->assertSame($a, $b);
6161
}
6262

@@ -67,6 +67,11 @@ public function testSub(...$params)
6767
{
6868
$a = bcsub(...$params);
6969
$b = BCMath::sub(...$params);
70+
71+
if (version_compare(PHP_VERSION, '8.0.10') < 0 && preg_match('#^-0\.?0*$#', $a)) {
72+
$this->markTestSkipped('< PHP 8.0.10 made it so that you can\'t have -0 per http://bugs.php.net/78238');
73+
}
74+
7075
$this->assertSame($a, $b);
7176
}
7277

@@ -78,6 +83,11 @@ public function testMul(...$params)
7883
{
7984
$a = bcmul(...$params);
8085
$b = BCMath::mul(...$params);
86+
87+
if (version_compare(PHP_VERSION, '8.0.10') < 0 && preg_match('#^-0\.?0*$#', $a)) {
88+
$this->markTestSkipped('< PHP 8.0.10 made it so that you can\'t have -0 per http://bugs.php.net/78238');
89+
}
90+
8191
$this->assertSame($a, $b);
8292
}
8393

0 commit comments

Comments
 (0)