Skip to content

Adding hasParameter() and getParameter() methods to ReflectionFunctionAbstract #10341

Closed
@ollieread

Description

@ollieread

Description

We currently have getMethod() and getProperty() 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()

/* {{{ 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, *lc_name;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
        RETURN_THROWS();
    }

    if (zend_parse_parameters_none() == FAILURE) {
        RETURN_FALSE;
    }

    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;
    }

    lc_name = zend_string_tolower(name);

    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,
            &parameter
        );

        if (arg_info.name == lc_name) {
            RETURN_TRUE;
        }

        arg_info++;
    }

    zend_string_release(lc_name);

    RETURN_FALSE;
}
/* }}} */

I think, if I can figure out (or pointed towards) how to return parameter, I could use the same code for ReflectionFunctionAbstract::getParameter().

Would appreciate any feedback on the proposal and also pointers for the code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions