Skip to content

Commit 0c86fd2

Browse files
authored
Merge pull request #127 from Stadly/label-concat
Concatenated labels
2 parents 0f1a1a6 + c254243 commit 0c86fd2

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/Visitor/Php/BasePHPVisitor.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,34 @@ protected function getStringArgument(Node\Expr\MethodCall $node, int $index): ?s
2727
return null;
2828
}
2929

30-
if (!$node->args[$index]->value instanceof Node\Scalar\String_) {
31-
return null;
32-
}
33-
34-
$label = $node->args[$index]->value->value;
30+
$label = $this->getStringValue($node->args[$index]->value);
3531
if (empty($label)) {
3632
return null;
3733
}
3834

3935
return $label;
4036
}
37+
38+
private function getStringValue(Node $node): ?string
39+
{
40+
if ($node instanceof Node\Scalar\String_) {
41+
return $node->value;
42+
}
43+
44+
if ($node instanceof Node\Expr\BinaryOp\Concat) {
45+
$left = $this->getStringValue($node->left);
46+
if (null === $left) {
47+
return null;
48+
}
49+
50+
$right = $this->getStringValue($node->right);
51+
if (null === $right) {
52+
return null;
53+
}
54+
55+
return $left.$right;
56+
}
57+
58+
return null;
59+
}
4160
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Github;
4+
5+
use Translation\Extractor\Annotation\Ignore;
6+
7+
class ConcatenatedStrings
8+
{
9+
public function buildForm(FormBuilderInterface $builder, array $options)
10+
{
11+
$translator->trans(
12+
'github.'.
13+
'issue_125.'.
14+
'a'
15+
);
16+
}
17+
}

tests/Smoke/AllExtractorsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function testNoException()
8888
$this->translationMissing($sc, 'github.issue_111.c');
8989
$this->translationExists($sc, 'github.issue_111.d');
9090

91+
$this->translationExists($sc, 'github.issue_125.a');
92+
9193
/*
9294
* It is okey to increase the error count if you adding more fixtures/code.
9395
* We just need to be aware that it changes.

0 commit comments

Comments
 (0)