Skip to content

Remove support for mixing parameter order in implode() #5008

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
wants to merge 1 commit into from
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
9 changes: 4 additions & 5 deletions Zend/tests/unexpected_ref_bug.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class Test {
return ",";
}
}
$my_var = str_repeat("A",64);
$data = call_user_func_array("explode",array(new Test(), &$my_var));
$my_var=array(1,2,3);
$data = call_user_func_array("implode",array(&$my_var, new Test()));
$my_var = str_repeat("A", 64);
$data = call_user_func_array("explode", array(new Test(), &$my_var));
$my_var = str_repeat("A", 64);
$data = call_user_func_array("str_replace", array(&$my_var, new Test(), "foo"));
echo "Done.\n";
?>
--EXPECTF--
Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
Done.
2 changes: 1 addition & 1 deletion ext/spl/tests/bug75717.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function flatten(array $nestedArraysAndStrings){
$iter = new RecursiveIteratorIterator(
new RecursiveArrayIterator($nestedArraysAndStrings));
foreach($iter as $leaf){ $flat[] = $leaf; }
return join(NULL, $flat);
return join(null, $flat);
}

$noRefs = [[[['some']]],[' nested '],"items"];
Expand Down
6 changes: 1 addition & 5 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,7 @@ function wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut

function explode(string $separator, string $str, int $limit = PHP_INT_MAX): array {}

/**
* @param string $glue Optional - defaults to empty string
* @param array $pieces
*/
function implode($glue, $pieces = UNKNOWN): string {}
function implode(string|array $glue, array $pieces = UNKNOWN): string {}

function strtok(string $str, string $token = UNKNOWN): string|false {}

