From 8f20b9969fb431cebc329995782631e8b5ecf6c2 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 29 Oct 2019 12:49:42 +0100 Subject: [PATCH] Expect number argument in round() --- ext/standard/math.c | 11 +++---- ext/standard/tests/math/round_variation1.phpt | 30 ++++++++++--------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index c810c432f3..200c662dca 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -281,7 +281,7 @@ static double php_expm1(double x) } /* }}}*/ -/* {{{ proto int|float abs(int number) +/* {{{ proto int|float abs(int|float number) Return the absolute value of the number */ PHP_FUNCTION(abs) { @@ -345,7 +345,7 @@ PHP_FUNCTION(floor) } /* }}} */ -/* {{{ proto float|false round(float number [, int precision [, int mode]]) +/* {{{ proto float round(float number [, int precision [, int mode]]) Returns the number rounded to specified precision */ PHP_FUNCTION(round) { @@ -356,7 +356,7 @@ PHP_FUNCTION(round) double return_val; ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_ZVAL(value) + Z_PARAM_NUMBER(value) Z_PARAM_OPTIONAL Z_PARAM_LONG(precision) Z_PARAM_LONG(mode) @@ -373,7 +373,6 @@ PHP_FUNCTION(round) places = precision; #endif } - convert_scalar_to_number_ex(value); switch (Z_TYPE_P(value)) { case IS_LONG: @@ -389,9 +388,7 @@ PHP_FUNCTION(round) RETURN_DOUBLE(return_val); break; - default: - RETURN_FALSE; - break; + EMPTY_SWITCH_DEFAULT_CASE() } } /* }}} */ diff --git a/ext/standard/tests/math/round_variation1.phpt b/ext/standard/tests/math/round_variation1.phpt index 454a9129f4..774532f103 100644 --- a/ext/standard/tests/math/round_variation1.phpt +++ b/ext/standard/tests/math/round_variation1.phpt @@ -81,14 +81,18 @@ $inputs = array( // loop through each element of $inputs to check the behaviour of round() $iterator = 1; foreach($inputs as $input) { - echo "\n-- Iteration $iterator --\n"; - var_dump(round($input, 14)); - $iterator++; + echo "\n-- Iteration $iterator --\n"; + try { + var_dump(round($input, 14)); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } + $iterator++; }; fclose($fp); ?> ===Done=== ---EXPECTF-- +--EXPECT-- *** Testing round() : usage variations *** -- Iteration 1 -- @@ -140,27 +144,25 @@ float(1) float(0) -- Iteration 17 -- -float(0) +round() expects parameter 1 to be int or float, string given -- Iteration 18 -- -float(0) +round() expects parameter 1 to be int or float, string given -- Iteration 19 -- -bool(false) +round() expects parameter 1 to be int or float, array given -- Iteration 20 -- -float(0) +round() expects parameter 1 to be int or float, string given -- Iteration 21 -- -float(0) +round() expects parameter 1 to be int or float, string given -- Iteration 22 -- -float(0) +round() expects parameter 1 to be int or float, string given -- Iteration 23 -- - -Notice: Object of class classA could not be converted to number in %s on line %d -float(1) +round() expects parameter 1 to be int or float, object given -- Iteration 24 -- float(0) @@ -169,5 +171,5 @@ float(0) float(0) -- Iteration 26 -- -float(%f) +round() expects parameter 1 to be int or float, resource given ===Done=== -- 2.50.1