Skip to content

Promote warnings to errors in array_rand() #4579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
54 changes: 35 additions & 19 deletions ext/standard/tests/array/array_rand.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 30 additions & 18 deletions ext/standard/tests/array/array_rand_variation5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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' ***

Expand All @@ -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