Skip to content

Commit a603c06

Browse files
committed
Support "string or array" in zpp
This is one of our more common argument unions. Usage is just prototyped in a few places, certainly not a full conversion. I'm removing the str_replace.phpt test, because aparently it was split up into smaller tests at some point, but the original has not been removed. Closes GH-4970.
1 parent cec7bd5 commit a603c06

File tree

7 files changed

+106
-1017
lines changed

7 files changed

+106
-1017
lines changed

Zend/zend_API.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ static zend_always_inline zval *zend_try_array_init(zval *zv)
11291129
_(Z_EXPECTED_OBJECT, "object") \
11301130
_(Z_EXPECTED_DOUBLE, "float") \
11311131
_(Z_EXPECTED_NUMBER, "int or float") \
1132+
_(Z_EXPECTED_STRING_OR_ARRAY, "string or array") \
11321133

11331134
#define Z_EXPECTED_TYPE_ENUM(id, str) id,
11341135
#define Z_EXPECTED_TYPE_STR(id, str) str,
@@ -1535,6 +1536,14 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(int num, char *e
15351536
#define Z_PARAM_VARIADIC(spec, dest, dest_num) \
15361537
Z_PARAM_VARIADIC_EX(spec, dest, dest_num, 0)
15371538

1539+
#define Z_PARAM_STR_OR_ARRAY_HT(dest_str, dest_ht) \
1540+
Z_PARAM_PROLOGUE(0, 0); \
1541+
if (UNEXPECTED(!zend_parse_arg_str_or_array_ht(_arg, &dest_str, &dest_ht))) { \
1542+
_expected_type = Z_EXPECTED_STRING_OR_ARRAY; \
1543+
_error_code = ZPP_ERROR_WRONG_ARG; \
1544+
break; \
1545+
}
1546+
15381547
/* End of new parameter parsing API */
15391548

15401549
/* Inlined implementations shared by new and old parameter parsing APIs */
@@ -1753,6 +1762,22 @@ static zend_always_inline void zend_parse_arg_zval_deref(zval *arg, zval **dest,
17531762
*dest = (check_null && UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) ? NULL : arg;
17541763
}
17551764

1765+
static zend_always_inline int zend_parse_arg_str_or_array_ht(
1766+
zval *arg, zend_string **dest_str, HashTable **dest_ht)
1767+
{
1768+
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
1769+
*dest_str = Z_STR_P(arg);
1770+
*dest_ht = NULL;
1771+
} else if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
1772+
*dest_ht = Z_ARRVAL_P(arg);
1773+
*dest_str = NULL;
1774+
} else {
1775+
*dest_ht = NULL;
1776+
return zend_parse_arg_str_slow(arg, dest_str);
1777+
}
1778+
return 1;
1779+
}
1780+
17561781
END_EXTERN_C()
17571782

17581783
#endif /* ZEND_API_H */

ext/standard/basic_functions.stub.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,11 @@ function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"):
580580
function substr(string $str, int $start, ?int $length = null): string|false {}
581581

582582
/**
583-
* @param mixed $str
584-
* @param mixed $replace
585583
* @param mixed $start
586584
* @param mixed $length
587585
*/
588-
function substr_replace($str, $replace, $start, $length = UNKNOWN): string|array|false {}
586+
function substr_replace(
587+
string|array $str, string|array $replace, $start, $length = UNKNOWN): string|array|false {}
589588

590589
function quotemeta(string $str): string {}
591590

@@ -620,18 +619,18 @@ function stripslashes(string $str): string {}
620619
/**
621620
* @param string|array $search
622621
* @param string|array $replace
623-
* @param string|array $subject
624622
* @param int $replace_count
625623
*/
626-
function str_replace($search, $replace, $subject, &$replace_count = UNKNOWN): string|array {}
624+
function str_replace(
625+
$search, $replace, string|array $subject, &$replace_count = UNKNOWN): string|array {}
627626

628627
/**
629628
* @param string|array $search
630629
* @param string|array $replace
631-
* @param string|array $subject
632630
* @param int $replace_count
633631
*/
634-
function str_ireplace($search, $replace, $subject, &$replace_count = UNKNOWN): string|array {}
632+
function str_ireplace(
633+
$search, $replace, string|array $subject, &$replace_count = UNKNOWN): string|array {}
635634

636635
function hebrev(string $str, int $max_chars_per_line = 0): string {}
637636

ext/standard/basic_functions_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_substr, 0, 2, MAY_BE_STRING|MAY_
917917
ZEND_END_ARG_INFO()
918918

919919
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_substr_replace, 0, 3, MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_FALSE)
920-
ZEND_ARG_INFO(0, str)
921-
ZEND_ARG_INFO(0, replace)
920+
ZEND_ARG_TYPE_MASK(0, str, MAY_BE_STRING|MAY_BE_ARRAY)
921+
ZEND_ARG_TYPE_MASK(0, replace, MAY_BE_STRING|MAY_BE_ARRAY)
922922
ZEND_ARG_INFO(0, start)
923923
ZEND_ARG_INFO(0, length)
924924
ZEND_END_ARG_INFO()
@@ -970,7 +970,7 @@ ZEND_END_ARG_INFO()
970970
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_str_replace, 0, 3, MAY_BE_STRING|MAY_BE_ARRAY)
971971
ZEND_ARG_INFO(0, search)
972972
ZEND_ARG_INFO(0, replace)
973-
ZEND_ARG_INFO(0, subject)
973+
ZEND_ARG_TYPE_MASK(0, subject, MAY_BE_STRING|MAY_BE_ARRAY)
974974
ZEND_ARG_INFO(1, replace_count)
975975
ZEND_END_ARG_INFO()
976976

0 commit comments

Comments
 (0)