Skip to content

Commit 881c93b

Browse files
author
Oscar Otero
committed
extract comments of functions precending gettext functions #6
1 parent f95d069 commit 881c93b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/PhpNodeVisitor.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/PhpScannerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public function testPhpCodeScanner()
4242
$this->assertNotNull($translation);
4343
$this->assertSame([$file => [66]], $translation->getReferences()->toArray());
4444
$this->assertCount(1, $translation->getExtractedComments());
45+
46+
$translation = $domain1->find(null, 'i18n tagged %s');
47+
$this->assertNotNull($translation);
48+
$this->assertSame([$file => [75]], $translation->getReferences()->toArray());
49+
$this->assertCount(1, $translation->getExtractedComments());
50+
$this->assertSame(['i18n Tagged comment on the line before'], $translation->getExtractedComments()->toArray());
4551
}
4652

4753
public function testInvalidFunction()

0 commit comments

Comments
 (0)