]> granicus.if.org Git - php/commitdiff
Promote warnings to errors in array_rand()
authorGeorge Peter Banyard <girgias@php.net>
Tue, 20 Aug 2019 23:20:53 +0000 (01:20 +0200)
committerGeorge Peter Banyard <girgias@php.net>
Mon, 26 Aug 2019 11:31:02 +0000 (13:31 +0200)
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

index e1b9a78e38c6f4813e16b5b1a14f83becd096135..fcb0f21bcdb1d57c1c463580738357f47276995f 100644 (file)
@@ -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;
        }
 
index 8972f8228baeb5b00af2ecf6eb1c415542efdba6..146e14d51d0d33c2380d36f8c9fa893e89735ddf 100644 (file)
@@ -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 */
index 72999eda5fdd64187a188e8615a858b6e39a858e..589572754829b9dc74c5b33fe4389983970bb1ef 100644 (file)
@@ -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)
index 30eb7d780104f1bf730927a309d5255cbd4439d2..03e20d6e07a877db8ce8f3db558eeb75239728db 100644 (file)
@@ -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