Skip to content

Commit bca5723

Browse files
Tests fixed and code approach reverted to the original one
1 parent a64ff0d commit bca5723

File tree

5 files changed

+101
-87
lines changed

5 files changed

+101
-87
lines changed

ext/standard/string.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,19 +1024,33 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return
10241024
PHP_FUNCTION(implode)
10251025
{
10261026
zend_string *arg1_str = NULL;
1027+
HashTable *arg1_array = NULL;
10271028
zend_array *pieces = NULL;
10281029

1029-
if (ZEND_NUM_ARGS() == 1) {
1030-
ZEND_PARSE_PARAMETERS_START(1, 1)
1031-
Z_PARAM_ARRAY_HT(pieces)
1032-
ZEND_PARSE_PARAMETERS_END();
1030+
ZEND_PARSE_PARAMETERS_START(1, 2)
1031+
Z_PARAM_ARRAY_HT_OR_STR(arg1_array, arg1_str)
1032+
Z_PARAM_OPTIONAL
1033+
Z_PARAM_ARRAY_HT(pieces)
1034+
ZEND_PARSE_PARAMETERS_END();
1035+
1036+
if (arg1_str != NULL && pieces == NULL) {
1037+
zend_type_error("%s(): Argument #2 ($array) must be of type array, null given", get_active_function_name());
1038+
RETURN_THROWS();
1039+
}
1040+
1041+
if (pieces == NULL) {
1042+
if (arg1_array == NULL) {
1043+
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name());
1044+
RETURN_THROWS();
1045+
}
10331046

10341047
arg1_str = ZSTR_EMPTY_ALLOC();
1048+
pieces = arg1_array;
10351049
} else {
1036-
ZEND_PARSE_PARAMETERS_START(2, 2)
1037-
Z_PARAM_STR(arg1_str)
1038-
Z_PARAM_ARRAY_HT(pieces)
1039-
ZEND_PARSE_PARAMETERS_END();
1050+
if (arg1_str == NULL) {
1051+
zend_argument_type_error(1, "must be of type string, array given");
1052+
RETURN_THROWS();
1053+
}
10401054
}
10411055

10421056
php_implode(arg1_str, pieces, return_value);

ext/standard/tests/strings/implode1.phpt

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -106,50 +106,6 @@ $resources = array($file_handle, $dir_handle);
106106

107107
var_dump( implode("::", $resources) );
108108

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-
}
152-
153109
/* closing resource handles */
154110
fclose($file_handle);
155111
closedir($dir_handle);
@@ -236,7 +192,7 @@ string(35) "2000-639010PHP000 0string%0with%0...%0"
236192
string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0"
237193

238194
*** Testing implode() on empty string ***
239-
implode(): Argument #1 ($array) must be of type array, string given
195+
implode(): Argument #2 ($array) must be of type array, null given
240196

241197
*** Testing implode() on sub-arrays ***
242198