Expand Down
4 changes: 2 additions & 2 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_explode, 0, 2, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_implode, 0, 1, IS_STRING, 0)
ZEND_ARG_INFO(0, glue)
ZEND_ARG_INFO(0, pieces)
ZEND_ARG_TYPE_MASK(0, glue, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_TYPE_INFO(0, pieces, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtok, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ PHPAPI zend_string *php_str_to_str(const char *haystack, size_t length, const ch
PHPAPI zend_string *php_trim(zend_string *str, char *what, size_t what_len, int mode);
PHPAPI size_t php_strip_tags(char *rbuf, size_t len, uint8_t *state, const char *allow, size_t allow_len);
PHPAPI size_t php_strip_tags_ex(char *rbuf, size_t len, uint8_t *stateptr, const char *allow, size_t allow_len, zend_bool allow_tag_spaces);
PHPAPI void php_implode(const zend_string *delim, zval *arr, zval *return_value);
PHPAPI void php_implode(const zend_string *delim, HashTable *arr, zval *return_value);
PHPAPI void php_explode(const zend_string *delim, zend_string *str, zval *return_value, zend_long limit);

PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end);
Expand Down
43 changes: 17 additions & 26 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,13 +1168,13 @@ PHP_FUNCTION(explode)
}
/* }}} */

/* {{{ proto string join(array src, string glue)
/* {{{ proto string join([string glue,] array pieces)
An alias for implode */
/* }}} */

/* {{{ php_implode
*/
PHPAPI void php_implode(const zend_string *glue, zval *pieces, zval *return_value)
PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return_value)
{
zval *tmp;
int numelems;
Expand All @@ -1187,20 +1187,20 @@ PHPAPI void php_implode(const zend_string *glue, zval *pieces, zval *return_valu
} *strings, *ptr;
ALLOCA_FLAG(use_heap)

numelems = zend_hash_num_elements(Z_ARRVAL_P(pieces));
numelems = zend_hash_num_elements(pieces);

if (numelems == 0) {
RETURN_EMPTY_STRING();
} else if (numelems == 1) {
/* loop to search the first not undefined element... */
ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(pieces), tmp) {
ZEND_HASH_FOREACH_VAL_IND(pieces, tmp) {
RETURN_STR(zval_get_string(tmp));
} ZEND_HASH_FOREACH_END();
}

ptr = strings = do_alloca((sizeof(*strings)) * numelems, use_heap);

ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(pieces), tmp) {
ZEND_HASH_FOREACH_VAL_IND(pieces, tmp) {
if (EXPECTED(Z_TYPE_P(tmp) == IS_STRING)) {
ptr->str = Z_STR_P(tmp);
len += ZSTR_LEN(ptr->str);
Expand Down Expand Up @@ -1264,41 +1264,32 @@ PHPAPI void php_implode(const zend_string *glue, zval *pieces, zval *return_valu
Joins array elements placing glue string between items and return one string */
PHP_FUNCTION(implode)
{
zval *arg1, *arg2 = NULL, *pieces;
zend_string *glue, *tmp_glue;
zend_string *arg1_str = NULL;
HashTable *arg1_array = NULL;
zend_array *pieces = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(arg1)
Z_PARAM_STR_OR_ARRAY_HT(arg1_str, arg1_array)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(arg2)
Z_PARAM_ARRAY_HT(pieces)
ZEND_PARSE_PARAMETERS_END();

if (arg2 == NULL) {
if (Z_TYPE_P(arg1) != IS_ARRAY) {
if (pieces == NULL) {
if (arg1_array == NULL) {
zend_type_error("Argument must be an array");
return;
}

glue = ZSTR_EMPTY_ALLOC();
tmp_glue = NULL;
pieces = arg1;
arg1_str = ZSTR_EMPTY_ALLOC();
pieces = arg1_array;
} else {
if (Z_TYPE_P(arg1) == IS_ARRAY) {
glue = zval_get_tmp_string(arg2, &tmp_glue);
pieces = arg1;
php_error_docref(NULL, E_DEPRECATED,
"Passing glue string after array is deprecated. Swap the parameters");
} else if (Z_TYPE_P(arg2) == IS_ARRAY) {
glue = zval_get_tmp_string(arg1, &tmp_glue);
pieces = arg2;
} else {
zend_type_error("Invalid arguments passed");
if (arg1_str == NULL) {
zend_type_error("The first argument must be string");
return;
}
}

php_implode(glue, pieces, return_value);
zend_tmp_string_release(tmp_glue);
php_implode(arg1_str, pieces, return_value);
}
/* }}} */

Expand Down
Binary file modified ext/standard/tests/strings/implode1.phpt
Binary file not shown.
40 changes: 12 additions & 28 deletions ext/standard/tests/strings/join_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ for($index = 0; $index < count($values); $index ++) {
echo "-- Iteration $counter --\n";
$glue = $values [$index];

var_dump( join($glue, $pieces) );
try {
var_dump(join($glue, $pieces));
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

$counter ++;
$counter++;
}

echo "Done\n";
Expand Down Expand Up @@ -121,35 +125,15 @@ string(29) "element11.07654321E-9element2"
-- Iteration 9 --
string(19) "element10.5element2"
-- Iteration 10 --

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(0) ""
The first argument must be string
-- Iteration 11 --

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(1) "0"
The first argument must be string
-- Iteration 12 --

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(1) "1"
The first argument must be string
-- Iteration 13 --

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(7) "1Array2"
The first argument must be string
-- Iteration 14 --

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(11) "redArraypen"
The first argument must be string
-- Iteration 15 --
string(17) "element11element2"
-- Iteration 16 --
Expand All @@ -169,7 +153,7 @@ string(16) "element1element2"
-- Iteration 23 --
string(16) "element1element2"
-- Iteration 24 --
string(%d) "element1Resource id #%delement2"
join() expects parameter 1 to be string or array, resource given
-- Iteration 25 --
string(16) "element1element2"
-- Iteration 26 --
Expand Down
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 @@ -108,49 +108,49 @@ echo "Done\n";

--- Testing join() by supplying different values for 'pieces' argument ---
-- Iteration 1 --
Invalid arguments passed
join() expects parameter 2 to be array, int given
-- Iteration 2 --
Invalid arguments passed
join() expects parameter 2 to be array, int given
-- Iteration 3 --
Invalid arguments passed
join() expects parameter 2 to be array, int given
-- Iteration 4 --
Invalid arguments passed
join() expects parameter 2 to be array, int given
-- Iteration 5 --
Invalid arguments passed
join() expects parameter 2 to be array, float given
-- Iteration 6 --
Invalid arguments passed
join() expects parameter 2 to be array, float given
-- Iteration 7 --
Invalid arguments passed
join() expects parameter 2 to be array, float given
-- Iteration 8 --
Invalid arguments passed
join() expects parameter 2 to be array, float given
-- Iteration 9 --
Invalid arguments passed
join() expects parameter 2 to be array, float given
-- Iteration 10 --
Invalid arguments passed
join() expects parameter 2 to be array, bool given
-- Iteration 11 --
Invalid arguments passed
join() expects parameter 2 to be array, bool given
-- Iteration 12 --
Invalid arguments passed
join() expects parameter 2 to be array, bool given
-- Iteration 13 --
Invalid arguments passed
join() expects parameter 2 to be array, bool given
-- Iteration 14 --
Invalid arguments passed
join() expects parameter 2 to be array, string given
-- Iteration 15 --
Invalid arguments passed
join() expects parameter 2 to be array, string given
-- Iteration 16 --
Invalid arguments passed
join() expects parameter 2 to be array, object given
-- Iteration 17 --
Invalid arguments passed
join() expects parameter 2 to be array, string given
-- Iteration 18 --
Invalid arguments passed
join() expects parameter 2 to be array, string given
-- Iteration 19 --
Invalid arguments passed
join() expects parameter 2 to be array, null given
-- Iteration 20 --
Invalid arguments passed
join() expects parameter 2 to be array, null given
-- Iteration 21 --
Invalid arguments passed
join() expects parameter 2 to be array, resource given
-- Iteration 22 --
Invalid arguments passed
join() expects parameter 2 to be array, null given
-- Iteration 23 --
Invalid arguments passed
join() expects parameter 2 to be array, null given
Done
Binary file modified ext/standard/tests/strings/join_variation4.phpt
Binary file not shown.
22 changes: 11 additions & 11 deletions ext/standard/tests/strings/join_variation5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ echo "*** Testing implode() : usage variations - sub arrays ***\n";
$sub_array = array(array(1,2,3,4), array(1 => "one", 2 => "two"), "PHP", 50);

// pieces as array containing sub array
var_dump( join("TEST", $sub_array) );
var_dump(join("TEST", $sub_array));

// glue as array & pieces as array containing sub array
var_dump( join(array(1, 2, 3, 4), $sub_array) );
try {
var_dump(join(array(1, 2, 3, 4), $sub_array));
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

// numeric value as glue, pieces as array containg sub array
var_dump( join(2, $sub_array) );
// numeric value as glue, pieces as array containing sub array
var_dump(join(2, $sub_array));

// using directly the sub_array as pieces
var_dump( join(", ", $sub_array[0]) );
var_dump( join(", ", $sub_array[1]) );
var_dump(join(", ", $sub_array[0]));
var_dump(join(", ", $sub_array[1]));

echo "Done\n";
?>
Expand All @@ -37,11 +41,7 @@ Warning: Array to string conversion in %s on line %d

Warning: Array to string conversion in %s on line %d
string(27) "ArrayTESTArrayTESTPHPTEST50"

Warning: Array to string conversion in %s on line %d

Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters in %s on line %d
string(19) "1Array2Array3Array4"
The first argument must be string

Warning: Array to string conversion in %s on line %d

Expand Down