Skip to content

Commit 6d13363

Browse files
author
Christian Schneider
committed
Add DocComments for function parameters
1 parent 7158e63 commit 6d13363

10 files changed

Lines changed: 111 additions & 10 deletions

Zend/zend_API.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,6 +2997,7 @@ ZEND_API void zend_convert_internal_arg_info(zend_arg_info *new_arg_info, const
29972997
new_arg_info->name = NULL;
29982998
new_arg_info->default_value = NULL;
29992999
}
3000+
new_arg_info->doc_comment = NULL;
30003001
new_arg_info->type = arg_info->type;
30013002
zend_convert_internal_arg_info_type(&new_arg_info->type, persistent);
30023003
}

Zend/zend_compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8024,6 +8024,7 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
80248024
} else {
80258025
arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
80268026
}
8027+
arg_infos->doc_comment = NULL;
80278028
arg_infos++;
80288029
op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
80298030

@@ -8122,6 +8123,7 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
81228123
arg_info->name = zend_string_copy(name);
81238124
arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
81248125
arg_info->default_value = NULL;
8126+
arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
81258127

81268128
if (attributes_ast) {
81278129
zend_compile_attributes(

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ typedef struct _zend_arg_info {
515515
zend_string *name;
516516
zend_type type;
517517
zend_string *default_value;
518+
zend_string *doc_comment;
518519
} zend_arg_info;
519520

520521
/* the following structure repeats the layout of zend_internal_arg_info,

Zend/zend_language_parser.y

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,13 +817,13 @@ optional_cpp_modifiers:
817817

818818
parameter:
819819
optional_cpp_modifiers optional_type_without_static
820-
is_reference is_variadic T_VARIABLE backup_doc_comment optional_property_hook_list
820+
is_reference is_variadic T_VARIABLE optional_property_hook_list backup_doc_comment
821821
{ $$ = zend_ast_create_ex(ZEND_AST_PARAM, $1 | $3 | $4, $2, $5, NULL,
822-
NULL, $6 ? zend_ast_create_zval_from_str($6) : NULL, $7); }
822+
NULL, $7 ? zend_ast_create_zval_from_str($7) : NULL, $6); }
823823
| optional_cpp_modifiers optional_type_without_static
824-
is_reference is_variadic T_VARIABLE backup_doc_comment '=' expr optional_property_hook_list
825-
{ $$ = zend_ast_create_ex(ZEND_AST_PARAM, $1 | $3 | $4, $2, $5, $8,
826-
NULL, $6 ? zend_ast_create_zval_from_str($6) : NULL, $9); }
824+
is_reference is_variadic T_VARIABLE '=' expr optional_property_hook_list backup_doc_comment
825+
{ $$ = zend_ast_create_ex(ZEND_AST_PARAM, $1 | $3 | $4, $2, $5, $7,
826+
NULL, $9 ? zend_ast_create_zval_from_str($9) : NULL, $8); }
827827
;
828828

829829
optional_type_without_static:

Zend/zend_opcode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
648648
if (arg_info[i].name) {
649649
zend_string_release_ex(arg_info[i].name, 0);
650650
}
651+
if (arg_info[i].doc_comment) {
652+
zend_string_release_ex(arg_info[i].doc_comment, 0);
653+
}
651654
zend_type_release(arg_info[i].type, /* persistent */ false);
652655
}
653656
efree(arg_info);

ext/reflection/php_reflection.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,6 +2653,21 @@ ZEND_METHOD(ReflectionParameter, __toString)
26532653

26542654
/* }}} */
26552655

2656+
/* {{{ Returns the doc comment for this function */
2657+
ZEND_METHOD(ReflectionParameter, getDocComment)
2658+
{
2659+
reflection_object *intern;
2660+
parameter_reference *param;
2661+
2662+
ZEND_PARSE_PARAMETERS_NONE();
2663+
2664+
GET_REFLECTION_OBJECT_PTR(param);
2665+
if (param->arg_info->doc_comment) {
2666+
RETURN_STR_COPY(param->arg_info->doc_comment);
2667+
}
2668+
RETURN_FALSE;
2669+
}
2670+
26562671
/* {{{ Returns this parameter's name */
26572672
ZEND_METHOD(ReflectionParameter, getName)
26582673
{

ext/reflection/php_reflection.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ public function __construct($function, int|string $param) {}
648648

649649
public function __toString(): string {}
650650

651+
/** @tentative-return-type */
652+
public function getDocComment(): string|false {}
653+
651654
/** @tentative-return-type */
652655
public function getName(): string {}
653656

ext/reflection/php_reflection_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/reflection/php_reflection_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--TEST--
2+
Test ReflectionParameter::getDocComment() usage.
3+
--INI--
4+
opcache.save_comments=1
5+
--FILE--
6+
<?php
7+
8+
function func(
9+
/**
10+
* My Doc Comment for $a
11+
*
12+
*/
13+
$a, $b, $c,
14+
/**
15+
* My Doc Comment for $d
16+
*/
17+
$d,
18+
/**Not a doc comment */
19+
$e,
20+
/**
21+
* Doc comment for $f
22+
*/
23+
$f,
24+
$g /** Doc comment for $g behind parameter */,
25+
) {}
26+
27+
foreach(['func'] as $function) {
28+
$rc = new ReflectionFunction($function);
29+
$rps = $rc->getParameters();
30+
foreach($rps as $rp) {
31+
echo "\n\n---> Doc comment for $function $" . $rp->getName() . ":\n";
32+
var_dump($rp->getDocComment());
33+
}
34+
}
35+
36+
?>
37+
--EXPECTF--
38+
---> Doc comment for func $a:
39+
string(%d) "/**
40+
* My Doc Comment for $a
41+
*
42+
*/"
43+
44+
45+
---> Doc comment for func $b:
46+
bool(false)
47+
48+
49+
---> Doc comment for func $c:
50+
bool(false)
51+
52+
53+
---> Doc comment for func $d:
54+
string(%d) "/**
55+
* My Doc Comment for $d
56+
*/"
57+
58+
59+
---> Doc comment for func $e:
60+
bool(false)
61+
62+
63+
---> Doc comment for func $f:
64+
string(%d) "/**
65+
* Doc comment for $f
66+
*/"
67+
68+
69+
---> Doc comment for func $g:
70+
string(%d) "/** Doc comment for $g behind parameter */"
71+
72+

0 commit comments

Comments
 (0)