com php-src: Reduce redundant storage of required number of parameters to required flag: ext/reflection/php_reflection.c
| From: | Sara Golemon | Date: | Mon, 26 Dec 2016 18:39:19 +0000 |
| Subject: | com php-src: Reduce redundant storage of required number of parameters to required flag: ext/reflection/php_reflection.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-96433@lists.php.net to get a copy of this message | ||
Commit: e52d2b88e4871f2e82fc26986d97417e76af8127
Author: Barbu Paul - Gheorghe <paullik.paul@gmail.com> Mon, 26 Dec 2016 10:39:19
-0800
Committer: Sara Golemon <pollita@php.net> Mon, 26 Dec 2016 10:39:19 -0800
Parents: 0aaea39740eee58b3ac26f31530e92f917619c1e
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e52d2b88e4871f2e82fc26986d97417e76af8127
Log:
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.
Changed paths:
M ext/reflection/php_reflection.c
Diff:
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index 2c05203..e89293e 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -183,7 +183,7 @@ typedef struct _property_reference {
/* Struct for parameters */
typedef struct _parameter_reference {
uint32_t offset;
- uint32_t required;
+ zend_bool required;
struct _zend_arg_info *arg_info;
zend_function *fptr;
} parameter_reference;
@@ -670,10 +670,10 @@ static zend_op* _get_recv_op(zend_op_array *op_array, uint32_t offset)
/* }}} */
/* {{{ _parameter_string */
-static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg_info *arg_info,
uint32_t offset, uint32_t required, char* indent)
+static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg_info *arg_info,
uint32_t offset, zend_bool required, char* indent)
{
string_printf(str, "Parameter #%d [ ", offset);
- if (offset >= required) {
+ if (!required) {
string_printf(str, "<optional> ");
} else {
string_printf(str, "<required> ");
@@ -708,7 +708,7 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg
} else {
string_printf(str, "$param%d", offset);
}
- if (fptr->type == ZEND_USER_FUNCTION && offset >= required) {
+ if (fptr->type == ZEND_USER_FUNCTION && !required) {
zend_op *precv = _get_recv_op((zend_op_array*)fptr, offset);
if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type !=
IS_UNUSED) {
zval zv;
@@ -747,7 +747,7 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg
static void _function_parameter_string(string *str, zend_function *fptr, char* indent)
{
struct _zend_arg_info *arg_info = fptr->common.arg_info;
- uint32_t i, num_args, required = fptr->common.required_num_args;
+ uint32_t i, num_args, num_required = fptr->common.required_num_args;
if (!arg_info) {
return;
@@ -761,7 +761,7 @@ static void _function_parameter_string(string *str, zend_function *fptr, char* i
string_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
for (i = 0; i < num_args; i++) {
string_printf(str, "%s ", indent);
- _parameter_string(str, fptr, arg_info, i, required, indent);
+ _parameter_string(str, fptr, arg_info, i, i < num_required, indent);
string_write(str, "\n", sizeof("\n")-1);
arg_info++;
}
@@ -1229,7 +1229,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
/* }}} */
/* {{{ reflection_parameter_factory */
-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)
+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)
{
reflection_object *intern;
parameter_reference *reference;
@@ -2066,7 +2066,7 @@ ZEND_METHOD(reflection_function, returnsReference)
/* }}} */
/* {{{ proto public bool ReflectionFunction::getNumberOfParameters()
- Gets the number of required parameters */
+ Gets the number of parameters */
ZEND_METHOD(reflection_function, getNumberOfParameters)
{
reflection_object *intern;
@@ -2121,7 +2121,14 @@ ZEND_METHOD(reflection_function, getParameters)
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, fptr->common.required_num_args, ¶meter);
+ reflection_parameter_factory(
+ _copy_function(fptr),
+ Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
+ arg_info,
+ i,
+ i < fptr->common.required_num_args,
+ ¶meter
+ );
add_next_index_zval(return_value, ¶meter);
arg_info++;
@@ -2533,7 +2540,7 @@ ZEND_METHOD(reflection_parameter, __construct)
ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
ref->arg_info = &arg_info[position];
ref->offset = (uint32_t)position;
- ref->required = fptr->common.required_num_args;
+ ref->required = position < fptr->common.required_num_args;
ref->fptr = fptr;
/* TODO: copy fptr */
intern->ptr = ref;
@@ -2838,7 +2845,7 @@ ZEND_METHOD(reflection_parameter, isOptional)
}
GET_REFLECTION_OBJECT_PTR(param);
- RETVAL_BOOL(param->offset >= param->required);
+ RETVAL_BOOL(!param->required);
}
/* }}} */