]> granicus.if.org Git - php/commitdiff
Allow array_splice() length to be null
authorColin O'Dell <colinodell@gmail.com>
Mon, 21 Oct 2019 21:22:04 +0000 (17:22 -0400)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 23 Oct 2019 09:22:12 +0000 (11:22 +0200)
UPGRADING
ext/standard/array.c
ext/standard/basic_functions.stub.php
ext/standard/basic_functions_arginfo.h
ext/standard/tests/array/array_splice_basic.phpt

index 40af9ca458827d51feed48f5a9131ea595b4f203..41cab54f1e5a2a870b6a458208e6ef91a4be5886 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -275,6 +275,9 @@ PHP 8.0 UPGRADE NOTES
     iconv_substr() can now be null. Null values will behave as if no length
     argument was provided and will therefore return the remainder of the string
     instead of an empty string.
+  . The length argument for array_splice() can now be null. Null values will
+    behave identically to omitting the argument, thus removing everything from
+    the 'offset' to the end of the array.
   . The 'salt' option of password_hash() is no longer supported. If the 'salt'
     option is used a warning is generated, the provided salt is ignored, and a
     generated salt is used instead.
index d3aac515a7d7b59a4823b7b11674dbdbf875e2b9..8fe281da264ebbc73f4cd31fbee416a63a3101dc 100644 (file)
@@ -3401,19 +3401,20 @@ PHP_FUNCTION(array_splice)
        HashTable  *rem_hash = NULL;
        zend_long offset,
                        length = 0;
+       zend_bool length_is_null = 1;
        int             num_in;                         /* Number of elements in the input array */
 
        ZEND_PARSE_PARAMETERS_START(2, 4)
                Z_PARAM_ARRAY_EX(array, 0, 1)
                Z_PARAM_LONG(offset)
                Z_PARAM_OPTIONAL
-               Z_PARAM_LONG(length)
+               Z_PARAM_LONG_OR_NULL(length, length_is_null)
                Z_PARAM_ZVAL(repl_array)
        ZEND_PARSE_PARAMETERS_END();
 
        num_in = zend_hash_num_elements(Z_ARRVAL_P(array));
 
-       if (ZEND_NUM_ARGS() < 3) {
+       if (length_is_null) {
                length = num_in;
        }
 
index 78bc728d2af9b350f90b9244dac40c315c5593bf..cacaa7ba3add879475f92b810135658ce27e0132 100755 (executable)
@@ -163,7 +163,7 @@ function array_shift(array &$stack) {}
 
 function array_unshift(array &$stack, ...$vars): int {}
 
-function array_splice(array &$arg, int $offset, int $length = UNKNOWN, $replacement = []): array {}
+function array_splice(array &$arg, int $offset, ?int $length = null, $replacement = []): array {}
 
 function array_slice(array $arg, int $offset, ?int $length = null, bool $preserve_keys = false): array {}
 
index 56b0f116c494c010351a93b9243491ff3f2c805a..40bac9cc7eeae9bbd3d9dcb2c32c23cd95de9516 100755 (executable)
@@ -194,7 +194,7 @@ ZEND_END_ARG_INFO()
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_splice, 0, 2, IS_ARRAY, 0)
        ZEND_ARG_TYPE_INFO(1, arg, IS_ARRAY, 0)
        ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
-       ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0)
+       ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 1)
        ZEND_ARG_INFO(0, replacement)
 ZEND_END_ARG_INFO()
 
index 9a96ccb362abc314d5171f4cd14e23ac0ddb56d5..cf6fcd1db25df9b528f389786ee78ae8b09a9167 100644 (file)
@@ -14,6 +14,12 @@ var_dump (array_splice($input, 2));
 var_dump ($input);
 // $input is now array("red", "green")
 
+echo "test truncation with null length \n";
+$input = array("red", "green", "blue", "yellow");
+var_dump (array_splice($input, 2, null));
+var_dump ($input);
+// $input is now array("red", "green")
+
 echo "test removing entries from the middle \n";
 $input = array("red", "green", "blue", "yellow");
 var_dump (array_splice($input, 1, -1));
@@ -56,6 +62,19 @@ array(2) {
   [1]=>
   string(5) "green"
 }
+test truncation with null length 
+array(2) {
+  [0]=>
+  string(4) "blue"
+  [1]=>
+  string(6) "yellow"
+}
+array(2) {
+  [0]=>
+  string(3) "red"
+  [1]=>
+  string(5) "green"
+}
 test removing entries from the middle 
 array(2) {
   [0]=>