Skip to content

Commit 47adcc3

Browse files
committed
Extracted duplicated code into a function. Fixed annotation type.
1 parent 79eb355 commit 47adcc3

File tree

2 files changed

+72
-142
lines changed

2 files changed

+72
-142
lines changed

Zend/tests/attributes/objects.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ string(7) "ERROR 4"
9999
string(50) "Attribute constructor of class 'A3' must be public"
100100

101101
string(7) "ERROR 5"
102-
string(72) "Annotation class 'A4' does not have a constructor, cannot pass arguments"
102+
string(71) "Attribute class 'A4' does not have a constructor, cannot pass arguments"

ext/reflection/php_reflection.c

Lines changed: 71 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ static void reflection_attribute_factory(zval *object, zend_string *name, zval *
10551055
}
10561056
/* }}} */
10571057

1058-
static int convert_ast_to_zval(zval *ret, zend_ast *ast, zend_class_entry *scope_ce)
1058+
static int convert_ast_to_zval(zval *ret, zend_ast *ast, zend_class_entry *scope_ce) /* {{{ */
10591059
{
10601060
if (ast->kind == ZEND_AST_CONSTANT) {
10611061
zend_string *name = zend_ast_get_constant_name(ast);
@@ -1079,8 +1079,9 @@ static int convert_ast_to_zval(zval *ret, zend_ast *ast, zend_class_entry *scope
10791079

10801080
return SUCCESS;
10811081
}
1082+
/* }}} */
10821083

1083-
static int convert_ast_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope_ce)
1084+
static int convert_ast_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope_ce) /* {{{ */
10841085
{
10851086
Bucket *p;
10861087
zval tmp;
@@ -1106,8 +1107,9 @@ static int convert_ast_attributes(zval *ret, HashTable *attributes, zend_class_e
11061107

11071108
return SUCCESS;
11081109
}
1110+
/* }}} */
11091111

1110-
static int load_attributes(zval *ret, HashTable *attr, zend_class_entry *scope)
1112+
static int load_attributes(zval *ret, HashTable *attr, zend_class_entry *scope) /* {{{ */
11111113
{
11121114
zval obj;
11131115
zval result;
@@ -1144,9 +1146,10 @@ static int load_attributes(zval *ret, HashTable *attr, zend_class_entry *scope)
11441146

11451147
return SUCCESS;
11461148
}
1149+
/* }}} */
11471150

11481151
static int convert_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope,
1149-
zend_string *name, zend_class_entry *base)
1152+
zend_string *name, zend_class_entry *base) /* {{{ */
11501153
{
11511154
Bucket *p;
11521155

@@ -1167,7 +1170,7 @@ static int convert_attributes(zval *ret, HashTable *attributes, zend_class_entry
11671170
} else {
11681171
ZEND_HASH_FOREACH_BUCKET(attributes, p) {
11691172
if (!p->key) {
1170-
// Skip inlined parameter annotations.
1173+
// Skip inlined parameter attributes.
11711174
continue;
11721175
}
11731176

@@ -1205,6 +1208,43 @@ static int convert_attributes(zval *ret, HashTable *attributes, zend_class_entry
12051208

12061209
return SUCCESS;
12071210
}
1211+
/* }}} */
1212+
1213+
static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attributes, zend_class_entry *scope) /* {{{ */
1214+
{
1215+
zval ret;
1216+
1217+
zend_string *name = NULL;
1218+
zend_bool inherited = 0;
1219+
zend_class_entry *base = NULL;
1220+
1221+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
1222+
RETURN_THROWS();
1223+
}
1224+
1225+
if (name && inherited) {
1226+
if (NULL == (base = zend_lookup_class(name))) {
1227+
if (!EG(exception)) {
1228+
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
1229+
}
1230+
1231+
RETURN_THROWS();
1232+
}
1233+
1234+
name = NULL;
1235+
}
1236+
1237+
if (!attributes) {
1238+
RETURN_EMPTY_ARRAY();
1239+
}
1240+
1241+
if (FAILURE == convert_attributes(&ret, attributes, scope, name, base)) {
1242+
RETURN_THROWS();
1243+
}
1244+
1245+
RETURN_ZVAL(&ret, 1, 1);
1246+
}
1247+
/* }}} */
12081248

12091249
static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
12101250
{
@@ -1818,39 +1858,17 @@ ZEND_METHOD(reflection_function, getAttributes)
18181858
reflection_object *intern;
18191859
zend_function *fptr;
18201860

1821-
zend_string *name = NULL;
1822-
zend_bool inherited = 0;
1823-
zend_class_entry *base = NULL;
1824-
1825-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
1826-
RETURN_THROWS();
1827-
}
1828-
1829-
if (name && inherited) {
1830-
if (NULL == (base = zend_lookup_class(name))) {
1831-
if (!EG(exception)) {
1832-
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
1833-
}
1834-
1835-
RETURN_THROWS();
1836-
}
1837-
1838-
name = NULL;
1839-
}
1861+
HashTable *attributes = NULL;
1862+
zend_class_entry *scope = NULL;
18401863

18411864
GET_REFLECTION_OBJECT_PTR(fptr);
18421865

18431866
if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.attributes) {
1844-
zval ret;
1845-
1846-
if (FAILURE == convert_attributes(&ret, fptr->op_array.attributes, fptr->common.scope, name, base)) {
1847-
RETURN_THROWS();
1848-
}
1849-
1850-
RETURN_ZVAL(&ret, 1, 1);
1867+
attributes = fptr->op_array.attributes;
1868+
scope = fptr->common.scope;
18511869
}
18521870

