Skip to content

Commit 47bdc4a

Browse files
committed
Parser now captures class constant doc comments, Reflection ClassConstant object now implements a getDocComment method
1 parent a7a45ae commit 47bdc4a

File tree

8 files changed

+60
-29
lines changed

8 files changed

+60
-29
lines changed

Zend/zend_API.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,7 +3724,7 @@ ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *nam
37243724
}
37253725
/* }}} */
37263726

3727-
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type) /* {{{ */
3727+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
37283728
{
37293729
zend_class_constant_info *const_info;
37303730

@@ -3740,6 +3740,7 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
37403740
const_info->name = zend_string_copy(name);
37413741
const_info->name = zend_new_interned_string(const_info->name);
37423742
const_info->flags = access_type;
3743+
const_info->doc_comment = doc_comment;
37433744
const_info->ce = ce;
37443745

37453746
ce->constants_table = perealloc(ce->constants_table, sizeof(zval) * ce->constants_count, ce->type == ZEND_INTERNAL_CLASS);
@@ -3759,108 +3760,108 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
37593760
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
37603761
{
37613762
zend_string *key = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3762-
int ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC);
3763+
int ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
37633764
zend_string_release(key);
37643765
return ret;
37653766
}
37663767
/* }}} */
37673768

3768-
ZEND_API int zend_declare_class_constant_null_ex(zend_class_entry *ce, zend_string *name, int access_type) /* {{{ */
3769+
ZEND_API int zend_declare_class_constant_null_ex(zend_class_entry *ce, zend_string *name, int access_type, zend_string *doc_comment) /* {{{ */
37693770
{
37703771
zval constant;
37713772

37723773
ZVAL_NULL(&constant);
3773-
return zend_declare_class_constant_ex(ce, name, &constant, access_type);
3774+
return zend_declare_class_constant_ex(ce, name, &constant, access_type, doc_comment);
37743775
}
37753776
/* }}} */
37763777

37773778
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
37783779
{
37793780
zend_string *propname = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3780-
int retval = zend_declare_class_constant_null_ex(ce, propname, ZEND_ACC_PUBLIC);
3781+
int retval = zend_declare_class_constant_null_ex(ce, propname, ZEND_ACC_PUBLIC, NULL);
37813782
zend_string_release(propname);
37823783
return retval;
37833784
}
37843785
/* }}} */
37853786

3786-
ZEND_API int zend_declare_class_constant_long_ex(zend_class_entry *ce, zend_string *name, zend_long value, int access_type) /* {{{ */
3787+
ZEND_API int zend_declare_class_constant_long_ex(zend_class_entry *ce, zend_string *name, zend_long value, int access_type, zend_string *doc_comment) /* {{{ */
37873788
{
37883789
zval constant;
37893790

37903791
ZVAL_LONG(&constant, value);
3791-
return zend_declare_class_constant_ex(ce, name, &constant, access_type);
3792+
return zend_declare_class_constant_ex(ce, name, &constant, access_type, doc_comment);
37923793
}
37933794
/* }}} */
37943795

37953796
ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
37963797
{
37973798

37983799
zend_string *propname = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3799-
int retval = zend_declare_class_constant_long_ex(ce, propname, value, ZEND_ACC_PUBLIC);
3800+
int retval = zend_declare_class_constant_long_ex(ce, propname, value, ZEND_ACC_PUBLIC, NULL);
38003801
zend_string_release(propname);
38013802
return retval;
38023803
}
38033804
/* }}} */
38043805

3805-
ZEND_API int zend_declare_class_constant_bool_ex(zend_class_entry *ce, zend_string *name, zend_bool value, int access_type) /* {{{ */
3806+
ZEND_API int zend_declare_class_constant_bool_ex(zend_class_entry *ce, zend_string *name, zend_bool value, int access_type, zend_string *doc_comment) /* {{{ */
38063807
{
38073808
zval constant;
38083809

38093810
ZVAL_BOOL(&constant, value);
3810-
return zend_declare_class_constant_ex(ce, name, &constant, access_type);
3811+
return zend_declare_class_constant_ex(ce, name, &constant, access_type, doc_comment);
38113812
}
38123813
/* }}} */
38133814

38143815
ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
38153816
{
38163817
zend_string *propname = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3817-
int retval = zend_declare_class_constant_bool_ex(ce, propname, value, ZEND_ACC_PUBLIC);
3818+
int retval = zend_declare_class_constant_bool_ex(ce, propname, value, ZEND_ACC_PUBLIC, NULL);
38183819
zend_string_release(propname);
38193820
return retval;
38203821
}
38213822
/* }}} */
38223823

38233824

