Skip to content

Commit 6e5dc76

Browse files
Reflection: use early returns to reduce indentation
1 parent 75064bb commit 6e5dc76

1 file changed

Lines changed: 151 additions & 156 deletions

File tree

ext/reflection/php_reflection.c

Lines changed: 151 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,9 @@ static zend_function *_copy_function(zend_function *fptr) /* {{{ */
199199
memcpy(copy_fptr, fptr, sizeof(zend_function));
200200
copy_fptr->common.function_name = zend_string_copy(fptr->common.function_name);
201201
return copy_fptr;
202-
} else {
203-
/* no copy needed */
204-
return fptr;
205202
}
203+
/* no copy needed */
204+
return fptr;
206205
}
207206
/* }}} */
208207

@@ -947,11 +946,11 @@ static zval *property_get_default(const zend_property_info *prop_info) {
947946
zval *prop = &ce->default_static_members_table[prop_info->offset];
948947
ZVAL_DEINDIRECT(prop);
949948
return prop;
950-
} else if (prop_info->flags & ZEND_ACC_VIRTUAL) {
949+
}
950+
if (prop_info->flags & ZEND_ACC_VIRTUAL) {
951951
return NULL;
952-
} else {
953-
return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
954952
}
953+
return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
955954
}
956955

957956
/* {{{ _property_string */
@@ -1048,32 +1047,33 @@ static void _property_string(smart_str *str, const zend_property_info *prop, con
10481047

10491048
static void _extension_ini_string(const zend_ini_entry *ini_entry, smart_str *str, int number) /* {{{ */
10501049
{
1051-
if (number == ini_entry->module_number) {
1052-
smart_str_append_printf(str, " Entry [ %s <", ZSTR_VAL(ini_entry->name));
1053-
if (ini_entry->modifiable == ZEND_INI_ALL) {
1054-
smart_str_appends(str, "ALL");
1055-
} else {
1056-
char *comma = "";
1057-
if (ini_entry->modifiable & ZEND_INI_USER) {
1058-
smart_str_appends(str, "USER");
1059-
comma = ",";
1060-
}
1061-
if (ini_entry->modifiable & ZEND_INI_PERDIR) {
1062-
smart_str_append_printf(str, "%sPERDIR", comma);
1063-
comma = ",";
1064-
}
1065-
if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
1066-
smart_str_append_printf(str, "%sSYSTEM", comma);
1067-
}
1050+
if (number != ini_entry->module_number) {
1051+
return;
1052+
}
1053+
smart_str_append_printf(str, " Entry [ %s <", ZSTR_VAL(ini_entry->name));
1054+
if (ini_entry->modifiable == ZEND_INI_ALL) {
1055+
smart_str_appends(str, "ALL");
1056+
} else {
1057+
char *comma = "";
1058+
if (ini_entry->modifiable & ZEND_INI_USER) {
1059+
smart_str_appends(str, "USER");
1060+
comma = ",";
10681061
}
1069-
1070-
smart_str_appends(str, "> ]\n");
1071-
smart_str_append_printf(str, " Current = '%s'\n", ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
1072-
if (ini_entry->modified) {
1073-
smart_str_append_printf(str, " Default = '%s'\n", ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
1062+
if (ini_entry->modifiable & ZEND_INI_PERDIR) {
1063+
smart_str_append_printf(str, "%sPERDIR", comma);
1064+
comma = ",";
1065+
}
1066+
if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
1067+
smart_str_append_printf(str, "%sSYSTEM", comma);
10741068
}
1075-
smart_str_appends(str, " }\n");
10761069
}
1070+
1071+
smart_str_appends(str, "> ]\n");
1072+
smart_str_append_printf(str, " Current = '%s'\n", ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
1073+
if (ini_entry->modified) {
1074+
smart_str_append_printf(str, " Default = '%s'\n", ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
1075+
}
1076+
smart_str_appends(str, " }\n");
10771077
}
10781078
/* }}} */
10791079

@@ -1572,15 +1572,14 @@ static zend_result get_parameter_default(zval *result, const parameter_reference
15721572
return FAILURE;
15731573
}
15741574
return zend_get_default_from_internal_arg_info(result, param->arg_info);
1575-
} else {
1576-
zval *default_value = get_default_from_recv((const zend_op_array *) param->fptr, param->offset);
1577-
if (!default_value) {
1578-
return FAILURE;
1579-
}
1580-
1581-
ZVAL_COPY(result, default_value);
1582-
return SUCCESS;
15831575
}
1576+
zval *default_value = get_default_from_recv((const zend_op_array *) param->fptr, param->offset);
1577+
if (!default_value) {
1578+
return FAILURE;
1579+
}
1580+
1581+
ZVAL_COPY(result, default_value);
1582+
return SUCCESS;
15841583
}
15851584

15861585
/* {{{ Preventing __clone from being called */
@@ -3283,26 +3282,25 @@ ZEND_METHOD(ReflectionMethod, getClosure)
32833282

32843283
if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
32853284
zend_create_fake_closure(return_value, mptr, mptr->common.scope, mptr->common.scope, NULL);
3286-
} else {
3287-
if (!obj) {
3288-
zend_argument_value_error(1, "cannot be null for non-static methods");
3289-
RETURN_THROWS();
3290-
}
3285+
return;
3286+
}
3287+
if (!obj) {
3288+
zend_argument_value_error(1, "cannot be null for non-static methods");
3289+
RETURN_THROWS();
3290+
}
32913291

3292-
if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope)) {
3293-
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this method was declared in", 0);
3294-
RETURN_THROWS();
3295-
}
3292+
if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope)) {
3293+
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this method was declared in", 0);
3294+
RETURN_THROWS();
3295+
}
32963296

