summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/standard/array.c6
-rw-r--r--ext/standard/basic_functions.stub.php2
-rw-r--r--ext/standard/tests/array/array_rand.phpt54
-rw-r--r--ext/standard/tests/array/array_rand_variation5.phpt48
4 files changed, 69 insertions, 41 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c
index e1b9a78e38..fcb0f21bcd 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 8972f8228b..146e14d51d 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 72999eda5f..5895727548 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 30eb7d7801..03e20d6e07 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