-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Adding hasParameter() and getParameter() methods to ReflectionFunctionAbstract #10341
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
Comments
With those this doesn't seem like a bad solution. Feel free to create a PR. I don't think this needs an RFC, unless somebody else feels different. |
Thanks @iluuu1994, I've updated the methods to be like so: This is /* {{{ Returns whether a parameter exists or not */
ZEND_METHOD(ReflectionFunctionAbstract, hasParameter)
{
reflection_object *intern;
zend_function *fptr;
struct _zend_arg_info *arg_info;
zend_string *name;
uint32_t i, num_args;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
RETURN_THROWS();
}
GET_REFLECTION_OBJECT_PTR(fptr);
arg_info= fptr->common.arg_info;
num_args = fptr->common.num_args;
if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
if (!num_args) {
RETURN_FALSE;
}
for (i = 0; i < num_args; i++) {
if (zend_string_equals(arg_info[0].name, name)) {
RETURN_TRUE;
}
arg_info++;
}
RETURN_FALSE;
}
/* }}} */ This is /* {{{ Returns a parameter specified by its name */
ZEND_METHOD(ReflectionFunctionAbstract, getParameter)
{
reflection_object *intern;
zend_function *fptr;
struct _zend_arg_info *arg_info;
zend_string *name;
uint32_t i, num_args;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
RETURN_THROWS();
}
GET_REFLECTION_OBJECT_PTR(fptr);
arg_info= fptr->common.arg_info;
num_args = fptr->common.num_args;
if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
if (!num_args) {
RETURN_FALSE;
}
for (i = 0; i < num_args; i++) {
zval parameter;
reflection_parameter_factory(
_copy_function(fptr),
Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
arg_info,
i,
i < fptr->common.required_num_args,
¶meter
);
if (zend_string_equals(arg_info[0].name, name)) {
RETURN_ZVAL(parameter);
}
arg_info++;
}
RETURN_NULL();
}
/* }}} */ The issue I'm having is that running
I'm unsure of the best way to return the |
A few suggestions:
|
Thanks, will look into it.
I don't get what this means, would you be able to elaborate? As far as I can tell, it is called once per parameter, the same way it is for |
|
Ah okay, thanks @iluuu1994. Will look into the best way to handle positions as well as names. I know there's |
If the argument is a number/numeric string then you can just return the |
I think I've nailed it now, here is ZEND_METHOD(ReflectionFunctionAbstract, hasParameter)
{
reflection_object *intern;
zend_function *fptr;
struct _zend_arg_info *arg_info;
zval *parameter;
uint32_t i, num_args, position;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", ¶meter) == FAILURE) {
RETURN_THROWS();
}
GET_REFLECTION_OBJECT_PTR(fptr);
num_args = fptr->common.num_args;
arg_info= fptr->common.arg_info;
if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
if (!num_args) {
RETURN_THROWS();
}
switch(Z_TYPE_P(parameter)) {
case IS_LONG:
position = Z_LVAL_P(parameter);
if (position < 0 || position > num_args) {
RETURN_FALSE;
}
RETURN_TRUE;
break;
case IS_STRING:
for (i = 0; i < num_args; i++) {
if (zend_string_equals(arg_info->name, Z_STR_P(parameter))) {
RETURN_TRUE;
}
arg_info++;
}
RETURN_FALSE;
break;
default:
zend_argument_type_error(1, "must be of type %s, %s given", "int or string", zend_zval_type_name(parameter));
break;
}
} For some reason,
Instead of
So I deleted the generated files, and now no matter how many times I run
|
Please create a proper PR, it's very cumbersome to review code in comments + tests are not run this way |
PR is here: #10431 Closing this in the meantime, as this issue no longer servers a purpose. |
Yes, sorry about the delay. Will continue with the PR asap, had a lot of stuff happening in my personal life lately. |
Description
We currently have
getMethod()
andgetProperty()
methods to retrieve a single method or properly by its name, but we do not have the same for parameters, even though we now have named properties.I am happy to PR this, and I think I may have figured out a way to achieve it, but right now, it involves looping through the parameters until one that matches is found, which feels less than ideal.
Here's
ReflectionFunctionAbstract::hasParameter()
I think, if I can figure out (or pointed towards) how to return
parameter
, I could use the same code forReflectionFunctionAbstract::getParameter()
.Would appreciate any feedback on the proposal and also pointers for the code.
The text was updated successfully, but these errors were encountered: