Skip to content

Commit 4b405d8

Browse files
authored
Display class constant and property doc comments via reflection (#13499)
1 parent db6a567 commit 4b405d8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

ext/reflection/php_reflection.c

+7
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ static void _class_const_string(smart_str *str, zend_string *name, zend_class_co
566566
const char *final = ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_FINAL ? "final " : "";
567567
zend_string *type_str = ZEND_TYPE_IS_SET(c->type) ? zend_type_to_string(c->type) : NULL;
568568
const char *type = type_str ? ZSTR_VAL(type_str) : zend_zval_type_name(&c->value);
569+
570+
if (c->doc_comment) {
571+
smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(c->doc_comment));
572+
}
569573
smart_str_append_printf(str, "%sConstant [ %s%s %s %s ] { ",
570574
indent, final, visibility, type, ZSTR_VAL(name));
571575
if (Z_TYPE(c->value) == IS_ARRAY) {
@@ -889,6 +893,9 @@ static zval *property_get_default(zend_property_info *prop_info) {
889893
/* {{{ _property_string */
890894
static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
891895
{
896+
if (prop && prop->doc_comment) {
897+
smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(prop->doc_comment));
898+
}
892899
smart_str_append_printf(str, "%sProperty [ ", indent);
893900
if (!prop) {
894901
smart_str_append_printf(str, "<dynamic> public $%s", prop_name);

ext/reflection/tests/ReflectionClassConstant_basic1.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ reflectClassConstant($instance, "BAD_CONST");
5757
Reflecting on class constant TestClass::PUB
5858

5959
__toString():
60-
string(35) "Constant [ public bool PUB ] { 1 }
60+
string(57) "/** My Doc comment */
61+
Constant [ public bool PUB ] { 1 }
6162
"
6263
getName():
6364
string(3) "PUB"
@@ -89,7 +90,8 @@ bool
8990
Reflecting on class constant TestClass::PROT
9091

9192
__toString():
92-
string(38) "Constant [ protected int PROT ] { 4 }
93+
string(65) "/** Another doc comment */
94+
Constant [ protected int PROT ] { 4 }
9395
"
9496
getName():
9597
string(4) "PROT"

ext/reflection/tests/constructor_promotion.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Class [ <user> class Test ] {
4646
- Properties [3] {
4747
Property [ public $z = NULL ]
4848
Property [ public int $x ]
49+
/** @SomeAnnotation() */
4950
Property [ public string $y ]
5051
}
5152

0 commit comments

Comments
 (0)