]> granicus.if.org Git - php/commitdiff
Remove redundant warning in array_push() and array_unshift()
authortimurib <timok@ya.ru>
Sun, 7 Jan 2018 15:10:12 +0000 (18:10 +0300)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sun, 25 Mar 2018 15:49:04 +0000 (17:49 +0200)
Cf. https://github.com/php/php-src/pull/3011.

UPGRADING
ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/tests/array/array_push.phpt
ext/standard/tests/array/array_push_empty.phpt [new file with mode: 0644]
ext/standard/tests/array/array_push_error1.phpt
ext/standard/tests/array/array_unshift_empty.phpt [new file with mode: 0644]
ext/standard/tests/array/array_unshift_error.phpt

index 44bcae469cfa3d0fedc3f89b982613f24e8ead93..6729b745f1ec1ed5f2610d38c74491aed2c21e6c 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -87,6 +87,8 @@ JSON:
 Standard:
   . debug_zval_dump() was changed to display recursive arrays and objects
     in the same way as var_dump(). Now, it doesn't display them twice.
+  . array_push() and array_unshift() can now also be called with a single
+    argument, which is particularly convenient wrt. the spread operator.
 
 PCRE:
   . preg_quote() now also escapes the '#' character.
index bd8da682bd1ce302f8be5a08849356a597f3d910..4bc363bd19df45ce60a3e6d242532e65de513eb1 100644 (file)
@@ -3217,7 +3217,7 @@ PHP_FUNCTION(array_push)
                argc;                   /* Number of function arguments */
 
 
-       ZEND_PARSE_PARAMETERS_START(2, -1)
+       ZEND_PARSE_PARAMETERS_START(1, -1)
                Z_PARAM_ARRAY_EX(stack, 0, 1)
                Z_PARAM_VARIADIC('+', args, argc)
        ZEND_PARSE_PARAMETERS_END();
@@ -3417,7 +3417,7 @@ PHP_FUNCTION(array_unshift)
        zend_string *key;
        zval *value;
 
-       ZEND_PARSE_PARAMETERS_START(2, -1)
+       ZEND_PARSE_PARAMETERS_START(1, -1)
                Z_PARAM_ARRAY_EX(stack, 0, 1)
                Z_PARAM_VARIADIC('+', args, argc)
        ZEND_PARSE_PARAMETERS_END();
index 95281705dcee477139bff5b6cd790144189427e1..01fb765be34ce5d956062e9f7926baebbf30e14f 100644 (file)
@@ -360,7 +360,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_shuffle, 0)
        ZEND_ARG_INFO(1, arg) /* ARRAY_INFO(1, arg, 0) */
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_push, 0, 0, 2)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_push, 0, 0, 1)
        ZEND_ARG_INFO(1, stack) /* ARRAY_INFO(1, stack, 0) */
        ZEND_ARG_VARIADIC_INFO(0, vars)
 ZEND_END_ARG_INFO()
@@ -373,7 +373,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_array_shift, 0)
        ZEND_ARG_INFO(1, stack) /* ARRAY_INFO(1, stack, 0) */
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_array_unshift, 0, 0, 2)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_array_unshift, 0, 0, 1)
        ZEND_ARG_INFO(1, stack) /* ARRAY_INFO(1, stack, 0) */
        ZEND_ARG_VARIADIC_INFO(0, vars)
 ZEND_END_ARG_INFO()
index 4d8a55e3294998bf3050ee13c7f13400618f7c0e..829100c54c267b14cb3b8d8e60ddf739f9334ae6 100644 (file)
@@ -72,7 +72,7 @@ echo"\nDone";
 --EXPECTF--
 *** Testing Error Conditions ***
 
-Warning: array_push() expects at least 2 parameters, 0 given in %s on line %d
+Warning: array_push() expects at least 1 parameter, 0 given in %s on line %d
 NULL
 
 Warning: array_push() expects parameter 1 to be array, int given in %s on line %d
