Skip to content

Commit da6dd8d

Browse files
author
Raphaël Droz
committed
- Account for the fact that $domain may be an optional argument.
- Get the tests to pass again. - Use twig PSR4 - Fix a couple of renamed variables in TwigFunctionsScanner
1 parent 9d0f5db commit da6dd8d

File tree

6 files changed

+46
-102
lines changed

6 files changed

+46
-102
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"squizlabs/php_codesniffer": "^3.0",
2222
"oscarotero/php-cs-fixer-config": "^1.0",
2323
"friendsofphp/php-cs-fixer": "^2.15",
24-
"timber/timber": "^1.8"
24+
"timber/timber": "dev-master"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/TimberScanner.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function createTwig(): Environment
4848

4949
protected function text(ParsedFunction $function): ?Translation
5050
{
51-
list($original, $domain) = $function->getArguments();
51+
list($original, $domain) = array_pad($function->getArguments(), 2, null);
5252

5353
return $this->addComments(
5454
$function,
@@ -58,7 +58,7 @@ protected function text(ParsedFunction $function): ?Translation
5858

5959
protected function text_context(ParsedFunction $function): ?Translation
6060
{
61-
list($original, $context, $domain) = $function->getArguments();
61+
list($original, $context, $domain) = array_pad($function->getArguments(), 3, null);
6262

6363
return $this->addComments(
6464
$function,
@@ -68,7 +68,7 @@ protected function text_context(ParsedFunction $function): ?Translation
6868

6969
protected function single_plural_number(ParsedFunction $function): ?Translation
7070
{
71-
list($original, $plural, $number, $domain) = $function->getArguments();
71+
list($original, $plural, $number, $domain) = array_pad($function->getArguments(), 4, null);
7272

7373
return $this->addComments(
7474
$function,
@@ -78,7 +78,7 @@ protected function single_plural_number(ParsedFunction $function): ?Translation
7878

7979
protected function single_plural_number_context(ParsedFunction $function): ?Translation
8080
{
81-
list($original, $plural, $number, $context, $domain) = $function->getArguments();
81+
list($original, $plural, $number, $context, $domain) = array_pad($function->getArguments(), 5, null);
8282

8383
return $this->addComments(
8484
$function,
@@ -88,7 +88,7 @@ protected function single_plural_number_context(ParsedFunction $function): ?Tran
8888

8989
protected function single_plural(ParsedFunction $function): ?Translation
9090
{
91-
list($original, $plural, $domain) = $function->getArguments();
91+
list($original, $plural, $domain) = array_pad($function->getArguments(), 3, null);
9292

9393
return $this->addComments(
9494
$function,
@@ -98,7 +98,7 @@ protected function single_plural(ParsedFunction $function): ?Translation
9898

9999
protected function single_plural_context(ParsedFunction $function): ?Translation
100100
{
101-
list($original, $plural, $context, $domain) = $function->getArguments();
101+
list($original, $plural, $context, $domain) = array_pad($function->getArguments(), 4, null);
102102

103103
return $this->addComments(
104104
$function,

src/TwigFunctionsScanner.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use Twig\Environment;
1616
use Twig\Source;
17-
use Twig_Node_Expression_Function;
17+
use Twig\Node\Expression\FunctionExpression;
1818

1919
class TwigFunctionsScanner implements FunctionsScannerInterface
2020
{
@@ -27,19 +27,21 @@ public function __construct(Environment $twig, array $functions)
2727
$this->functions = $functions;
2828
}
2929

30-
private function createFunction(Twig_Node_Expression_Function $node, string $filename): ?ParsedFunction
30+
private function createFunction(FunctionExpression $node, string $filename): ?ParsedFunction
3131
{
3232
$name = $node->getAttribute('name');
3333

34-
if (in_array($name, $this->functions)) {
34+
if (! in_array($name, $this->functions, true)) {
3535
return null;
3636
}
3737

38-
$line = $value->getTemplateLine();
38+
$line = $node->getTemplateLine();
3939
$function = new ParsedFunction($name, $filename, $line);
4040

41-
foreach ($value->getNode('arguments')->getIterator() as $argument) {
42-
$function->addArgument($argument->getAttribute('value'));
41+
foreach ($node->getNode('arguments')->getIterator() as $argument) {
42+
// Some *n*gettext() arguments may not be regular values but expressions.
43+
$arg = $argument->hasAttribute('value') ? $argument->getAttribute('value') : null;
44+
$function->addArgument($arg);
4345
}
4446

4547
return $function;
@@ -51,7 +53,7 @@ private function createFunction(Twig_Node_Expression_Function $node, string $fil
5153
*/
5254
private function extractGettextFunctions($token, string $filename, array &$functions): void
5355
{
54-
if ($token instanceof Twig_Node_Expression_Function) {
56+
if ($token instanceof FunctionExpression) {
5557
$function = $this->createFunction($token, $filename);
5658

5759
if ($function) {
@@ -68,15 +70,13 @@ private function extractGettextFunctions($token, string $filename, array &$funct
6870

6971
public function scan(string $code, string $filename): array
7072
{
71-
$function = [];
73+
$functions = [];
7274

7375
$tokens = $this->twig->parse(
7476
$this->twig->tokenize(new Source($code, $filename))
7577
);
7678

77-
foreach ($tokens as $token) {
78-
$this->extractFunctions($token, $filename, $functions);
79-
}
79+
$this->extractGettextFunctions($tokens, $filename, $functions);
8080

8181
return $functions;
8282
}

src/TwigScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function setTwig(Environment $twig): self
4343

4444
public function getFunctionsScanner(): FunctionsScannerInterface
4545
{
46-
$twig = $this->twig ?: self::createTwig();
46+
$twig = $this->twig ?: static::createTwig();
4747

4848
return new TwigFunctionsScanner($twig, array_keys($this->functions));
4949
}

tests/TimberFunctionsScannerTest.php

Lines changed: 26 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function testScanOnEmptyCode()
4141
$this->assertSame([], $functions);
4242
}
4343

44+
public function cmp($function, $file, $line, $name, $args, $comments = [])
45+
{
46+
$this->assertSame($file, $function->getFilename());
47+
$this->assertSame($line, $function->getLine());
48+
$this->assertSame($line, $function->getLastLine());
49+
$this->assertSame($name, $function->getName());
50+
$this->assertSame($args, $function->getArguments());
51+
if ($comments) {
52+
$this->assertCount(count($comments), $function->getComments());
53+
}
54+
}
55+
4456
public function testTwigFunctionsExtractor()
4557
{
4658
$scanner = new TwigFunctionsScanner(
@@ -50,123 +62,55 @@ public function testTwigFunctionsExtractor()
5062
$file = __DIR__ . '/assets/input.html.twig';
5163
$code = file_get_contents($file);
5264
$functions = $scanner->scan($code, $file);
53-
5465
$this->assertCount(11, $functions);
5566

5667
// text 1
5768
$function = array_shift($functions);
58-
$this->assertSame('__', $function->getName());
59-
$this->assertSame(1, $function->countArguments());
60-
$this->assertSame(['text 1'], $function->getArguments());
61-
$this->assertSame(2, $function->getLine());
62-
$this->assertSame(2, $function->getLastLine());
63-
$this->assertSame($file, $function->getFilename());
64-
$this->assertCount(0, $function->getComments());
69+
$this->cmp($function, $file, 2, '__', ['text 1']);
6570

6671
// text 2
6772
$function = array_shift($functions);
68-
$this->assertSame('__', $function->getName());
69-
$this->assertSame(2, $function->countArguments());
70-
$this->assertSame(['text-domain1', 'text 2 with domain'], $function->getArguments());
71-
$this->assertSame(3, $function->getLine());
72-
$this->assertSame(3, $function->getLastLine());
73-
$this->assertSame($file, $function->getFilename());
74-
$this->assertCount(0, $function->getComments());
73+
$this->cmp($function, $file, 3, '__', ['text 2 with domain', 'text-domain1']);
7574

7675
// text 3
7776
$function = array_shift($functions);
78-
$this->assertSame('__', $function->getName());
79-
$this->assertSame(1, $function->countArguments());
80-
$this->assertSame(7, $function->getLine());
81-
$this->assertSame(7, $function->getLastLine());
82-
$this->assertSame($file, $function->getFilename());
83-
$this->assertCount(0, $function->getComments());
77+
$this->cmp($function, $file, 7, '__', [ 'text 3 (with parenthesis)']);
8478

8579
// text 4
8680
$function = array_shift($functions);
87-
$this->assertSame('_x', $function->getName());
88-
$this->assertSame(2, $function->countArguments());
89-
$this->assertSame(['some context here', 'text 4'], $function->getArguments());
90-
$this->assertSame(8, $function->getLine());
91-
$this->assertSame(8, $function->getLastLine());
92-
$this->assertSame($file, $function->getFilename());
93-
$this->assertCount(0, $function->getComments());
81+
$this->cmp($function, $file, 8, '_x', ['text 4', 'some context here']);
9482

9583
// text 5
9684
$function = array_shift($functions);
97-
$this->assertSame('_x', $function->getName());
98-
$this->assertSame(3, $function->countArguments());
99-
$this->assertSame(
100-
['text-domain2', 'some other context', 'text 5 "with double quotes"'],
101-
$function->getArguments()
102-
);
103-
$this->assertSame(9, $function->getLine());
104-
$this->assertSame(9, $function->getLastLine());
105-
$this->assertSame($file, $function->getFilename());
106-
$this->assertCount(0, $function->getComments());
85+
$this->cmp($function, $file, 9, '_x', ['text 5 "with double quotes"', 'some other context', 'text-domain2']);
10786

10887
// text 6
10988
$function = array_shift($functions);
110-
$this->assertSame('__', $function->getName());
111-
$this->assertSame(1, $function->countArguments());
112-
$this->assertSame(['text 6 \'with escaped single quotes\''], $function->getArguments());
113-
$this->assertSame(10, $function->getLine());
114-
$this->assertSame(10, $function->getLastLine());
115-
$this->assertSame($file, $function->getFilename());
116-
$this->assertCount(0, $function->getComments());
89+
$this->cmp($function, $file, 10, '__', ['text 6 \'with escaped single quotes\'']);
11790

11891
// text 7
11992
$function = array_shift($functions);
120-
$this->assertSame('_n', $function->getName());
121-
$this->assertSame(3, $function->countArguments());
122-
$this->assertSame(['text-domain2', 'text 7 %d foo', 'text 7 %d foos'], $function->getArguments());
123-
$this->assertSame(14, $function->getLine());
124-
$this->assertSame(14, $function->getLastLine());
125-
$this->assertSame($file, $function->getFilename());
126-
$this->assertCount(0, $function->getComments());
93+
$this->cmp($function, $file, 14, '_n', ['text 7 %d foo', 'text 7 %d foos', null, 'text-domain2']);
12794

12895
// text 8
12996
$function = array_shift($functions);
130-
$this->assertSame('_nx', $function->getName());
131-
$this->assertSame(3, $function->countArguments());
132-
$this->assertSame(['another context', 'text 8 %d bar', 'text 8 %d bars'], $function->getArguments());
133-
$this->assertSame(15, $function->getLine());
134-
$this->assertSame(15, $function->getLastLine());
135-
$this->assertSame($file, $function->getFilename());
136-
$this->assertCount(0, $function->getComments());
97+
$this->cmp($function, $file, 15, '_nx', ['text 8 %d bar', 'text 8 %d bars', null, 'another context']);
13798

13899
// text 9
139100
$function = array_shift($functions);
140-
$this->assertSame('__', $function->getName());
141-
$this->assertSame(1, $function->countArguments());
142-
$this->assertSame(['text 9 "with escaped double quotes"'], $function->getArguments());
143-
$this->assertSame(16, $function->getLine());
144-
$this->assertSame(16, $function->getLastLine());
145-
$this->assertSame($file, $function->getFilename());
146-
$this->assertCount(0, $function->getComments());
101+
$this->cmp($function, $file, 16, '__', ['text 9 "with escaped double quotes"']);
147102

148103
// text 10
149104
$function = array_shift($functions);
150-
$this->assertSame('__', $function->getName());
151-
$this->assertSame(1, $function->countArguments());
152-
$this->assertSame(["text 10 'with single quotes'"], $function->getArguments());
153-
$this->assertSame(17, $function->getLine());
154-
$this->assertSame(17, $function->getLastLine());
155-
$this->assertSame($file, $function->getFilename());
156-
$this->assertCount(0, $function->getComments());
105+
$this->cmp($function, $file, 17, '__', ["text 10 'with single quotes'"]);
157106

158107
// text 11
159108
$function = array_shift($functions);
160-
$this->assertSame('_n', $function->getName());
161-
$this->assertSame(2, $function->countArguments());
162-
$this->assertSame(['text 11 with plural', 'The plural form'], $function->getArguments());
163-
$this->assertSame(20, $function->getLine());
164-
$this->assertSame(20, $function->getLastLine());
165-
$this->assertSame($file, $function->getFilename());
166-
$this->assertCount(0, $function->getComments());
109+
$this->cmp($function, $file, 20, '_n', ['text 11 with plural', 'The plural form', 5]);
110+
167111
/* ToDo
168-
$comments = $function->getComments();
169-
$this->assertSame("notes: This is an actual note for translators.", array_shift($comments));
112+
$comments = $function->getComments();
113+
$this->assertSame("notes: This is an actual note for translators.", array_shift($comments));
170114
*/
171115
}
172116
}

tests/assets/input.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<header>
2-
<h1>{{ __('text 1') }}</h1>
2+
<h1>{{ __('text 1') }} {{ max(1,2) }}</h1>
33
<h1>{{ __('text 2 with domain', 'text-domain1') }}</h1>
44
</header>
55

0 commit comments

Comments
 (0)