Skip to content

Commit 247d4cd

Browse files
authored
Merge pull request #20 from swissspidy/fix/comment-association
Find comments of wrapping nodes and on the previous line
2 parents 41aef9e + 4759f7e commit 247d4cd

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎src/PhpNodeVisitor.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

‎tests/PhpFunctionsScannerTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,52 @@ public function testPhpFunctionsExtractor()
188188
$this->assertSame($file, $function->getFilename());
189189
$this->assertCount(0, $function->getComments());
190190
}
191+
192+
public function testCommentsOfWrappingNodes()
193+
{
194+
$code = <<<'PHP'
195+
<?php
196+
printf(
197+
/* translators: %s: Name */
198+
__('Hello %s'),
199+
$name
200+
);
201+
202+
$labels = [
203+
// translators: Label
204+
'label' => __('Label'),
205+
'other' => __('Other'),
206+
];
207+
208+
echo '<p>' .
209+
/* translators: Concatenated */
210+
__('Concatenated') . '</p>';
211+
?>
212+
<?php /* translators: Previous block */ ?>
213+
<p><?php _e('Template'); ?></p>
214+
215+
<?php /* translators: Too far away */ ?>
216+
217+
<p><?php _e('No comment'); ?></p>
218+
PHP;
219+
220+
$scanner = new PhpFunctionsScanner(['__', '_e']);
221+
$comments = [];
222+
223+
foreach ($scanner->scan($code, 'file.php') as $function) {
224+
$comments[$function->getArguments()[0]] = $function->getComments();
225+
}
226+
227+
$this->assertSame(
228+
[
229+
'Hello %s' => ['translators: %s: Name'],
230+
'Label' => ['translators: Label'],
231+
'Other' => [],
232+
'Concatenated' => ['translators: Concatenated'],
233+
'Template' => ['translators: Previous block'],
234+
'No comment' => [],
235+
],
236+
$comments
237+
);
238+
}
191239
}

0 commit comments

Comments
 (0)