Fixing incorrect error message when passing null to join/implode's array parameter (#12683)

Closes GH-12682
This commit is contained in:
Vinicius Dias 2023-12-01 06:55:20 -03:00 committed by GitHub
parent c779bdc953
commit f1af7223f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 71 deletions

View file

@ -1035,7 +1035,11 @@ PHP_FUNCTION(implode)
if (pieces == NULL) { if (pieces == NULL) {
if (arg1_array == NULL) { if (arg1_array == NULL) {
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); zend_type_error(
"%s(): If argument #1 ($separator) is of type string, "
"argument #2 ($array) must be of type array, null given",
get_active_function_name()
);
RETURN_THROWS(); RETURN_THROWS();
} }

View file

@ -0,0 +1,29 @@
--TEST--
Test implode() function: error conditions
--FILE--
<?php
/* only glue */
try {
var_dump(implode("glue"));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* NULL as pieces */
try {
var_dump(implode("glue", NULL));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* integer as glue */
try {
var_dump(implode(12, "pieces"));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
implode(): Argument #2 ($array) must be of type ?array, string given

View file

@ -3,23 +3,24 @@ Test implode() function
--FILE-- --FILE--
<?php <?php
echo "*** Testing implode() for basic operations ***\n"; echo "*** Testing implode() for basic operations ***\n";
$arrays = array ( $arrays = array(
array(1,2), array(1,2),
array(1.1,2.2), array(1.1,2.2),
array(array(2),array(1)), array(array(2),array(1)),
array(false,true), array(false,true),
array(), array(),
array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc") array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc"),
array(NULL),
); );
/* loop to output string with ', ' as $glue, using implode() */ /* loop to output string with ', ' as $glue, using implode() */
foreach ($arrays as $array) { foreach ($arrays as $array) {
var_dump( implode(', ', $array) ); var_dump(implode(', ', $array));
var_dump($array); var_dump($array);
} }
echo "\n*** Testing implode() with variations of glue ***\n"; echo "\n*** Testing implode() with variations of glue ***\n";
/* checking possible variations */ /* checking possible variations */
$pieces = array ( $pieces = array(
2, 2,
0, 0,
-639, -639,
@ -30,7 +31,7 @@ $pieces = array (
" ", " ",
"string\x00with\x00...\0" "string\x00with\x00...\0"
); );
$glues = array ( $glues = array(
"TRUE", "TRUE",
true, true,
false, false,
@ -72,7 +73,7 @@ try {
echo $exception->getMessage() . "\n"; echo $exception->getMessage() . "\n";
} }
try { try {
var_dump( implode(2, $sub_array) ); var_dump(implode(2, $sub_array));
} catch (TypeError $exception) { } catch (TypeError $exception) {
echo $exception->getMessage() . "\n"; echo $exception->getMessage() . "\n";
} }
@ -90,7 +91,7 @@ $obj = new foo(); //creating new object
$arr = array(); $arr = array();
$arr[0] = &$obj; $arr[0] = &$obj;
$arr[1] = &$obj; $arr[1] = &$obj;
var_dump( implode(",", $arr) ); var_dump(implode(",", $arr));
var_dump($arr); var_dump($arr);
/* Checking on resource types */ /* Checking on resource types */
@ -104,51 +105,7 @@ $dir_handle = opendir( __DIR__ );
/* store resources in array for comparison */ /* store resources in array for comparison */
$resources = array($file_handle, $dir_handle); $resources = array($file_handle, $dir_handle);
var_dump( implode("::", $resources) ); var_dump(implode("::", $resources));
echo "\n*** Testing error conditions ***\n";
/* only glue */
try {
var_dump( implode("glue") );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* int as pieces */
try {
var_dump( implode("glue",1234) );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* NULL as pieces */
try {
var_dump( implode("glue", NULL) );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* pieces as NULL array */
try {
var_dump( implode(",", array(NULL)) );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* integer as glue */
try {
var_dump( implode(12, "pieces") );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* NULL as glue */
try {
var_dump( implode(NULL, "abcd") );
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
/* closing resource handles */ /* closing resource handles */
fclose($file_handle); fclose($file_handle);
@ -214,6 +171,11 @@ array(6) {
[5]=> [5]=>
string(21) "ccccccccccccccccccccc" string(21) "ccccccccccccccccccccc"
} }
string(0) ""
array(1) {
[0]=>
NULL
}
*** Testing implode() with variations of glue *** *** Testing implode() with variations of glue ***
-- Iteration 1 -- -- Iteration 1 --
@ -236,7 +198,7 @@ string(35) "2000-639010PHP000 0string%0with%0...%0"
string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0" string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0"
*** Testing implode() on empty string *** *** Testing implode() on empty string ***
implode(): Argument #1 ($array) must be of type array, string given implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
*** Testing implode() on sub-arrays *** *** Testing implode() on sub-arrays ***
@ -264,14 +226,4 @@ array(2) {
*** Testing end() on resource type *** *** Testing end() on resource type ***
string(%d) "Resource id #%d::Resource id #%d" 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
string(0) ""
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
Done Done

View file

@ -20,5 +20,5 @@ echo "Done\n";
*** Testing join() : error conditions *** *** Testing join() : error conditions ***
-- Testing join() with less than expected no. of arguments -- -- Testing join() with less than expected no. of arguments --
join(): Argument #1 ($array) must be of type array, string given join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
Done Done

View file

@ -29,7 +29,6 @@ class test
// array with different values // array with different values
$values = array ( $values = array (
// integer values // integer values
0, 0,
1, 1,
@ -84,7 +83,7 @@ for($index = 0; $index < count($values); $index ++) {
$pieces = $values [$index]; $pieces = $values [$index];
try { try {
var_dump( join($glue, $pieces) ); var_dump(join($glue, $pieces));
} catch (TypeError $e) { } catch (TypeError $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }
@ -138,13 +137,13 @@ join(): Argument #2 ($array) must be of type ?array, string given
-- Iteration 18 -- -- 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 -- -- Iteration 19 --
join(): Argument #1 ($array) must be of type array, string given join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
-- Iteration 20 -- -- Iteration 20 --
join(): Argument #1 ($array) must be of type array, string given join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
-- Iteration 21 -- -- 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 -- -- Iteration 22 --
join(): Argument #1 ($array) must be of type array, string given join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
-- Iteration 23 -- -- Iteration 23 --
join(): Argument #1 ($array) must be of type array, string given join(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given
Done Done