@@ -149,8 +149,9 @@ private static function mul($x, $y, $scale, $pad)
149149 private static function div ($ x , $ y , $ scale , $ pad )
150150 {
151151 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 ' );
154155 }
155156
156157 $ temp = '1 ' . str_repeat ('0 ' , $ scale );
@@ -173,8 +174,9 @@ private static function div($x, $y, $scale, $pad)
173174 private static function mod ($ x , $ y , $ scale , $ pad )
174175 {
175176 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 ' );
178180 }
179181
180182 list ($ q ) = $ x ->divide ($ y );
@@ -261,7 +263,9 @@ private static function pow($x, $y, $scale, $pad)
261263 private static function powmod ($ x , $ e , $ n , $ scale , $ pad )
262264 {
263265 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 ' );
265269 }
266270 if ($ n [0 ] == '- ' ) {
267271 $ n = substr ($ n , 1 );
@@ -364,18 +368,11 @@ public static function __callStatic($name, $arguments)
364368 ];
365369 if (count ($ arguments ) < $ params [$ name ] - 1 ) {
366370 $ 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 " );
372372 }
373373 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 );
379376 }
380377 $ numbers = array_slice ($ arguments , 0 , $ params [$ name ] - 1 );
381378
@@ -390,6 +387,12 @@ public static function __callStatic($name, $arguments)
390387 $ ints = $ numbers ;
391388 $ numbers = [];
392389 $ names = ['base ' , 'exponent ' , 'modulus ' ];
390+ break ;
391+ case 'sqrt ' :
392+ $ names = ['num ' ];
393+ break ;
394+ default :
395+ $ names = ['num1 ' , 'num2 ' ];
393396 }
394397 foreach ($ ints as $ i => &$ int ) {
395398 if (!is_numeric ($ int )) {
@@ -398,10 +401,7 @@ public static function __callStatic($name, $arguments)
398401 $ pos = strpos ($ int , '. ' );
399402 if ($ pos !== false ) {
400403 $ 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 " );
405405 }
406406 }
407407 foreach ($ numbers as $ i => $ arg ) {
@@ -411,18 +411,18 @@ public static function __callStatic($name, $arguments)
411411 case is_string ($ arg ):
412412 case is_object ($ arg ) && method_exists ($ arg , '__toString ' ):
413413 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+ }
414417 break ;
415418 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 " );
421421 }
422422 }
423423 if (!isset (self ::$ scale )) {
424424 $ scale = ini_get ('bcmath.scale ' );
425- self ::$ scale = $ scale !== false ? intval ($ scale ) : 0 ;
425+ self ::$ scale = $ scale !== false ? max ( intval ($ scale), 0 ) : 0 ;
426426 }
427427 $ scale = isset ($ arguments [$ params [$ name ] - 1 ]) ? $ arguments [$ params [$ name ] - 1 ] : self ::$ scale ;
428428 switch (true ) {
@@ -431,15 +431,13 @@ public static function __callStatic($name, $arguments)
431431 case is_string ($ scale ) && preg_match ('#0-9\.# ' , $ scale [0 ]):
432432 break ;
433433 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 );
439437 }
440438 $ scale = (int ) $ scale ;
441439 if ($ scale < 0 ) {
442- $ scale = 0 ;
440+ throw new \ ValueError ( " bc $ name (): Argument # $ params [ $ name ] ( \$ scale) must be between 0 and 2147483647 " ) ;
443441 }
444442
445443 $ pad = 0 ;
0 commit comments