3297-
/* This is an original closure object and __invoke is to be called. */
3298-
if (Z_OBJCE_P(obj) == zend_ce_closure &&
3299-
(mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
3300-
{
3301-
RETURN_OBJ_COPY(Z_OBJ_P(obj));
3302-
} else {
3303-
zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE_P(obj), obj);
3304-
}
3297+
/* This is an original closure object and __invoke is to be called. */
3298+
if (Z_OBJCE_P(obj) == zend_ce_closure &&
3299+
(mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
3300+
{
3301+
RETURN_OBJ_COPY(Z_OBJ_P(obj));
33053302
}
3303+
zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE_P(obj), obj);
33063304
}
33073305
/* }}} */
33083306

@@ -4456,25 +4454,26 @@ ZEND_METHOD(ReflectionClass, getMethods)
44564454
_addmethod(mptr, ce, Z_ARRVAL_P(return_value), filter);
44574455
} ZEND_HASH_FOREACH_END();
44584456

4459-
if (ce == zend_ce_closure) {
4460-
bool has_obj = Z_TYPE(intern->obj) != IS_UNDEF;
4461-
zval obj_tmp;
4462-
zend_object *obj;
4463-
if (!has_obj) {
4464-
object_init_ex(&obj_tmp, ce);
4465-
obj = Z_OBJ(obj_tmp);
4466-
} else {
4467-
obj = Z_OBJ(intern->obj);
4468-
}
4469-
zend_function *closure = zend_get_closure_invoke_method(obj);
4470-
if (closure
4471-
&& !_addmethod(closure, ce, Z_ARRVAL_P(return_value), filter)
4472-
) {
4473-
_free_function(closure);
4474-
}
4475-
if (!has_obj) {
4476-
zval_ptr_dtor(&obj_tmp);
4477-
}
4457+
if (ce != zend_ce_closure) {
4458+
return;
4459+
}
4460+
bool has_obj = Z_TYPE(intern->obj) != IS_UNDEF;
4461+
zval obj_tmp;
4462+
zend_object *obj;
4463+
if (!has_obj) {
4464+
object_init_ex(&obj_tmp, ce);
4465+
obj = Z_OBJ(obj_tmp);
4466+
} else {
4467+
obj = Z_OBJ(intern->obj);
4468+
}
4469+
zend_function *closure = zend_get_closure_invoke_method(obj);
4470+
if (closure
4471+
&& !_addmethod(closure, ce, Z_ARRVAL_P(return_value), filter)
4472+
) {
4473+
_free_function(closure);
4474+
}
4475+
if (!has_obj) {
4476+
zval_ptr_dtor(&obj_tmp);
44784477
}
44794478
}
44804479
/* }}} */
@@ -5309,41 +5308,38 @@ ZEND_METHOD(ReflectionClass, getTraitAliases)
53095308
ZEND_PARSE_PARAMETERS_NONE();
53105309
GET_REFLECTION_OBJECT_PTR(ce);
53115310