1853-
RETURN_EMPTY_ARRAY();
1871+
reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU, attributes, scope);
18541872
}
18551873
/* }}} */
18561874

@@ -2783,45 +2801,23 @@ ZEND_METHOD(reflection_parameter, getAttributes)
27832801
reflection_object *intern;
27842802
parameter_reference *param;
27852803

2786-
zend_string *name = NULL;
2787-
zend_bool inherited = 0;
2788-
zend_class_entry *base = NULL;
2789-
2790-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
2791-
RETURN_THROWS();
2792-
}
2793-
2794-
if (name && inherited) {
2795-
if (NULL == (base = zend_lookup_class(name))) {
2796-
if (!EG(exception)) {
2797-
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
2798-
}
2799-
2800-
RETURN_THROWS();
2801-
}
2802-
2803-
name = NULL;
2804-
}
2804+
HashTable *attributes = NULL;
2805+
zend_class_entry *scope = NULL;
28052806

28062807
GET_REFLECTION_OBJECT_PTR(param);
28072808

28082809
if (param->fptr->type == ZEND_USER_FUNCTION && param->fptr->op_array.attributes) {
28092810
zval *attr;
28102811

28112812
if (NULL != (attr = zend_hash_index_find(param->fptr->op_array.attributes, param->offset))) {
2812-
zval ret;
2813-
28142813
ZEND_ASSERT(Z_TYPE_P(attr) == IS_ARRAY);
28152814

2816-
if (FAILURE == convert_attributes(&ret, Z_ARRVAL_P(attr), param->fptr->common.scope, name, base)) {
2817-
RETURN_THROWS();
2818-
}
2819-
2820-
RETURN_ZVAL(&ret, 1, 1);
2815+
attributes = Z_ARRVAL_P(attr);
2816+
scope = param->fptr->common.scope;
28212817
}
28222818
}
28232819

2824-
RETURN_EMPTY_ARRAY();
2820+
reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU, attributes, scope);
28252821
}
28262822

28272823
/* {{{ proto public bool ReflectionParameter::getPosition()
@@ -3893,39 +3889,17 @@ ZEND_METHOD(reflection_class_constant, getAttributes)
38933889
reflection_object *intern;
38943890
zend_class_constant *ref;
38953891

3896-
zend_string *name = NULL;
3897-
zend_bool inherited = 0;
3898-
zend_class_entry *base = NULL;
3899-
3900-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
3901-
RETURN_THROWS();
3902-
}
3903-
3904-
if (name && inherited) {
3905-
if (NULL == (base = zend_lookup_class(name))) {
3906-
if (!EG(exception)) {
3907-
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
3908-
}
3909-
3910-
RETURN_THROWS();
3911-
}
3912-
3913-
name = NULL;
3914-
}
3892+
HashTable *attributes = NULL;
3893+
zend_class_entry *scope = NULL;
39153894

39163895
GET_REFLECTION_OBJECT_PTR(ref);
39173896

39183897
if (ref->attributes) {
3919-
zval ret;
3920-
3921-
if (FAILURE == convert_attributes(&ret, ref->attributes, ref->ce, name, base)) {
3922-
RETURN_THROWS();
3923-
}
3924-
3925-
RETURN_ZVAL(&ret, 1, 1);
3898+
attributes = ref->attributes;
3899+
scope = ref->ce;
39263900
}
39273901

3928-
RETURN_EMPTY_ARRAY();
3902+
reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU, attributes, scope);
39293903
}
39303904
/* }}} */
39313905

