@@ -15,7 +15,7 @@ class PhpNodeVisitor implements NodeVisitor
1515 protected $ validFunctions ;
1616 protected $ filename ;
1717 protected $ functions = [];
18- protected $ bufferComments = [] ;
18+ protected $ bufferComments ;
1919
2020 public function __construct (string $ filename , array $ validFunctions = null )
2121 {
@@ -35,21 +35,20 @@ public function enterNode(Node $node)
3535
3636 if ($ name && ($ this ->validFunctions === null || in_array ($ name , $ this ->validFunctions ))) {
3737 $ this ->functions [] = $ this ->createFunction ($ node );
38+ } elseif ($ node ->getComments ()) {
39+ $ this ->bufferComments = $ node ;
3840 }
39-
40- $ this ->bufferComments = [];
4141 return null ;
4242 }
4343
4444 switch ($ node ->getType ()) {
4545 case 'Stmt_Echo ' :
4646 case 'Stmt_Return ' :
4747 case 'Expr_Print ' :
48- $ this ->bufferComments = $ node-> getComments () ;
48+ $ this ->bufferComments = $ node ;
4949 return null ;
5050 }
5151
52- $ this ->bufferComments = [];
5352 return null ;
5453 }
5554
@@ -81,10 +80,14 @@ protected function createFunction(FuncCall $node): ParsedFunction
8180 $ function ->addComment (static ::getComment ($ comment ));
8281 }
8382
84- foreach ($ this ->bufferComments as $ comment ) {
85- $ function ->addComment (static ::getComment ($ comment ));
83+ if ($ this ->bufferComments && $ this ->bufferComments ->getStartLine () === $ node ->getStartLine ()) {
84+ foreach ($ this ->bufferComments ->getComments () as $ comment ) {
85+ $ function ->addComment (static ::getComment ($ comment ));
86+ }
8687 }
8788
89+ $ this ->bufferComments = null ;
90+
8891 foreach ($ node ->args as $ argument ) {
8992 $ value = $ argument ->value ;
9093
0 commit comments