Skip to content

Commit d904da8

Browse files
claudeswissspidy
andcommitted
Find comments of wrapping nodes and on the previous line
A comment in front of a function call belongs to the outermost node that starts there. Only a few statement types were looked at, so the comment was lost when the call was an argument of another call, the value of an array item or part of a concatenation: printf( /* translators: %s: Name */ __('Hello %s'), $name ); Comments on the line before the call are now found as well, which is common in templates: <?php /* translators: Label */ ?> <p><?php _e('Label'); ?></p> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Claude-Session: https://claude.ai/code/session_01UWTd5qmztfFv37ybV4xsjV
1 parent 43f53b6 commit d904da8

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎src/PhpNodeVisitor.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function enterNode(Node $node)
5454
case 'Expr_Assign':
5555
$this->bufferComments[] = $node;
5656
break;
57+
default:
58+
// Comments in front of a function call belong to the outermost node starting there,
59+
// e.g. the argument or array item the function call is the value of.
60+
if ($node->getComments()) {
61+
$this->bufferComments[] = $node;
62+
}
63+
break;
5764
}
5865

5966
return null;
@@ -92,8 +99,11 @@ protected function createFunction(Expr $node): ParsedFunction
9299

93100
if ($this->bufferComments) {
94101
foreach ($this->bufferComments as $bufferComment) {
95-
if ($bufferComment->getStartLine() === $node->getStartLine()) {
96-
foreach ($bufferComment->getComments() as $comment) {
102+
$sameLine = $bufferComment->getStartLine() === $node->getStartLine();
103+
104+
foreach ($bufferComment->getComments() as $comment) {
105+
// Comments on the line before, e.g. in its own PHP block in a template.
106+
if ($sameLine || $comment->getEndLine() === $node->getStartLine() - 1) {
97107
$function->addComment(static::getComment($comment));
98108
}
99109
}

‎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)