5311+
if (!ce->trait_aliases) {
5312+
RETURN_EMPTY_ARRAY();
5313+
}
5314+
5315+
array_init(return_value);
5316+
for (uint32_t i = 0; ce->trait_aliases[i]; i++) {
5317+
zend_trait_method_reference *cur_ref = &ce->trait_aliases[i]->trait_method;
53125318

5313-
if (ce->trait_aliases) {
5314-
uint32_t i = 0;
5319+
if (!ce->trait_aliases[i]->alias) {
5320+
continue;
5321+
}
5322+
zend_string *class_name = cur_ref->class_name;
53155323

5316-
array_init(return_value);
5317-
while (ce->trait_aliases[i]) {
5318-
zend_trait_method_reference *cur_ref = &ce->trait_aliases[i]->trait_method;
5319-
5320-
if (ce->trait_aliases[i]->alias) {
5321-
zend_string *class_name = cur_ref->class_name;
5322-
5323-
if (!class_name) {
5324-
zend_string *lcname = zend_string_tolower(cur_ref->method_name);
5325-
5326-
for (uint32_t j = 0; j < ce->num_traits; j++) {
5327-
zend_class_entry *trait =
5328-
zend_hash_find_ptr(CG(class_table), ce->trait_names[j].lc_name);
5329-
ZEND_ASSERT(trait && "Trait must exist");
5330-
if (zend_hash_exists(&trait->function_table, lcname)) {
5331-
class_name = trait->name;
5332-
break;
5333-
}
5334-
}
5335-
zend_string_release_ex(lcname, false);
5336-
ZEND_ASSERT(class_name != NULL);
5337-
}
5324+
if (!class_name) {
5325+
zend_string *lcname = zend_string_tolower(cur_ref->method_name);
53385326

5339-
zend_string *mname = zend_string_alloc(ZSTR_LEN(class_name) + ZSTR_LEN(cur_ref->method_name) + 2, false);
5340-
snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(cur_ref->method_name));
5341-
add_assoc_str_ex(return_value, ZSTR_VAL(ce->trait_aliases[i]->alias), ZSTR_LEN(ce->trait_aliases[i]->alias), mname);
5327+
for (uint32_t j = 0; j < ce->num_traits; j++) {
5328+
zend_class_entry *trait =
5329+
zend_hash_find_ptr(CG(class_table), ce->trait_names[j].lc_name);
5330+
ZEND_ASSERT(trait && "Trait must exist");
5331+
if (zend_hash_exists(&trait->function_table, lcname)) {
5332+
class_name = trait->name;
5333+
break;
5334+
}
53425335
}
5343-
i++;
5336+
zend_string_release_ex(lcname, false);
5337+
ZEND_ASSERT(class_name != NULL);
53445338
}
5345-
} else {
5346-
RETURN_EMPTY_ARRAY();
5339+
5340+
zend_string *mname = zend_string_alloc(ZSTR_LEN(class_name) + ZSTR_LEN(cur_ref->method_name) + 2, false);
5341+
snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(cur_ref->method_name));
5342+
add_assoc_str_ex(return_value, ZSTR_VAL(ce->trait_aliases[i]->alias), ZSTR_LEN(ce->trait_aliases[i]->alias), mname);
53475343
}
53485344
}
53495345
/* }}} */
@@ -5785,44 +5781,44 @@ ZEND_METHOD(ReflectionProperty, getValue)
57855781
if (member_p) {
57865782
RETURN_COPY_DEREF(member_p);
57875783
}
5788-
} else {
5789-
if (!object) {
5790-
zend_argument_type_error(1, "must be provided for instance properties");
5791-
RETURN_THROWS();
5792-
}
5784+
return;
5785+
}
5786+
if (!object) {
5787+
zend_argument_type_error(1, "must be provided for instance properties");
5788+
RETURN_THROWS();
5789+
}
57935790

5794-
/* TODO: Should this always use intern->ce? */
5795-
if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
5796-
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this property was declared in", 0);
5797-
RETURN_THROWS();
5798-
}
5791+
/* TODO: Should this always use intern->ce? */
5792+
if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
5793+
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this property was declared in", 0);
5794+
RETURN_THROWS();
5795+
}
57995796

5800-
if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
5801-
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];
5797+
if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
5798+
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];
58025799