@@ -264,14 +220,4 @@ array(2) {
264220

265221
*** Testing end() on resource type ***
266222
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
277223
Done
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
--FILE--
3+
<?php
4+
/* only glue */
5+
try {
6+
var_dump( implode("glue") );
7+
} catch (TypeError $e) {
8+
echo $e->getMessage(), "\n";
9+
}
10+
11+
/* int as pieces */
12+
try {
13+
var_dump( implode("glue",1234) );
14+
} catch (TypeError $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
18+
/* NULL as pieces */
19+
try {
20+
var_dump( implode("glue", NULL) );
21+
} catch (TypeError $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
/* pieces as NULL array */
26+
try {
27+
var_dump( implode(",", array(NULL)) );
28+
} catch (TypeError $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
/* integer as glue */
33+
try {
34+
var_dump( implode(12, "pieces") );
35+
} catch (TypeError $e) {
36+
echo $e->getMessage(), "\n";
37+
}
38+
39+
/* NULL as glue */
40+
try {
41+
var_dump( implode(NULL, "abcd") );
42+
} catch (TypeError $e) {
43+
echo $e->getMessage(), "\n";
44+
}
45+
?>
46+
--EXPECTF--
47+
implode(): Argument #2 ($array) must be of type array, null given
48+
implode(): Argument #2 ($array) must be of type array, int given
49+
implode(): Argument #2 ($array) must be of type array, null given
50+
string(0) ""
51+
implode(): Argument #2 ($array) must be of type array, string given
52+
53+
Deprecated: implode(): Passing null to parameter #1 ($separator) of type array|string is deprecated in %s on line %d
54+
implode(): Argument #2 ($array) must be of type array, string given

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(): 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_error2.phpt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,49 +102,49 @@ echo "Done\n";
102102

103103
--- Testing join() by supplying different values for 'pieces' argument ---
104104
-- Iteration 1 --
105-
join(): Argument #2 ($array) must be of type ?array, int given
105+
join(): Argument #2 ($array) must be of type array, int given
106106
-- Iteration 2 --
107-
join(): Argument #2 ($array) must be of type ?array, int given
107+
join(): Argument #2 ($array) must be of type array, int given
108108
-- Iteration 3 --
109-
join(): Argument #2 ($array) must be of type ?array, int given
109+
join(): Argument #2 ($array) must be of type array, int given
110110
-- Iteration 4 --
111-
join(): Argument #2 ($array) must be of type ?array, int given
111+
join(): Argument #2 ($array) must be of type array, int given
112112
-- Iteration 5 --
113-
join(): Argument #2 ($array) must be of type ?array, float given
113+
join(): Argument #2 ($array) must be of type array, float given
114114
-- Iteration 6 --
115-
join(): Argument #2 ($array) must be of type ?array, float given
115+
join(): Argument #2 ($array) must be of type array, float given
116116
-- Iteration 7 --
117-
join(): Argument #2 ($array) must be of type ?array, float given
117+
join(): Argument #2 ($array) must be of type array, float given
118118
-- Iteration 8 --
119-
join(): Argument #2 ($array) must be of type ?array, float given
119+
join(): Argument #2 ($array) must be of type array, float given
120120
-- Iteration 9 --
121-
join(): Argument #2 ($array) must be of type ?array, float given
121+
join(): Argument #2 ($array) must be of type array, float given
122122
-- Iteration 10 --
123-
join(): Argument #2 ($array) must be of type ?array, true given
123+
join(): Argument #2 ($array) must be of type array, true given
124124
-- Iteration 11 --
125-
join(): Argument #2 ($array) must be of type ?array, false given
125+
join(): Argument #2 ($array) must be of type array, false given
126126
-- Iteration 12 --
127-
join(): Argument #2 ($array) must be of type ?array, true given
127+
join(): Argument #2 ($array) must be of type array, true given
128128
-- Iteration 13 --
129-
join(): Argument #2 ($array) must be of type ?array, false given
129+
join(): Argument #2 ($array) must be of type array, false given
130130
-- Iteration 14 --
131-
join(): Argument #2 ($array) must be of type ?array, string given
131+
join(): Argument #2 ($array) must be of type array, string given
132132
-- Iteration 15 --
133-
join(): Argument #2 ($array) must be of type ?array, string given
133+
join(): Argument #2 ($array) must be of type array, string given
134134
-- Iteration 16 --
135-
join(): Argument #2 ($array) must be of type ?array, test given
135+
join(): Argument #2 ($array) must be of type array, test given
136136
-- Iteration 17 --
137-
join(): Argument #2 ($array) must be of type ?array, string given
137+
join(): Argument #2 ($array) must be of type array, string given
138138
-- Iteration 18 --
139-
join(): Argument #2 ($array) must be of type ?array, string given
139+
join(): Argument #2 ($array) must be of type array, string given
140140
-- Iteration 19 --
141-
join(): Argument #1 ($array) must be of type array, string given
141+
join(): Argument #2 ($array) must be of type array, null given
142142
-- Iteration 20 --
143-
join(): Argument #1 ($array) must be of type array, string given
143+
join(): Argument #2 ($array) must be of type array, null given
144144
-- Iteration 21 --
145-
join(): Argument #2 ($array) must be of type ?array, resource given
145+
join(): Argument #2 ($array) must be of type array, resource given
146146
-- Iteration 22 --
147-
join(): Argument #1 ($array) must be of type array, string given
147+
join(): Argument #2 ($array) must be of type array, null given
148148
-- Iteration 23 --
149-
join(): Argument #1 ($array) must be of type array, string given
149+
join(): Argument #2 ($array) must be of type array, null given
150150
Done

0 commit comments

Comments
 (0)