From 8046bf17c8887d469a257c43aea2ebd7202f1e60 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Thu, 17 Aug 2023 13:34:24 +0100 Subject: [PATCH 1/2] Fix implode() function signature --- ext/standard/basic_functions.stub.php | 4 +- ext/standard/basic_functions_arginfo.h | 4 +- ext/standard/string.c | 2 +- ext/standard/tests/strings/implode1.phpt | 8 ++-- .../tests/strings/join_variation2.phpt | 46 +++++++++---------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 66b458897adb4..19dd1160b3e3c 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -2331,10 +2331,10 @@ function explode(string $separator, string $string, int $limit = PHP_INT_MAX): a /** * @compile-time-eval */ -function implode(string|array $separator, ?array $array = null): string {} +function implode(string|array $separator, array $array = []): string {} /** @alias implode */ -function join(string|array $separator, ?array $array = null): string {} +function join(string|array $separator, array $array = []): string {} /** * @compile-time-eval diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 36b9cb1b9dce6..1fd095679075d 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 487cee0751d47b18bf0a8fbdb050313783f1b369 */ + * Stub hash: 629da63fc239f1891c939a4e487e61912f3460b5 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -858,7 +858,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_implode, 0, 1, IS_STRING, 0) ZEND_ARG_TYPE_MASK(0, separator, MAY_BE_STRING|MAY_BE_ARRAY, NULL) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, array, IS_ARRAY, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, array, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() #define arginfo_join arginfo_implode diff --git a/ext/standard/string.c b/ext/standard/string.c index 70743f4db7085..bbb66c10ce9e5 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1030,7 +1030,7 @@ PHP_FUNCTION(implode) ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_ARRAY_HT_OR_STR(arg1_array, arg1_str) Z_PARAM_OPTIONAL - Z_PARAM_ARRAY_HT_OR_NULL(pieces) + Z_PARAM_ARRAY_HT(pieces) ZEND_PARSE_PARAMETERS_END(); if (pieces == NULL) { diff --git a/ext/standard/tests/strings/implode1.phpt b/ext/standard/tests/strings/implode1.phpt index d467a37dd3175..e9a501545cf12 100644 --- a/ext/standard/tests/strings/implode1.phpt +++ b/ext/standard/tests/strings/implode1.phpt @@ -267,11 +267,11 @@ string(%d) "Resource id #%d::Resource id #%d" *** Testing error conditions *** implode(): Argument #1 ($array) must be of type array, string given -implode(): Argument #2 ($array) must be of type ?array, int given -implode(): Argument #1 ($array) must be of type array, string given +implode(): Argument #2 ($array) must be of type array, int given +implode(): Argument #2 ($array) must be of type array, null given string(0) "" -implode(): Argument #2 ($array) must be of type ?array, string given +implode(): Argument #2 ($array) must be of type array, string given Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d -implode(): Argument #2 ($array) must be of type ?array, string given +implode(): Argument #2 ($array) must be of type array, string given Done diff --git a/ext/standard/tests/strings/join_variation2.phpt b/ext/standard/tests/strings/join_variation2.phpt index f71f5c31f0cbb..1bd0aa689d232 100644 --- a/ext/standard/tests/strings/join_variation2.phpt +++ b/ext/standard/tests/strings/join_variation2.phpt @@ -102,49 +102,49 @@ echo "Done\n"; --- Testing join() by supplying different values for 'pieces' argument --- -- Iteration 1 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 2 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 3 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 4 -- -join(): Argument #2 ($array) must be of type ?array, int given +join(): Argument #2 ($array) must be of type array, int given -- Iteration 5 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 6 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 7 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 8 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 9 -- -join(): Argument #2 ($array) must be of type ?array, float given +join(): Argument #2 ($array) must be of type array, float given -- Iteration 10 -- -join(): Argument #2 ($array) must be of type ?array, true given +join(): Argument #2 ($array) must be of type array, true given -- Iteration 11 -- -join(): Argument #2 ($array) must be of type ?array, false given +join(): Argument #2 ($array) must be of type array, false given -- Iteration 12 -- -join(): Argument #2 ($array) must be of type ?array, true given +join(): Argument #2 ($array) must be of type array, true given -- Iteration 13 -- -join(): Argument #2 ($array) must be of type ?array, false given +join(): Argument #2 ($array) must be of type array, false given -- Iteration 14 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 15 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 16 -- -join(): Argument #2 ($array) must be of type ?array, test given +join(): Argument #2 ($array) must be of type array, test given -- Iteration 17 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 18 -- -join(): Argument #2 ($array) must be of type ?array, string given +join(): Argument #2 ($array) must be of type array, string given -- Iteration 19 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 20 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 21 -- -join(): Argument #2 ($array) must be of type ?array, resource given +join(): Argument #2 ($array) must be of type array, resource given -- Iteration 22 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given -- Iteration 23 -- -join(): Argument #1 ($array) must be of type array, string given +join(): Argument #2 ($array) must be of type array, null given Done From 9cea77dccd73b396835a0709b2be6e5112776b53 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 18 Aug 2023 14:39:14 +0100 Subject: [PATCH 2/2] Improve readability by using switch --- ext/standard/string.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index bbb66c10ce9e5..7fd3e8b77e588 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1033,19 +1033,24 @@ PHP_FUNCTION(implode) Z_PARAM_ARRAY_HT(pieces) ZEND_PARSE_PARAMETERS_END(); - if (pieces == NULL) { - if (arg1_array == NULL) { - zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); - RETURN_THROWS(); - } - - arg1_str = ZSTR_EMPTY_ALLOC(); - pieces = arg1_array; - } else { - if (arg1_str == NULL) { - zend_argument_type_error(1, "must be of type string, array given"); - RETURN_THROWS(); - } + switch (ZEND_NUM_ARGS()) { + case 1: + if (arg1_array == NULL) { + zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); + RETURN_THROWS(); + } + arg1_str = ZSTR_EMPTY_ALLOC(); + pieces = arg1_array; + break; + case 2: + if (arg1_str == NULL) { + zend_argument_type_error(1, "must be of type string, array given"); + RETURN_THROWS(); + } + break; + default: + // already checked by ZPP + ZEND_UNREACHABLE(); } php_implode(arg1_str, pieces, return_value);