-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add ZPP macros for class name or object parameters #5647
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b61954a
Add ZPP macros for string|object parameters
kocsismate 8070705
Add convenience macros for ZPP validation of nullable objects
kocsismate c6012d5
Rename macro
kocsismate 0b86dcb
Remove the duplicated condition
kocsismate 2328b3f
Remove support for a null default value
kocsismate d8e33aa
Change the new macro to Z_PARAM_CLASS_NAME_OR_OBJ
kocsismate 99b1076
Fix destination
kocsismate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1217,6 +1217,8 @@ static zend_always_inline zval *zend_try_array_init(zval *zv) | |
_(Z_EXPECTED_STRING_OR_ARRAY_OR_NULL, "of type string|array|null") \ | ||
_(Z_EXPECTED_STRING_OR_LONG, "of type string|int") \ | ||
_(Z_EXPECTED_STRING_OR_LONG_OR_NULL, "of type string|int|null") \ | ||
_(Z_EXPECTED_CLASS_NAME_OR_OBJECT, "a valid class name or object") \ | ||
_(Z_EXPECTED_CLASS_NAME_OR_OBJECT_OR_NULL, "a valid class name, object, or null") \ | ||
|
||
#define Z_EXPECTED_TYPE | ||
|
||
|
@@ -1395,6 +1397,20 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num | |
#define Z_PARAM_CLASS(dest) \ | ||
Z_PARAM_CLASS_EX(dest, 0, 0) | ||
|
||
#define Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest, allow_null) \ | ||
Z_PARAM_PROLOGUE(0, 0); \ | ||
if (UNEXPECTED(!zend_parse_arg_class_name_or_obj(_arg, &dest, _i, allow_null))) { \ | ||
_expected_type = allow_null ? Z_EXPECTED_CLASS_NAME_OR_OBJECT_OR_NULL : Z_EXPECTED_CLASS_NAME_OR_OBJECT; \ | ||
_error_code = ZPP_ERROR_WRONG_ARG; \ | ||
break; \ | ||
} | ||
|
||
#define Z_PARAM_CLASS_NAME_OR_OBJ(dest) \ | ||
Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest, 0); | ||
|
||
#define Z_PARAM_CLASS_NAME_OR_OBJ_OR_NULL(dest) \ | ||
Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest, 1); | ||
|
||
/* old "d" */ | ||
#define Z_PARAM_DOUBLE_EX2(dest, is_null, check_null, deref, separate) \ | ||
Z_PARAM_PROLOGUE(deref, separate); \ | ||
|
@@ -1513,6 +1529,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num | |
#define Z_PARAM_OBJECT(dest) \ | ||
Z_PARAM_OBJECT_EX(dest, 0, 0) | ||
|
||
#define Z_PARAM_OBJECT_OR_NULL(dest) \ | ||
Z_PARAM_OBJECT_EX(dest, 1, 0) | ||
|
||
/* old "O" */ | ||
#define Z_PARAM_OBJECT_OF_CLASS_EX2(dest, _ce, check_null, deref, separate) \ | ||
Z_PARAM_PROLOGUE(deref, separate); \ | ||
|
@@ -1534,6 +1553,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num | |
#define Z_PARAM_OBJECT_OF_CLASS(dest, _ce) \ | ||
Z_PARAM_OBJECT_OF_CLASS_EX(dest, _ce, 0, 0) | ||
|
||
#define Z_PARAM_OBJECT_OF_CLASS_OR_NULL(dest, _ce) \ | ||
Z_PARAM_OBJECT_OF_CLASS_EX(dest, _ce, 1, 0) | ||
|
||
/* old "p" */ | ||
#define Z_PARAM_PATH_EX2(dest, dest_len, check_null, deref, separate) \ | ||
Z_PARAM_PROLOGUE(deref, separate); \ | ||
|
@@ -1673,6 +1695,7 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num | |
|
||
#define Z_PARAM_STR_OR_LONG_OR_NULL(dest_str, dest_long, is_null) \ | ||
Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, 1); | ||
|
||
/* End of new parameter parsing API */ | ||
|
||
/* Inlined implementations shared by new and old parameter parsing APIs */ | ||
|
@@ -1931,6 +1954,25 @@ static zend_always_inline int zend_parse_arg_str_or_long(zval *arg, zend_string | |
return 1; | ||
} | ||
|
||
static zend_always_inline int zend_parse_arg_class_name_or_obj( | ||
zval *arg, zend_class_entry **destination, int num, int allow_null | ||
) { | ||
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) { | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*destination = zend_lookup_class(Z_STR_P(arg)); | ||
|
||
return *destination != NULL; | ||
} else if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) { | ||
*destination = Z_OBJ_P(arg)->ce; | ||
} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) { | ||
*destination = NULL; | ||
} else { | ||
*destination = NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line can be dropped, we don't initialize in the failure case. |
||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
END_EXTERN_C() | ||
|
||
#endif /* ZEND_API_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.