From 26558cc7960c540827f5216666878b3671879f21 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Wed, 21 Aug 2019 01:20:53 +0200 Subject: [PATCH] Promote warnings to errors in array_rand() --- ext/standard/array.c | 4 +- ext/standard/tests/array/array_rand.phpt | 54 ++++++++++++------- .../tests/array/array_rand_variation5.phpt | 48 ++++++++++------- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 70523d479ef4f..2fb423c1d6322 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -5823,7 +5823,7 @@ PHP_FUNCTION(array_rand) num_avail = zend_hash_num_elements(Z_ARRVAL_P(input)); if (num_avail == 0) { - php_error_docref(NULL, E_WARNING, "Array is empty"); + zend_throw_error(NULL, "Array is empty"); return; } @@ -5864,7 +5864,7 @@ PHP_FUNCTION(array_rand) } if (num_req <= 0 || num_req > num_avail) { - php_error_docref(NULL, E_WARNING, "Second argument has to be between 1 and the number of elements in the array"); + zend_throw_error(NULL, "Second argument has to be between 1 and the number of elements in the array"); return; } diff --git a/ext/standard/tests/array/array_rand.phpt b/ext/standard/tests/array/array_rand.phpt index 72999eda5fdd6..589572754829b 100644 --- a/ext/standard/tests/array/array_rand.phpt +++ b/ext/standard/tests/array/array_rand.phpt @@ -3,31 +3,47 @@ array_rand() tests --FILE-- getMessage() . "\n"; +} + +try { + var_dump(array_rand(array(), 0)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + var_dump(array_rand(array(1,2,3), 0)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + var_dump(array_rand(array(1,2,3), -1)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + var_dump(array_rand(array(1,2,3), 10)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + var_dump(array_rand(array(1,2,3), 3)); var_dump(array_rand(array(1,2,3), 2)); echo "Done\n"; ?> --EXPECTF-- -Warning: array_rand(): Array is empty in %s on line %d -NULL - -Warning: array_rand(): Array is empty in %s on line %d -NULL - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL +Array is empty +Array is empty +Second argument has to be between 1 and the number of elements in the array +Second argument has to be between 1 and the number of elements in the array +Second argument has to be between 1 and the number of elements in the array array(3) { [0]=> int(%d) diff --git a/ext/standard/tests/array/array_rand_variation5.phpt b/ext/standard/tests/array/array_rand_variation5.phpt index 30eb7d780104f..03e20d6e07a87 100644 --- a/ext/standard/tests/array/array_rand_variation5.phpt +++ b/ext/standard/tests/array/array_rand_variation5.phpt @@ -32,17 +32,36 @@ var_dump( array_rand($input, 1) ); // with valid $num_req value // with invalid num_req value echo"\n-- With num_req = 0 --\n"; -var_dump( array_rand($input, 0) ); // with $num_req=0 +try { + var_dump( array_rand($input, 0) ); // with $num_req=0 +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + echo"\n-- With num_req = -1 --\n"; -var_dump( array_rand($input, -1) ); // with $num_req=-1 +try { + var_dump( array_rand($input, -1) ); // with $num_req=-1 +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + echo"\n-- With num_req = -2 --\n"; -var_dump( array_rand($input, -2) ); // with $num_req=-2 -echo"\n-- With num_req more than number of members in 'input' array --\n"; -var_dump( array_rand($input, 13) ); // with $num_req=13 +try { + var_dump( array_rand($input, -2) ); // with $num_req=-2 +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} +echo"\n-- With num_req more than number of members in 'input' array --\n"; +try { + var_dump( array_rand($input, 13) ); // with $num_req=13 +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} -echo "Done"; ?> + +DONE --EXPECTF-- *** Testing array_rand() : with invalid values for 'req_num' *** @@ -53,22 +72,15 @@ int(%d) int(%d) -- With num_req = 0 -- - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL +Second argument has to be between 1 and the number of elements in the array -- With num_req = -1 -- - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL +Second argument has to be between 1 and the number of elements in the array -- With num_req = -2 -- - -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL +Second argument has to be between 1 and the number of elements in the array -- With num_req more than number of members in 'input' array -- +Second argument has to be between 1 and the number of elements in the array -Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d -NULL -Done +DONE