diff --git a/ext/standard/tests/array/array_push_empty.phpt b/ext/standard/tests/array/array_push_empty.phpt
new file mode 100644 (file)
index 0000000..7ca7e7f
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Test array_push() function : push empty set to the array
+--FILE--
+<?php
+/* Prototype  : int array_push(array $stack[, mixed $...])
+ * Description: Pushes elements onto the end of the array
+ * Source code: ext/standard/array.c
+ */
+
+$array = [1,2,3];
+$values = [];
+
+var_dump( array_push($array) );
+var_dump( array_push($array, ...$values) );
+var_dump( $array );
+
+echo "Done";
+?>
+--EXPECTF--
+int(3)
+int(3)
+array(3) {
+  [0]=>
+  int(1)
+  [1]=>
+  int(2)
+  [2]=>
+  int(3)
+}
+Done
index fe26fb697e79c8bcdbf6cec889f1cf9b72efa7bb..ec866348b2fbc4bf335d1729bcf2be6ea5c68bb1 100644 (file)
@@ -2,7 +2,7 @@
 Test array_push() function : error conditions - Pass incorrect number of args
 --FILE--
 <?php
-/* Prototype  : int array_push(array $stack, mixed $var [, mixed $...])
+/* Prototype  : int array_push(array $stack[, mixed $...])
  * Description: Pushes elements onto the end of the array 
  * Source code: ext/standard/array.c
  */
@@ -15,8 +15,7 @@ echo "*** Testing array_push() : error conditions ***\n";
 
 // Testing array_push with one less than the expected number of arguments
 echo "\n-- Testing array_push() function with less than expected no. of arguments --\n";
-$stack = array(1, 2);
-var_dump( array_push($stack) );
+var_dump( array_push() );
 
 echo "Done";
 ?>
@@ -25,6 +24,6 @@ echo "Done";
 
 -- Testing array_push() function with less than expected no. of arguments --
 
-Warning: array_push() expects at least 2 parameters, 1 given in %s on line %d
+Warning: array_push() expects at least 1 parameter, 0 given in %s on line %d
 NULL
 Done
diff --git a/ext/standard/tests/array/array_unshift_empty.phpt b/ext/standard/tests/array/array_unshift_empty.phpt
new file mode 100644 (file)
index 0000000..546a24a
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Test array_unshift() function : prepend array with empty set
+--FILE--
+<?php
+/* Prototype  : int array_unshift(array $array[, mixed ...])
+ * Description: Pushes elements onto the beginning of the array
+ * Source code: ext/standard/array.c
+*/
+
+$array = [1,2,3];
+$values = [];
+
+var_dump( array_unshift($array) );
+var_dump( array_unshift($array, ...$values) );
+var_dump( $array );
+
+echo "Done";
+?>
+--EXPECTF--
+int(3)
+int(3)
+array(3) {
+  [0]=>
+  int(1)
+  [1]=>
+  int(2)
+  [2]=>
+  int(3)
+}
+Done
index 1381111ec418200cfd045894169014457f6efae2..3eeb3eb7500fc28be64c4f44dfa5ad10dee10749 100644 (file)
@@ -2,7 +2,7 @@
 Test array_unshift() function : error conditions 
 --FILE--
 <?php
-/* Prototype  : int array_unshift(array $array, mixed $var [, mixed ...])
+/* Prototype  : int array_unshift(array $array[, mixed ...])
  * Description: Pushes elements onto the beginning of the array 
  * Source code: ext/standard/array.c
 */
@@ -12,11 +12,6 @@ echo "*** Testing array_unshift() : error conditions ***\n";
 // Zero arguments
 echo "\n-- Testing array_unshift() function with Zero arguments --\n";
 var_dump( array_unshift() );
-
-// Testing array_unshift with one less than the expected number of arguments
-echo "\n-- Testing array_unshift() function with less than expected no. of arguments --\n";
-$array = array(1, 2);
-var_dump( array_unshift($array) );
 echo "Done";
 ?>
 --EXPECTF--
@@ -24,11 +19,6 @@ echo "Done";
 
 -- Testing array_unshift() function with Zero arguments --
 
-Warning: array_unshift() expects at least 2 parameters, 0 given in %s on line %d
-NULL
-
--- Testing array_unshift() function with less than expected no. of arguments --
-
-Warning: array_unshift() expects at least 2 parameters, 1 given in %s on line %d
+Warning: array_unshift() expects at least 1 parameter, 0 given in %s on line %d
 NULL
 Done