Skip to content

Commit c1c8538

Browse files
committed
Promote warnings to errors in array_rand()
1 parent 0038db2 commit c1c8538

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

ext/standard/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5801,7 +5801,7 @@ PHP_FUNCTION(array_multisort)
58015801
}
58025802
/* }}} */
58035803

5804-
/* {{{ proto mixed array_rand(array input [, int num_req])
5804+
/* {{{ proto int|string|array array_rand(array input [, int num_req])
58055805
Return key/keys for random entry/entries in the array */
58065806
PHP_FUNCTION(array_rand)
58075807
{
@@ -5825,7 +5825,7 @@ PHP_FUNCTION(array_rand)
58255825
num_avail = zend_hash_num_elements(Z_ARRVAL_P(input));
58265826

58275827
if (num_avail == 0) {
5828-
php_error_docref(NULL, E_WARNING, "Array is empty");
5828+
zend_throw_error(NULL, "Array is empty");
58295829
return;
58305830
}
58315831

@@ -5866,7 +5866,7 @@ PHP_FUNCTION(array_rand)
58665866
}
58675867

58685868
if (num_req <= 0 || num_req > num_avail) {
5869-
php_error_docref(NULL, E_WARNING, "Second argument has to be between 1 and the number of elements in the array");
5869+
zend_throw_error(NULL, "Second argument has to be between 1 and the number of elements in the array");
58705870
return;
58715871
}
58725872

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function array_udiff_uassoc(array $arr1, array $arr2, ...$rest): array {}
245245
*/
246246
function array_multisort(&$arr1, $sort_order = SORT_ASC, $sort_flags = SORT_REGULAR, &...$arr2): bool {}
247247

248-
/** @return int|string|array|null */
248+
/** @return int|string|array */
249249
function array_rand(array $arg, int $num_req = 1) {}
250250

251251
/** @return int|float */

ext/standard/tests/array/array_rand.phpt

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,47 @@ array_rand() tests
33
--FILE--
44
<?php
55

6-
var_dump(array_rand(array()));
7-
var_dump(array_rand(array(), 0));
8-
var_dump(array_rand(array(1,2,3), 0));
9-
var_dump(array_rand(array(1,2,3), -1));
10-
var_dump(array_rand(array(1,2,3), 10));
6+
try {
7+
var_dump(array_rand(array()));
8+
} catch (\Error $e) {
9+
echo $e->getMessage() . "\n";
10+
}
11+
12+
try {
13+
var_dump(array_rand(array(), 0));
14+
} catch (\Error $e) {
15+
echo $e->getMessage() . "\n";
16+
}
17+
18+
try {
19+
var_dump(array_rand(array(1,2,3), 0));
20+
} catch (\Error $e) {
21+
echo $e->getMessage() . "\n";
22+
}
23+
24+
try {
25+
var_dump(array_rand(array(1,2,3), -1));
26+
} catch (\Error $e) {
27+
echo $e->getMessage() . "\n";
28+
}
29+
30+
try {
31+
var_dump(array_rand(array(1,2,3), 10));
32+
} catch (\Error $e) {
33+
echo $e->getMessage() . "\n";
34+
}
35+
1136
var_dump(array_rand(array(1,2,3), 3));
1237
var_dump(array_rand(array(1,2,3), 2));
1338

1439
echo "Done\n";
1540
?>
1641
--EXPECTF--
17-
Warning: array_rand(): Array is empty in %s on line %d
18-
NULL
19-
20-
Warning: array_rand(): Array is empty in %s on line %d
21-
NULL
22-
23-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
24-
NULL
25-
26-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
27-
NULL
28-
29-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
30-
NULL
42+
Array is empty
43+
Array is empty
44+
Second argument has to be between 1 and the number of elements in the array
45+
Second argument has to be between 1 and the number of elements in the array
46+
Second argument has to be between 1 and the number of elements in the array
3147
array(3) {
3248
[0]=>
3349
int(%d)

ext/standard/tests/array/array_rand_variation5.phpt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,36 @@ var_dump( array_rand($input, 1) ); // with valid $num_req value
3232

3333
// with invalid num_req value
3434
echo"\n-- With num_req = 0 --\n";
35-
var_dump( array_rand($input, 0) ); // with $num_req=0
35+
try {
36+
var_dump( array_rand($input, 0) ); // with $num_req=0
37+
} catch (\Error $e) {
38+
echo $e->getMessage() . "\n";
39+
}
40+
3641
echo"\n-- With num_req = -1 --\n";
37-
var_dump( array_rand($input, -1) ); // with $num_req=-1
42+
try {
43+
var_dump( array_rand($input, -1) ); // with $num_req=-1
44+
} catch (\Error $e) {
45+
echo $e->getMessage() . "\n";
46+
}
47+
3848
echo"\n-- With num_req = -2 --\n";
39-
var_dump( array_rand($input, -2) ); // with $num_req=-2
40-
echo"\n-- With num_req more than number of members in 'input' array --\n";
41-
var_dump( array_rand($input, 13) ); // with $num_req=13
49+
try {
50+
var_dump( array_rand($input, -2) ); // with $num_req=-2
51+
} catch (\Error $e) {
52+
echo $e->getMessage() . "\n";
53+
}
4254

55+
echo"\n-- With num_req more than number of members in 'input' array --\n";
56+
try {
57+
var_dump( array_rand($input, 13) ); // with $num_req=13
58+
} catch (\Error $e) {
59+
echo $e->getMessage() . "\n";
60+
}
4361

44-
echo "Done";
4562
?>
63+
64+
DONE
4665
--EXPECTF--
4766
*** Testing array_rand() : with invalid values for 'req_num' ***
4867

@@ -53,22 +72,15 @@ int(%d)
5372
int(%d)
5473

5574
-- With num_req = 0 --
56-
57-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
58-
NULL
75+
Second argument has to be between 1 and the number of elements in the array
5976

6077
-- With num_req = -1 --
61-
62-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
63-
NULL
78+
Second argument has to be between 1 and the number of elements in the array
6479

6580
-- With num_req = -2 --
66-
67-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
68-
NULL
81+
Second argument has to be between 1 and the number of elements in the array
6982

7083
-- With num_req more than number of members in 'input' array --
84+
Second argument has to be between 1 and the number of elements in the array
7185

72-
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
73-
NULL
74-
Done
86+
DONE

0 commit comments

Comments
 (0)