Skip to content

Commit f1af722

Browse files
Fixing incorrect error message when passing null to join/implode's array parameter (#12683)
Closes GH-12682
1 parent c779bdc commit f1af722

File tree

6 files changed

+55
-71
lines changed

6 files changed

+55
-71
lines changed

ext/standard/string.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,11 @@ PHP_FUNCTION(implode)
10351035

10361036
if (pieces == NULL) {
10371037
if (arg1_array == NULL) {
1038-
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name());
1038+
zend_type_error(
1039+
"%s(): If argument #1 ($separator) is of type string, "
1040+
"argument #2 ($array) must be of type array, null given",
1041+
get_active_function_name()
1042+
);
10391043
RETURN_THROWS();
10401044
}
10411045

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Test implode() function: error conditions
3+
--FILE--
4+
<?php
5+
/* only glue */
6+
try {
7+
var_dump(implode("glue"));
8+
} catch (TypeError $e) {
9+
echo $e->getMessage(), "\n";
10+
}
11+
12+
/* NULL as pieces */
13+
try {
14+
var_dump(implode("glue", NULL));
15+
} catch (TypeError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
/* integer as glue */
20+
try {
21+
var_dump(implode(12, "pieces"));
22+
} catch (TypeError $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
28+
implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
29+
implode(): Argument #2 ($array) must be of type ?array, string given

ext/standard/tests/strings/implode1.phpt renamed to ext/standard/tests/strings/implode_variation.phpt

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ Test implode() function
33
--FILE--
44
<?php
55
echo "*** Testing implode() for basic operations ***\n";
6-
$arrays = array (
6+
$arrays = array(
77
array(1,2),
88
array(1.1,2.2),
99
array(array(2),array(1)),
1010
array(false,true),
1111
array(),
12-
array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc")
12+
array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc"),
13+
array(NULL),
1314
);
1415
/* loop to output string with ', ' as $glue, using implode() */
1516
foreach ($arrays as $array) {
16-
var_dump( implode(', ', $array) );
17+
var_dump(implode(', ', $array));
1718
var_dump($array);
1819
}
1920

2021
echo "\n*** Testing implode() with variations of glue ***\n";
2122
/* checking possible variations */
22-
$pieces = array (
23+
$pieces = array(
2324
2,
2425
0,
2526
-639,
@@ -30,7 +31,7 @@ $pieces = array (
3031
" ",
3132
"string\x00with\x00...\0"
3233
);
33-
$glues = array (
34+
$glues = array(
3435
"TRUE",
3536
true,
3637
false,
@@ -72,7 +73,7 @@ try {
7273
echo $exception->getMessage() . "\n";
7374
}
7475
try {
75-
var_dump( implode(2, $sub_array) );
76+
var_dump(implode(2, $sub_array));
7677
} catch (TypeError $exception) {
7778
echo $exception->getMessage() . "\n";
7879
}
@@ -90,7 +91,7 @@ $obj = new foo(); //creating new object
9091
$arr = array();
9192
$arr[0] = &$obj;
9293
$arr[1] = &$obj;
93-
var_dump( implode(",", $arr) );
94+
var_dump(implode(",", $arr));
9495
var_dump($arr);
9596

9697
/* Checking on resource types */
@@ -104,51 +105,7 @@ $dir_handle = opendir( __DIR__ );
104105
/* store resources in array for comparison */
105106
$resources = array($file_handle, $dir_handle);
106107

107-
var_dump( implode("::", $resources) );
108-
109-
echo "\n*** Testing error conditions ***\n";
110-
111-
/* only glue */
112-
try {
113-
var_dump( implode("glue") );
114-
} catch (TypeError $e) {
115-
echo $e->getMessage(), "\n";
116-
}
117-
118-
/* int as pieces */
119-
try {
120-
var_dump( implode("glue",1234) );
121-
} catch (TypeError $e) {
122-
echo $e->getMessage(), "\n";
123-
}
124-
125-
/* NULL as pieces */
126-
try {
127-
var_dump( implode("glue", NULL) );
128-
} catch (TypeError $e) {
129-
echo $e->getMessage(), "\n";
130-
}
131-
132-
/* pieces as NULL array */
133-
try {
134-
var_dump( implode(",", array(NULL)) );
135-
} catch (TypeError $e) {
136-
echo $e->getMessage(), "\n";
137-
}
138-
139-
/* integer as glue */
140-
try {
141-
var_dump( implode(12, "pieces") );
142-
} catch (TypeError $e) {
143-
echo $e->getMessage(), "\n";
144-
}
145-
146-
/* NULL as glue */
147-
try {
148-
var_dump( implode(NULL, "abcd") );
149-
} catch (TypeError $e) {
150-
echo $e->getMessage(), "\n";
151-
}
108+
var_dump(implode("::", $resources));
152109

153110
/* closing resource handles */
154111
fclose($file_handle);
@@ -214,6 +171,11 @@ array(6) {
214171
[5]=>
215172
string(21) "ccccccccccccccccccccc"
216173
}
174+
string(0) ""
175+
array(1) {
176+
[0]=>
177+
NULL
178+
}
217179

218180
*** Testing implode() with variations of glue ***
219181
-- Iteration 1 --
@@ -236,7 +198,7 @@ string(35) "2000-639010PHP000 0string%0with%0...%0"
236198
string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0"
237199

238200
*** Testing implode() on empty string ***
239-
implode(): Argument #1 ($array) must be of type array, string given
201+
implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
240202

241203
*** Testing implode() on sub-arrays ***
242204

@@ -264,14 +226,4 @@ array(2) {
264226

265227
*** Testing end() on resource type ***
266228
string(%d) "Resource id #%d::Resource id #%d"
267-
268-
*** Testing error conditions ***
269-
implode(): Argument #1 ($array) must be of type array, string given
270-
implode(): Argument #2 ($array) must be of type ?array, int given
271-
implode(): Argument #1 ($array) must be of type array, string given
272-
string(0) ""
273-
implode(): Argument #2 ($array) must be of type ?array, string given
274-
275-
Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d
276-
implode(): Argument #2 ($array) must be of type ?array, string given
277229
Done

ext/standard/tests/strings/join_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ echo "Done\n";
2020
*** Testing join() : error conditions ***
2121

2222
-- Testing join() with less than expected no. of arguments --
23-
join(): Argument #1 ($array) must be of type array, string given
23+
join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
2424
Done

ext/standard/tests/strings/join_variation2.phpt renamed to ext/standard/tests/strings/join_error1.phpt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class test
2929

3030
// array with different values
3131
$values = array (
32-
3332
// integer values
3433
0,
3534
1,
@@ -84,7 +83,7 @@ for($index = 0; $index < count($values); $index ++) {
8483
$pieces = $values [$index];
8584

8685
try {
87-
var_dump( join($glue, $pieces) );
86+
var_dump(join($glue, $pieces));
8887
} catch (TypeError $e) {
8988
echo $e->getMessage(), "\n";
9089
}
@@ -138,13 +137,13 @@ join(): Argument #2 ($array) must be of type ?array, string given
138137
-- Iteration 18 --
139138
join(): Argument #2 ($array) must be of type ?array, string given
140139
-- Iteration 19 --
141-
join(): Argument #1 ($array) must be of type array, string given
140+
join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
142141
-- Iteration 20 --
143-
join(): Argument #1 ($array) must be of type array, string given
142+
join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
144143
-- Iteration 21 --
145144
join(): Argument #2 ($array) must be of type ?array, resource given
146145
-- Iteration 22 --
147-
join(): Argument #1 ($array) must be of type array, string given
146+
join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
148147
-- Iteration 23 --
149-
join(): Argument #1 ($array) must be of type array, string given
148+
join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
150149
Done

0 commit comments

Comments
 (0)