Skip to content

Support "string or array" in zpp #4970

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
25 changes: 25 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ static zend_always_inline zval *zend_try_array_init(zval *zv)
_(Z_EXPECTED_OBJECT, "object") \
_(Z_EXPECTED_DOUBLE, "float") \
_(Z_EXPECTED_NUMBER, "int or float") \
_(Z_EXPECTED_STRING_OR_ARRAY, "string or array") \

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

#define Z_PARAM_STR_OR_ARRAY_HT(dest_str, dest_ht) \
Z_PARAM_PROLOGUE(0, 0); \
if (UNEXPECTED(!zend_parse_arg_str_or_array_ht(_arg, &dest_str, &dest_ht))) { \
_expected_type = Z_EXPECTED_STRING_OR_ARRAY; \
_error_code = ZPP_ERROR_WRONG_ARG; \
break; \
}

/* End of new parameter parsing API */

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

static zend_always_inline int zend_parse_arg_str_or_array_ht(
zval *arg, zend_string **dest_str, HashTable **dest_ht)
{
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
*dest_str = Z_STR_P(arg);
*dest_ht = NULL;
} else if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
*dest_ht = Z_ARRVAL_P(arg);
*dest_str = NULL;
} else {
*dest_ht = NULL;
return zend_parse_arg_str_slow(arg, dest_str);
}
return 1;
}

END_EXTERN_C()

#endif /* ZEND_API_H */
13 changes: 6 additions & 7 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,11 @@ function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"):
function substr(string $str, int $start, ?int $length = null): string|false {}

/**
* @param mixed $str
* @param mixed $replace
* @param mixed $start
* @param mixed $length
*/
function substr_replace($str, $replace, $start, $length = UNKNOWN): string|array|false {}
function substr_replace(
string|array $str, string|array $replace, $start, $length = UNKNOWN): string|array|false {}

function quotemeta(string $str): string {}

Expand Down Expand Up @@ -620,18 +619,18 @@ function stripslashes(string $str): string {}
/**
* @param string|array $search
* @param string|array $replace
* @param string|array $subject
* @param int $replace_count
*/
function str_replace($search, $replace, $subject, &$replace_count = UNKNOWN): string|array {}
function str_replace(
$search, $replace, string|array $subject, &$replace_count = UNKNOWN): string|array {}

/**
* @param string|array $search
* @param string|array $replace
* @param string|array $subject
* @param int $replace_count
*/
function str_ireplace($search, $replace, $subject, &$replace_count = UNKNOWN): string|array {}
function str_ireplace(
$search, $replace, string|array $subject, &$replace_count = UNKNOWN): string|array {}

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

Expand Down
6 changes: 3 additions & 3 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_substr, 0, 2, MAY_BE_STRING|MAY_
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_substr_replace, 0, 3, MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_INFO(0, str)
ZEND_ARG_INFO(0, replace)
ZEND_ARG_TYPE_MASK(0, str, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_TYPE_MASK(0, replace, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_INFO(0, start)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -970,7 +970,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_str_replace, 0, 3, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_INFO(0, search)
ZEND_ARG_INFO(0, replace)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_TYPE_MASK(0, subject, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_INFO(1, replace_count)
ZEND_END_ARG_INFO()

Expand Down
Loading