5803-
if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
5804-
zval *retval = OBJ_PROP(Z_OBJ_P(object), prop_offset);
5805-
if (EXPECTED(!Z_ISUNDEF_P(retval))) {
5806-
RETURN_COPY_DEREF(retval);
5807-
}
5800+
if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
5801+
zval *retval = OBJ_PROP(Z_OBJ_P(object), prop_offset);
5802+
if (EXPECTED(!Z_ISUNDEF_P(retval))) {
5803+
RETURN_COPY_DEREF(retval);
58085804
}
58095805
}
5806+
}
58105807

5811-
zval rv;
5812-
const zend_class_entry *old_scope = EG(fake_scope);
5813-
EG(fake_scope) = intern->ce;
5814-
zval *member_p = Z_OBJ_P(object)->handlers->read_property(Z_OBJ_P(object),
5815-
ref->unmangled_name, BP_VAR_R, ref->cache_slot, &rv);
5816-
EG(fake_scope) = old_scope;
5808+
zval rv;
5809+
const zend_class_entry *old_scope = EG(fake_scope);
5810+
EG(fake_scope) = intern->ce;
5811+
zval *member_p = Z_OBJ_P(object)->handlers->read_property(Z_OBJ_P(object),
5812+
ref->unmangled_name, BP_VAR_R, ref->cache_slot, &rv);
5813+
EG(fake_scope) = old_scope;
58175814

5818-
if (member_p != &rv) {
5819-
RETURN_COPY_DEREF(member_p);
5820-
} else {
5821-
if (Z_ISREF_P(member_p)) {
5822-
zend_unwrap_reference(member_p);
5823-
}
5824-
RETURN_COPY_VALUE(member_p);
5815+
if (member_p != &rv) {
5816+
RETURN_COPY_DEREF(member_p);
5817+
} else {
5818+
if (Z_ISREF_P(member_p)) {
5819+
zend_unwrap_reference(member_p);
58255820
}
5821+
RETURN_COPY_VALUE(member_p);
58265822
}
58275823
}
58285824
/* }}} */
@@ -6195,35 +6191,34 @@ ZEND_METHOD(ReflectionProperty, isInitialized)
61956191
RETURN_BOOL(!Z_ISUNDEF_P(member_p));
61966192
}
61976193
RETURN_FALSE;
6198-
} else {
6199-
if (!object) {
6200-
zend_argument_type_error(1, "must be provided for instance properties");
6201-
RETURN_THROWS();
6202-
}
6194+
}
6195+
if (!object) {
6196+
zend_argument_type_error(1, "must be provided for instance properties");
6197+
RETURN_THROWS();
6198+
}
62036199

6204-
/* TODO: Should this always use intern->ce? */
6205-
if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
6206-
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this property was declared in", 0);
6207-
RETURN_THROWS();
6208-
}
6200+
/* TODO: Should this always use intern->ce? */
6201+
if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
6202+
zend_throw_exception(reflection_exception_ptr, "Given object is not an instance of the class this property was declared in", 0);
6203+
RETURN_THROWS();
6204+
}
62096205

6210-
if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
6211-
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];
6206+
if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
6207+
uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];
62126208

6213-
if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
6214-
zval *value = OBJ_PROP(Z_OBJ_P(object), prop_offset);
6215-
RETURN_BOOL(!Z_ISUNDEF_P(value));
6216-
}
6209+
if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
6210+
zval *value = OBJ_PROP(Z_OBJ_P(object), prop_offset);
6211+
RETURN_BOOL(!Z_ISUNDEF_P(value));
62176212
}
6213+
}
62186214

6219-
const zend_class_entry *old_scope = EG(fake_scope);
6220-
EG(fake_scope) = intern->ce;
6221-
int retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object),
6222-
ref->unmangled_name, ZEND_PROPERTY_EXISTS, ref->cache_slot);
6223-
EG(fake_scope) = old_scope;
6215+
const zend_class_entry *old_scope = EG(fake_scope);
6216+
EG(fake_scope) = intern->ce;
6217+
int retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object),
6218+
ref->unmangled_name, ZEND_PROPERTY_EXISTS, ref->cache_slot);
6219+
EG(fake_scope) = old_scope;
62246220

6225-
RETVAL_BOOL(retval);
6226-
}
6221+
RETVAL_BOOL(retval);
62276222
}
62286223
/* }}} */
62296224

0 commit comments

Comments
 (0)