Skip to content

Commit 46f051c

Browse files
committed
extract comments of functions prepended with echo, print or return #6
1 parent 40fc463 commit 46f051c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/PhpNodeVisitor.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class PhpNodeVisitor implements NodeVisitor
1515
protected $validFunctions;
1616
protected $filename;
1717
protected $functions = [];
18+
protected $bufferComments = [];
1819

1920
public function __construct(string $filename, array $validFunctions = null)
2021
{
@@ -36,9 +37,19 @@ public function enterNode(Node $node)
3637
$this->functions[] = $this->createFunction($node);
3738
}
3839

40+
$this->bufferComments = [];
3941
return null;
4042
}
4143

44+
switch ($node->getType()) {
45+
case 'Stmt_Echo':
46+
case 'Stmt_Return':
47+
case 'Expr_Print':
48+
$this->bufferComments = $node->getComments();
49+
return null;
50+
}
51+
52+
$this->bufferComments = [];
4253
return null;
4354
}
4455

@@ -70,6 +81,10 @@ protected function createFunction(FuncCall $node): ParsedFunction
7081
$function->addComment(static::getComment($comment));
7182
}
7283

84+
foreach ($this->bufferComments as $comment) {
85+
$function->addComment(static::getComment($comment));
86+
}
87+
7388
foreach ($node->args as $argument) {
7489
$value = $argument->value;
7590

tests/assets/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
//This comment is related with the first function
33

4-
fn1('arg1', 'arg2', 3);
4+
print(fn1('arg1', 'arg2', 3));
55
fn2($var);
66
fn3(fn4('arg4'), 'arg5', fn5(6, 7.5));
77
fn6(['arr']);
88
fn7(CONSTANT_1);
99
// fn_8();
1010
/* ALLOW: This is a comment to fn9 */
11-
fn9(ARG_8);
11+
return fn9(ARG_8);
1212

1313
/* Comment to fn10 */ fn10();
1414

@@ -19,7 +19,7 @@
1919
Related comment
2020
number one
2121
*/
22-
fn12(
22+
echo fn12(
2323
/* Related comment 2 */
2424
'arg11',
2525
/* ALLOW: Related comment 3 */

0 commit comments

Comments
 (0)