7
7
use PhpParser \Node ;
8
8
use PhpParser \Node \Expr ;
9
9
use PhpParser \Node \Expr \FuncCall ;
10
+ use PhpParser \Node \Expr \MethodCall ;
10
11
use PhpParser \Node \Identifier ;
11
12
use PhpParser \Node \Name ;
12
13
use PhpParser \NodeVisitor ;
13
14
14
15
class PhpNodeVisitor implements NodeVisitor
15
16
{
16
- protected $ validFunctions ;
17
- protected $ filename ;
18
- protected $ functions = [];
19
- protected $ bufferComments ;
17
+ protected ?array $ validFunctions ;
18
+ protected string $ filename ;
19
+ protected array $ functions = [];
20
+
21
+ /** @var Comment[] */
22
+ protected array $ bufferComments = [];
20
23
21
24
public function __construct (string $ filename , array $ validFunctions = null )
22
25
{
@@ -40,15 +43,17 @@ public function enterNode(Node $node)
40
43
if ($ name && ($ this ->validFunctions === null || in_array ($ name , $ this ->validFunctions ))) {
41
44
$ this ->functions [] = $ this ->createFunction ($ node );
42
45
} elseif ($ node ->getComments ()) {
43
- $ this ->bufferComments = $ node ;
46
+ $ this ->bufferComments [] = $ node ;
44
47
}
45
- return null ;
48
+ break ;
49
+
50
+ case 'Stmt_Expression ' :
46
51
case 'Stmt_Echo ' :
47
52
case 'Stmt_Return ' :
48
53
case 'Expr_Print ' :
49
54
case 'Expr_Assign ' :
50
- $ this ->bufferComments = $ node ;
51
- return null ;
55
+ $ this ->bufferComments [] = $ node ;
56
+ break ;
52
57
}
53
58
54
59
return null ;
@@ -85,13 +90,17 @@ protected function createFunction(Expr $node): ParsedFunction
85
90
$ function ->addComment (static ::getComment ($ comment ));
86
91
}
87
92
88
- if ($ this ->bufferComments && $ this ->bufferComments ->getStartLine () === $ node ->getStartLine ()) {
89
- foreach ($ this ->bufferComments ->getComments () as $ comment ) {
90
- $ function ->addComment (static ::getComment ($ comment ));
93
+ if ($ this ->bufferComments ) {
94
+ foreach ($ this ->bufferComments as $ bufferComment ) {
95
+ if ($ bufferComment ->getStartLine () === $ node ->getStartLine ()) {
96
+ foreach ($ bufferComment ->getComments () as $ comment ) {
97
+ $ function ->addComment (static ::getComment ($ comment ));
98
+ }
99
+ }
91
100
}
92
101
}
93
102
94
- $ this ->bufferComments = null ;
103
+ $ this ->bufferComments = [] ;
95
104
96
105
foreach ($ node ->args as $ argument ) {
97
106
$ value = $ argument ->value ;
@@ -140,8 +149,8 @@ protected static function getValue(Expr $value)
140
149
141
150
switch ($ type ) {
142
151
case 'Scalar_String ' :
143
- case 'Scalar_LNumber ' :
144
- case 'Scalar_DNumber ' :
152
+ case 'Scalar_Int ' :
153
+ case 'Scalar_Float ' :
145
154
return $ value ->value ;
146
155
case 'Expr_BinaryOp_Concat ' :
147
156
$ values = [];
0 commit comments