Skip to content

Commit e52d2b8

Browse files
Barbu Paul - Gheorghesgolemon
Barbu Paul - Gheorghe
authored andcommitted
Reduce redundant storage of required number of parameters to required flag
The required field, until now, stored how many required parameters the function, it belongs to, has. I think this is rather problematic because it's a feature of the function to know how many required parameters it has, not of the parameter itself. The parameter should only say if it's required or optional (among other unrelated things). Also storing the function's number of required parameters in every parameter was redundant since the _zend_function structure already has that information. And storing the same value (number of required parameters) across multiple variables is inefficient and could lead to inconsistencies.
1 parent 0aaea39 commit e52d2b8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

ext/reflection/php_reflection.c

+18-11
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ typedef struct _property_reference {
183183
/* Struct for parameters */
184184
typedef struct _parameter_reference {
185185
uint32_t offset;
186-
uint32_t required;
186+
zend_bool required;
187187
struct _zend_arg_info *arg_info;
188188
zend_function *fptr;
189189
} parameter_reference;
@@ -670,10 +670,10 @@ static zend_op* _get_recv_op(zend_op_array *op_array, uint32_t offset)
670670
/* }}} */
671671

672672
/* {{{ _parameter_string */
673-
static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, uint32_t required, char* indent)
673+
static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent)
674674
{
675675
string_printf(str, "Parameter #%d [ ", offset);
676-
if (offset >= required) {
676+
if (!required) {
677677
string_printf(str, "<optional> ");
678678
} else {
679679
string_printf(str, "<required> ");
@@ -708,7 +708,7 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg
708708
} else {
709709
string_printf(str, "$param%d", offset);
710710
}
711-
if (fptr->type == ZEND_USER_FUNCTION && offset >= required) {
711+
if (fptr->type == ZEND_USER_FUNCTION && !required) {
712712
zend_op *precv = _get_recv_op((zend_op_array*)fptr, offset);
713713
if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) {
714714
zval zv;
@@ -747,7 +747,7 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg
747747
static void _function_parameter_string(string *str, zend_function *fptr, char* indent)
748748
{
749749
struct _zend_arg_info *arg_info = fptr->common.arg_info;
750-
uint32_t i, num_args, required = fptr->common.required_num_args;
750+
uint32_t i, num_args, num_required = fptr->common.required_num_args;
751751

752752
if (!arg_info) {
753753
return;
@@ -761,7 +761,7 @@ static void _function_parameter_string(string *str, zend_function *fptr, char* i
761761
string_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
762762
for (i = 0; i < num_args; i++) {
763763
string_printf(str, "%s ", indent);
764-
_parameter_string(str, fptr, arg_info, i, required, indent);
764+
_parameter_string(str, fptr, arg_info, i, i < num_required, indent);
765765
string_write(str, "\n", sizeof("\n")-1);
766766
arg_info++;
767767
}
@@ -1229,7 +1229,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
12291229
/* }}} */
12301230

12311231
/* {{{ reflection_parameter_factory */
1232-
static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, uint32_t required, zval *object)
1232+
static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, zval *object)
12331233
{
12341234
reflection_object *intern;
12351235
parameter_reference *reference;
@@ -2066,7 +2066,7 @@ ZEND_METHOD(reflection_function, returnsReference)
20662066
/* }}} */
20672067

20682068
/* {{{ proto public bool ReflectionFunction::getNumberOfParameters()
2069-
Gets the number of required parameters */
2069+
Gets the number of parameters */
20702070
ZEND_METHOD(reflection_function, getNumberOfParameters)
20712071
{
20722072
reflection_object *intern;
@@ -2121,7 +2121,14 @@ ZEND_METHOD(reflection_function, getParameters)
21212121
for (i = 0; i < num_args; i++) {
21222122
zval parameter;
21232123

2124-
reflection_parameter_factory(_copy_function(fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, arg_info, i, fptr->common.required_num_args, &parameter);
2124+
reflection_parameter_factory(
2125+
_copy_function(fptr),
2126+
Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
2127+
arg_info,
2128+
i,
2129+
i < fptr->common.required_num_args,
2130+
&parameter
2131+
);
21252132
add_next_index_zval(return_value, &parameter);
21262133

21272134
arg_info++;
@@ -2533,7 +2540,7 @@ ZEND_METHOD(reflection_parameter, __construct)
25332540
ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
25342541
ref->arg_info = &arg_info[position];
25352542
ref->offset = (uint32_t)position;
2536-
ref->required = fptr->common.required_num_args;
2543+
ref->required = position < fptr->common.required_num_args;
25372544
ref->fptr = fptr;
25382545
/* TODO: copy fptr */
25392546
intern->ptr = ref;
@@ -2838,7 +2845,7 @@ ZEND_METHOD(reflection_parameter, isOptional)
28382845
}
28392846
GET_REFLECTION_OBJECT_PTR(param);
28402847

2841-
RETVAL_BOOL(param->offset >= param->required);
2848+
RETVAL_BOOL(!param->required);
28422849
}
28432850
/* }}} */
28442851

0 commit comments

Comments
 (0)