@@ -59,6 +59,13 @@ public function enterNode(Node $node)
5959 case 'Expr_Assign ' :
6060 $ this ->bufferComments [] = $ node ;
6161 break ;
62+ default :
63+ // Comments in front of a function call belong to the outermost node starting there,
64+ // e.g. the argument or array item the function call is the value of.
65+ if ($ node ->getComments ()) {
66+ $ this ->bufferComments [] = $ node ;
67+ }
68+ break ;
6269 }
6370
6471 return null ;
@@ -93,8 +100,12 @@ protected function createFunction(Expr $node): ParsedFunction
93100
94101 // nikic/php-parser 4 assigns a comment to every node starting after it, not only to the outermost one.
95102 $ seen = [];
96- $ addComments = static function (Node $ commentedNode ) use ($ function , &$ seen ): void {
103+ $ addComments = static function (Node $ commentedNode, ? int $ endLine = null ) use ($ function , &$ seen ): void {
97104 foreach ($ commentedNode ->getComments () as $ comment ) {
105+ if ($ endLine !== null && $ comment ->getEndLine () !== $ endLine ) {
106+ continue ;
107+ }
108+
98109 $ position = $ comment ->getStartFilePos ();
99110
100111 if (!isset ($ seen [$ position ])) {
@@ -109,6 +120,9 @@ protected function createFunction(Expr $node): ParsedFunction
109120 foreach ($ this ->bufferComments as $ bufferComment ) {
110121 if ($ bufferComment ->getStartLine () === $ node ->getStartLine ()) {
111122 $ addComments ($ bufferComment );
123+ } else {
124+ // Comments on the line before, e.g. in its own PHP block in a template.
125+ $ addComments ($ bufferComment , $ node ->getStartLine () - 1 );
112126 }
113127 }
114128
0 commit comments