@@ -149,8 +149,9 @@ private static function mul($x, $y, $scale, $pad)
149
149
private static function div ($ x , $ y , $ scale , $ pad )
150
150
{
151
151
if ($ y == '0 ' ) {
152
- trigger_error ("bcdiv(): Division by zero " , E_USER_WARNING );
153
- return null ;
152
+ // < PHP 8.0 triggered a warning
153
+ // >= PHP 8.0 throws an exception
154
+ throw new \DivisionByZeroError ('Division by zero ' );
154
155
}
155
156
156
157
$ temp = '1 ' . str_repeat ('0 ' , $ scale );
@@ -173,8 +174,9 @@ private static function div($x, $y, $scale, $pad)
173
174
private static function mod ($ x , $ y , $ scale , $ pad )
174
175
{
175
176
if ($ y == '0 ' ) {
176
- trigger_error ("bcmod(): Division by zero " , E_USER_WARNING );
177
- return null ;
177
+ // < PHP 8.0 triggered a warning
178
+ // >= PHP 8.0 throws an exception
179
+ throw new \DivisionByZeroError ('Division by zero ' );
178
180
}
179
181
180
182
list ($ q ) = $ x ->divide ($ y );
@@ -261,7 +263,9 @@ private static function pow($x, $y, $scale, $pad)
261
263
private static function powmod ($ x , $ e , $ n , $ scale , $ pad )
262
264
{
263
265
if ($ e [0 ] == '- ' || $ n == '0 ' ) {
264
- return false ;
266
+ // < PHP 8.0 returned false
267
+ // >= PHP 8.0 throws an exception
268
+ throw new \ValueError ('bcpowmod(): Argument #2 ($exponent) must be greater than or equal to 0 ' );
265
269
}
266
270
if ($ n [0 ] == '- ' ) {
267
271
$ n = substr ($ n , 1 );
@@ -364,18 +368,11 @@ public static function __callStatic($name, $arguments)
364
368
];
365
369
if (count ($ arguments ) < $ params [$ name ] - 1 ) {
366
370
$ min = $ params [$ name ] - 1 ;
367
- trigger_error (
368
- "bc $ name() expects at least $ min parameters, " . func_num_args () . " given " ,
369
- E_USER_WARNING
370
- );
371
- return null ;
371
+ throw new \ArgumentCountError ("bc $ name() expects at least $ min parameters, " . func_num_args () . " given " );
372
372
}
373
373
if (count ($ arguments ) > $ params [$ name ]) {
374
- trigger_error (
375
- "bc $ name() expects at most {$ params [$ name ]} parameters, " . func_num_args () . " given " ,
376
- E_USER_WARNING
377
- );
378
- return null ;
374
+ $ str = "bc $ name() expects at most {$ params [$ name ]} parameters, " . func_num_args () . " given " ;
375
+ throw new \ArgumentCountError ($ str );
379
376
}
380
377
$ numbers = array_slice ($ arguments , 0 , $ params [$ name ] - 1 );
381
378
@@ -390,6 +387,12 @@ public static function __callStatic($name, $arguments)
390
387
$ ints = $ numbers ;
391
388
$ numbers = [];
392
389
$ names = ['base ' , 'exponent ' , 'modulus ' ];
390
+ break ;
391
+ case 'sqrt ' :
392
+ $ names = ['num ' ];
393
+ break ;
394
+ default :
395
+ $ names = ['num1 ' , 'num2 ' ];
393
396
}
394
397
foreach ($ ints as $ i => &$ int ) {
395
398
if (!is_numeric ($ int )) {
@@ -398,10 +401,7 @@ public static function __callStatic($name, $arguments)
398
401
$ pos = strpos ($ int , '. ' );
399
402
if ($ pos !== false ) {
400
403
$ int = substr ($ int , 0 , $ pos );
401
- trigger_error (
402
- "bc $ name(): non-zero scale in $ names [$ i ]" ,
403
- E_USER_WARNING
404
- );
404
+ throw new \ValueError ("bc $ name(): Argument #2 ( \$$ names [$ i ]) cannot have a fractional part " );
405
405
}
406
406
}
407
407
foreach ($ numbers as $ i => $ arg ) {
@@ -411,18 +411,18 @@ public static function __callStatic($name, $arguments)
411
411
case is_string ($ arg ):
412
412
case is_object ($ arg ) && method_exists ($ arg , '__toString ' ):
413
413
case is_null ($ arg ):
414
+ if (!is_bool ($ arg ) && !is_null ($ arg ) && !is_numeric ("$ arg " )) {
415
+ trigger_error ("bc $ name: bcmath function argument is not well-formed " , E_USER_WARNING );
416
+ }
414
417
break ;
415
418
default :
416
- trigger_error (
417
- "bc $ name() expects parameter $ i to be integer, " . gettype ($ arg ) . " given " ,
418
- E_USER_WARNING
419
- );
420
- return null ;
419
+ $ type = is_object ($ arg ) ? get_class ($ arg ) : gettype ($ arg );
420
+ throw new \TypeError ("bc $ name(): Argument # $ i ( \$$ names [$ i ]) must be of type string, $ type given " );
421
421
}
422
422
}
423
423
if (!isset (self ::$ scale )) {
424
424
$ scale = ini_get ('bcmath.scale ' );
425
- self ::$ scale = $ scale !== false ? intval ($ scale ) : 0 ;
425
+ self ::$ scale = $ scale !== false ? max ( intval ($ scale), 0 ) : 0 ;
426
426
}
427
427
$ scale = isset ($ arguments [$ params [$ name ] - 1 ]) ? $ arguments [$ params [$ name ] - 1 ] : self ::$ scale ;
428
428
switch (true ) {
@@ -431,15 +431,13 @@ public static function __callStatic($name, $arguments)
431
431
case is_string ($ scale ) && preg_match ('#0-9\.# ' , $ scale [0 ]):
432
432
break ;
433
433
default :
434
- trigger_error (
435
- "bc $ name() expects parameter {$ params [$ name ]} to be integer, " . gettype ($ scale ) . " given " ,
436
- E_USER_WARNING
437
- );
438
- return null ;
434
+ $ type = is_object ($ arg ) ? get_class ($ arg ) : gettype ($ arg );
435
+ $ str = "bc $ name(): Argument # $ params [$ name ] ( \$scale) must be of type ?int, string given " ;
436
+ throw new \TypeError ($ str );
439
437
}
440
438
$ scale = (int ) $ scale ;
441
439
if ($ scale < 0 ) {
442
- $ scale = 0 ;
440
+ throw new \ ValueError ( " bc $ name (): Argument # $ params [ $ name ] ( \$ scale) must be between 0 and 2147483647 " ) ;
443
441
}
444
442
445
443
$ pad = 0 ;
0 commit comments