Skip to content

Fix implode() function signature #11991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 19 additions & 14 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,22 +1030,27 @@ 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) {
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();
}
Copy link
Contributor

@ju1ius ju1ius Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
arg1_str = ZSTR_EMPTY_ALLOC();
pieces = arg1_array;

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);
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/tests/strings/implode1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 23 additions & 23 deletions ext/standard/tests/strings/join_variation2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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