3824-
ZEND_API int zend_declare_class_constant_double_ex(zend_class_entry *ce, zend_string *name, double value, int access_type) /* {{{ */
3825+
ZEND_API int zend_declare_class_constant_double_ex(zend_class_entry *ce, zend_string *name, double value, int access_type, zend_string *doc_comment) /* {{{ */
38253826
{
38263827
zval constant;
38273828

38283829
ZVAL_DOUBLE(&constant, value);
3829-
return zend_declare_class_constant_ex(ce, name, &constant, access_type);
3830+
return zend_declare_class_constant_ex(ce, name, &constant, access_type, doc_comment);
38303831
}
38313832
/* }}} */
38323833

38333834
ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
38343835
{
38353836
zend_string *propname = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3836-
int retval = zend_declare_class_constant_double_ex(ce, propname, value, ZEND_ACC_PUBLIC);
3837+
int retval = zend_declare_class_constant_double_ex(ce, propname, value, ZEND_ACC_PUBLIC, NULL);
38373838
zend_string_release(propname);
38383839
return retval;
38393840
}
38403841
/* }}} */
38413842

3842-
ZEND_API int zend_declare_class_constant_stringl_ex(zend_class_entry *ce, zend_string *name, const char *value, size_t value_length, int access_type) /* {{{ */
3843+
ZEND_API int zend_declare_class_constant_stringl_ex(zend_class_entry *ce, zend_string *name, const char *value, size_t value_length, int access_type, zend_string *doc_comment) /* {{{ */
38433844
{
38443845
zval constant;
38453846

38463847
ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
3847-
return zend_declare_class_constant_ex(ce, name, &constant, access_type);
3848+
return zend_declare_class_constant_ex(ce, name, &constant, access_type, doc_comment);
38483849
}
38493850
/* }}} */
38503851

38513852

38523853
ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
38533854
{
38543855
zend_string *propname = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3855-
int retval = zend_declare_class_constant_stringl_ex(ce, propname, value, value_length, ZEND_ACC_PUBLIC);
3856+
int retval = zend_declare_class_constant_stringl_ex(ce, propname, value, value_length, ZEND_ACC_PUBLIC, NULL);
38563857
zend_string_release(propname);
38573858
return retval;
38583859
}
38593860
/* }}} */
38603861

3861-
ZEND_API int zend_declare_class_constant_string_ex(zend_class_entry *ce, zend_string *name, const char *value, int access_type) /* {{{ */
3862+
ZEND_API int zend_declare_class_constant_string_ex(zend_class_entry *ce, zend_string *name, const char *value, int access_type, zend_string *doc_comment) /* {{{ */
38623863
{
3863-
return zend_declare_class_constant_stringl_ex(ce, name, value, strlen(value), access_type);
3864+
return zend_declare_class_constant_stringl_ex(ce, name, value, strlen(value), access_type, doc_comment);
38643865
}
38653866
/* }}} */
38663867

Zend/zend_API.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,19 @@ ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name
324324
ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type);
325325
ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type);
326326

327-
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type);
327+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment);
328328
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value);
329-
ZEND_API int zend_declare_class_constant_null_ex(zend_class_entry *ce, zend_string *name, int access_type);
329+
ZEND_API int zend_declare_class_constant_null_ex(zend_class_entry *ce, zend_string *name, int access_type, zend_string *doc_comment);
330330
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length);
331-
ZEND_API int zend_declare_class_constant_long_ex(zend_class_entry *ce, zend_string *name, zend_long value, int access_type);
331+
ZEND_API int zend_declare_class_constant_long_ex(zend_class_entry *ce, zend_string *name, zend_long value, int access_type, zend_string *doc_comment);
332332
ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value);
333-
ZEND_API int zend_declare_class_constant_bool_ex(zend_class_entry *ce, zend_string *name, zend_bool value, int access_type);
333+
ZEND_API int zend_declare_class_constant_bool_ex(zend_class_entry *ce, zend_string *name, zend_bool value, int access_type, zend_string *doc_comment);
334334
ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value);
335-
ZEND_API int zend_declare_class_constant_double_ex(zend_class_entry *ce, zend_string *name, double value, int access_type);
335+
ZEND_API int zend_declare_class_constant_double_ex(zend_class_entry *ce, zend_string *name, double value, int access_type, zend_string *doc_comment);
336336
ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value);
337-
ZEND_API int zend_declare_class_constant_stringl_ex(zend_class_entry *ce, zend_string *name, const char *value, size_t value_length, int access_type);
337+
ZEND_API int zend_declare_class_constant_stringl_ex(zend_class_entry *ce, zend_string *name, const char *value, size_t value_length, int access_type, zend_string *doc_comment);
338338
ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length);
339-
ZEND_API int zend_declare_class_constant_string_ex(zend_class_entry *ce, zend_string *name, const char *value, int access_type);
339+
ZEND_API int zend_declare_class_constant_string_ex(zend_class_entry *ce, zend_string *name, const char *value, int access_type, zend_string *doc_comment);
340340
ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value);
341341

