com php-src: Promote warnings to errors in array_rand() : ext/standard/array.c ext/standard/basic_functions .stub.php
ext/standard/tests/array/array_rand.phpt ext/standard/tests/array/array_rand_variation5.phpt

From: Date: Mon, 26 Aug 2019 11:31:02 +0000
Subject: com php-src: Promote warnings to errors in array_rand() : ext/standard/array.c ext/standard/basic_functions .stub.php
ext/standard/tests/array/array_rand.phpt ext/standard/tests/array/array_rand_variation5.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-108058@lists.php.net to get a copy of this message
Commit: c1c8538f952ff8cc4358fc988d5e9b11dcfbf6e6 Author: George Peter Banyard <girgias@php.net> Wed, 21 Aug 2019 01:20:53 +0200 Parents: 0038db22510cce37b24491c266123ba5b1e93953 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=c1c8538f952ff8cc4358fc988d5e9b11dcfbf6e6 Log: Promote warnings to errors in array_rand() Changed paths: M ext/standard/array.c M ext/standard/basic_functions.stub.php M ext/standard/tests/array/array_rand.phpt M ext/standard/tests/array/array_rand_variation5.phpt Diff: diff --git a/ext/standard/array.c b/ext/standard/array.c index e1b9a78...fcb0f21 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -5801,7 +5801,7 @@ PHP_FUNCTION(array_multisort) } /* }}} */ -/* {{{ proto mixed array_rand(array input [, int num_req]) +/* {{{ proto int|string|array array_rand(array input [, int num_req]) Return key/keys for random entry/entries in the array */ PHP_FUNCTION(array_rand) { @@ -5825,7 +5825,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; } @@ -5866,7 +5866,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/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 8972f82..146e14d 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -245,7 +245,7 @@ function array_udiff_uassoc(array $arr1, array $arr2, ....$rest): array {} */ function array_multisort(&$arr1, $sort_order = SORT_ASC, $sort_flags = SORT_REGULAR, &...$arr2): bool {} -/** @return int|string|array|null */ +/** @return int|string|array */ function array_rand(array $arg, int $num_req = 1) {} /** @return int|float */ diff --git a/ext/standard/tests/array/array_rand.phpt b/ext/standard/tests/array/array_rand.phpt index 72999ed..5895727 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-- <?php -var_dump(array_rand(array())); -var_dump(array_rand(array(), 0)); -var_dump(array_rand(array(1,2,3), 0)); -var_dump(array_rand(array(1,2,3), -1)); -var_dump(array_rand(array(1,2,3), 10)); +try { + var_dump(array_rand(array())); +} catch (\Error $e) { + echo $e->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 30eb7d7..03e20d6 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

« previous php.cvs (#108058) next »