77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \FuncCall ;
10+ use PhpParser \Node \Expr \MethodCall ;
1011use PhpParser \Node \Identifier ;
1112use PhpParser \Node \Name ;
1213use PhpParser \NodeVisitor ;
1314
1415class PhpNodeVisitor implements NodeVisitor
1516{
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 = [];
2023
2124 public function __construct (string $ filename , array $ validFunctions = null )
2225 {
@@ -40,15 +43,17 @@ public function enterNode(Node $node)
4043 if ($ name && ($ this ->validFunctions === null || in_array ($ name , $ this ->validFunctions ))) {
4144 $ this ->functions [] = $ this ->createFunction ($ node );
4245 } elseif ($ node ->getComments ()) {
43- $ this ->bufferComments = $ node ;
46+ $ this ->bufferComments [] = $ node ;
4447 }
45- return null ;
48+ break ;
49+
50+ case 'Stmt_Expression ' :
4651 case 'Stmt_Echo ' :
4752 case 'Stmt_Return ' :
4853 case 'Expr_Print ' :
4954 case 'Expr_Assign ' :
50- $ this ->bufferComments = $ node ;
51- return null ;
55+ $ this ->bufferComments [] = $ node ;
56+ break ;
5257 }
5358
5459 return null ;
@@ -85,13 +90,17 @@ protected function createFunction(Expr $node): ParsedFunction
8590 $ function ->addComment (static ::getComment ($ comment ));
8691 }
8792
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+ }
91100 }
92101 }
93102
94- $ this ->bufferComments = null ;
103+ $ this ->bufferComments = [] ;
95104
96105 foreach ($ node ->args as $ argument ) {
97106 $ value = $ argument ->value ;
@@ -140,8 +149,8 @@ protected static function getValue(Expr $value)
140149
141150 switch ($ type ) {
142151 case 'Scalar_String ' :
143- case 'Scalar_LNumber ' :
144- case 'Scalar_DNumber ' :
152+ case 'Scalar_Int ' :
153+ case 'Scalar_Float ' :
145154 return $ value ->value ;
146155 case 'Expr_BinaryOp_Concat ' :
147156 $ values = [];
0 commit comments