@@ -4298,39 +4272,17 @@ ZEND_METHOD(reflection_class, getAttributes)
42984272
reflection_object *intern;
42994273
zend_class_entry *ce;
43004274

4301-
zend_string *name = NULL;
4302-
zend_bool inherited = 0;
4303-
zend_class_entry *base = NULL;
4304-
4305-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
4306-
RETURN_THROWS();
4307-
}
4308-
4309-
if (name && inherited) {
4310-
if (NULL == (base = zend_lookup_class(name))) {
4311-
if (!EG(exception)) {
4312-
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
4313-
}
4314-
4315-
RETURN_THROWS();
4316-
}
4317-
4318-
name = NULL;
4319-
}
4275+
HashTable *attributes = NULL;
4276+
zend_class_entry *scope = NULL;
43204277

43214278
GET_REFLECTION_OBJECT_PTR(ce);
43224279

43234280
if (ce->type == ZEND_USER_CLASS && ce->info.user.attributes) {
4324-
zval ret;
4325-
4326-
if (FAILURE == convert_attributes(&ret, ce->info.user.attributes, ce, name, base)) {
4327-
RETURN_THROWS();
4328-
}
4329-
4330-
RETURN_ZVAL(&ret, 1, 1);
4281+
attributes = ce->info.user.attributes;
4282+
scope = ce;
43314283
}
43324284

4333-
RETURN_EMPTY_ARRAY();
4285+
reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU, attributes, scope);
43344286
}
43354287
/* }}} */
43364288

@@ -5844,39 +5796,17 @@ ZEND_METHOD(reflection_property, getAttributes)
58445796
reflection_object *intern;
58455797
property_reference *ref;
58465798

5847-
zend_string *name = NULL;
5848-
zend_bool inherited = 0;
5849-
zend_class_entry *base = NULL;
5850-
5851-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
5852-
RETURN_THROWS();
5853-
}
5854-
5855-
if (name && inherited) {
5856-
if (NULL == (base = zend_lookup_class(name))) {
5857-
if (!EG(exception)) {
5858-
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
5859-
}
5860-
5861-
RETURN_THROWS();
5862-
}
5863-
5864-
name = NULL;
5865-
}
5799+
HashTable *attributes = NULL;
5800+
zend_class_entry *scope = NULL;
58665801

58675802
GET_REFLECTION_OBJECT_PTR(ref);
58685803

58695804
if (ref->prop->attributes) {
5870-
zval ret;
5871-
5872-
if (FAILURE == convert_attributes(&ret, ref->prop->attributes, ref->prop->ce, name, base)) {
5873-
RETURN_THROWS();
5874-
}
5875-
5876-
RETURN_ZVAL(&ret, 1, 1);
5805+
attributes = ref->prop->attributes;
5806+
scope = ref->prop->ce;
58775807
}
58785808

5879-
RETURN_EMPTY_ARRAY();
5809+
reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU, attributes, scope);
58805810
}
58815811
/* }}} */
58825812

@@ -6756,7 +6686,7 @@ ZEND_METHOD(reflection_attribute, getAsObject)
67566686
}
67576687
} else if (argc) {
67586688
attribute_ctor_cleanup(&obj, args, argc);
6759-
zend_throw_error(NULL, "Annotation class '%s' does not have a constructor, cannot pass arguments", ZSTR_VAL(ce->name));
6689+
zend_throw_error(NULL, "Attribute class '%s' does not have a constructor, cannot pass arguments", ZSTR_VAL(ce->name));
67606690
RETURN_THROWS();
67616691
}
67626692

0 commit comments

Comments
 (0)