Skip to content

Commit 0f6db23

Browse files
Add ast_dump() option AST_DUMP_EXCLUDE_DOC_COMMENT (nikic#192)
Closes nikic#191.
1 parent ed09b38 commit 0f6db23

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tests/ast_dump_with_exclude_doc.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
ast_dump() with AST_DUMP_EXCLUDE_DOC_COMMENT
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/../util.php';
7+
8+
$code = <<<'PHP'
9+
<?php
10+
/**
11+
* test
12+
* @param mixed $foo
13+
* @return void
14+
*/
15+
function test($foo) {
16+
}
17+
PHP;
18+
19+
$ast = ast\parse_code($code, $version=80);
20+
echo ast_dump($ast, AST_DUMP_EXCLUDE_DOC_COMMENT);
21+
22+
?>
23+
--EXPECT--
24+
AST_STMT_LIST
25+
0: AST_FUNC_DECL
26+
flags: 0
27+
name: "test"
28+
params: AST_PARAM_LIST
29+
0: AST_PARAM
30+
flags: 0
31+
type: null
32+
name: "foo"
33+
default: null
34+
attributes: null
35+
stmts: AST_STMT_LIST
36+
returnType: null
37+
attributes: null
38+
__declId: 0

util.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use ast\flags;
44

55
const AST_DUMP_LINENOS = 1;
6+
const AST_DUMP_EXCLUDE_DOC_COMMENT = 2;
67

78
function get_flag_info() : array {
89
static $info;
@@ -65,13 +66,16 @@ function ast_dump($ast, int $options = 0) : string {
6566
$result .= "\n flags: " . format_flags($ast->kind, $ast->flags);
6667
}
6768
foreach ($ast->children as $i => $child) {
69+
if (($options & AST_DUMP_EXCLUDE_DOC_COMMENT) && $i === 'docComment') {
70+
continue;
71+
}
6872
$result .= "\n $i: " . str_replace("\n", "\n ", ast_dump($child, $options));
6973
}
7074
return $result;
7175
} else if ($ast === null) {
7276
return 'null';
7377
} else if (is_string($ast)) {
74-
return "\"$ast\"";
78+
return "\"$ast\"";
7579
} else {
7680
return (string) $ast;
7781
}

0 commit comments

Comments
 (0)