342342
ZEND_API int zend_update_class_constants(zend_class_entry *class_type);

Zend/zend_ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ enum _zend_ast_kind {
124124
ZEND_AST_SWITCH,
125125
ZEND_AST_SWITCH_CASE,
126126
ZEND_AST_DECLARE,
127-
ZEND_AST_CONST_ELEM,
128127
ZEND_AST_USE_TRAIT,
129128
ZEND_AST_TRAIT_PRECEDENCE,
130129
ZEND_AST_METHOD_REFERENCE,
@@ -142,6 +141,7 @@ enum _zend_ast_kind {
142141
ZEND_AST_CATCH,
143142
ZEND_AST_PARAM,
144143
ZEND_AST_PROP_ELEM,
144+
ZEND_AST_CONST_ELEM,
145145

146146
/* 4 child nodes */
147147
ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,

Zend/zend_compile.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4979,13 +4979,19 @@ void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
49794979
zend_ast *const_ast = list->child[i];
49804980
zend_ast *name_ast = const_ast->child[0];
49814981
zend_ast *value_ast = const_ast->child[1];
4982+
zend_ast *doc_comment_ast = const_ast->child[2];
49824983
zend_string *name = zend_ast_get_str(name_ast);
4984+
zend_string *doc_comment = NULL;
49834985
zval value_zv;
49844986

49854987
zend_const_expr_to_zval(&value_zv, value_ast);
49864988
name = zend_new_interned_string_safe(name);
49874989

4988-
zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr);
4990+
if (doc_comment_ast) {
4991+
doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
4992+
}
4993+
4994+
zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment);
49894995
}
49904996
}
49914997
/* }}} */

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ typedef struct _zend_class_constant_info {
300300
uint32_t offset; /* offset for values in (zend_class_entry*)->constants_table */
301301
uint32_t flags;
302302
zend_string *name;
303+
zend_string *doc_comment;
303304
zend_class_entry *ce;
304305
} zend_class_constant_info;
305306

Zend/zend_language_parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ class_const_list:
810810
;
811811

812812
class_const_decl:
813-
identifier '=' expr { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3); }
813+
identifier '=' expr backup_doc_comment { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3, ($4 ? zend_ast_create_zval_from_str($4) : NULL)); }
814814
;
815815

816816
const_decl:

Zend/zend_opcode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ ZEND_API void destroy_zend_class(zval *zv)
298298
ZEND_HASH_FOREACH_PTR(&ce->constants_info, const_info) {
299299
if (const_info->ce == ce || (const_info->flags & ZEND_ACC_SHADOW)) {
300300
zend_string_release(const_info->name);
301+
if (const_info->doc_comment) {
302+
zend_string_release(const_info->doc_comment);
303+
}
301304
}
302305
} ZEND_HASH_FOREACH_END();
303306
zend_hash_destroy(&ce->properties_info);

ext/reflection/php_reflection.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,6 +5755,25 @@ ZEND_METHOD(reflection_class_constant, getDeclaringClass)
57555755
}
57565756
/* }}} */
57575757

5758+
/* {{{ proto public string ReflectionClassConstant::getDocComment()
5759+
Returns the doc comment for this function */
5760+
ZEND_METHOD(reflection_class_constant, getDocComment)
5761+
{
5762+
reflection_object *intern;
5763+
class_constant_reference *ref;
5764+
5765+
if (zend_parse_parameters_none() == FAILURE) {
5766+
return;
5767+
}
5768+
GET_REFLECTION_OBJECT_PTR(ref);
5769+
if (ref->const_info.doc_comment) {
5770+
RETURN_STR_COPY(ref->const_info.doc_comment);
5771+
}
5772+
RETURN_FALSE;
5773+
}
5774+
/* }}} */
5775+
5776+
57585777
/* {{{ proto public static mixed ReflectionExtension::export(string name [, bool return]) throws ReflectionException
57595778
Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
57605779
ZEND_METHOD(reflection_extension, export)
@@ -6654,6 +6673,7 @@ static const zend_function_entry reflection_class_constant_functions[] = {
66546673
ZEND_ME(reflection_class_constant, isProtected, arginfo_reflection__void, 0)
66556674
ZEND_ME(reflection_class_constant, getModifiers, arginfo_reflection__void, 0)
66566675
ZEND_ME(reflection_class_constant, getDeclaringClass, arginfo_reflection__void, 0)
6676+
ZEND_ME(reflection_class_constant, getDocComment, arginfo_reflection__void, 0)
66576677
PHP_FE_END
66586678
};
66596679

0 commit comments

Comments
 (0)