8000 deal with indented heredoc/nowdoc tokens · symfony/symfony@d4b3bb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4b3bb2

Browse files
committed
deal with indented heredoc/nowdoc tokens
1 parent b3c4d63 commit d4b3bb2

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Symfony/Component/Translation/Extractor/PhpExtractor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ private function getValue(\Iterator $tokenIterator)
178178
}
179179
break;
180180
case \T_END_HEREDOC:
181+
if ($indentation = strspn($t[1], ' ')) {
182+
$docPartWithLineBreaks = $docPart;
183+
$docPart = '';
184+
185+
foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, PREG_SPLIT_DELIM_CAPTURE) as $str) {
186+
if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
187+
$docPart .= $str;
188+
} else {
189+
$docPart .= substr($str, $indentation);
190+
}
191+
}
192+
}
193+
181194
$message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
182195
$docToken = '';
183196
$docPart = '';

src/Symfony/Component/Translation/Tests/Extractor/PhpExtractorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ public function testExtraction($resource)
7575
$this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
7676
}
7777

78+
/**
79+
* @requires PHP 7.3
80+
*/
81+
public function testExtractionFromIndentedHeredocNowdoc()
82+
{
83+
$catalogue = new MessageCatalogue('en');
84+
85+
$extractor = new PhpExtractor();
86+
$extractor->setPrefix('prefix');
87+
$extractor->extract(__DIR__.'/../fixtures/extractor-7.3/translation.html.php', $catalogue 8000 );
88+
89+
$expectedCatalogue = [
90+
'messages' => [
91+
"heredoc\nindented\n further" => "prefixheredoc\nindented\n further",
92+
"nowdoc\nindented\n further" => "prefixnowdoc\nindented\n further",
93+
],
94+
];
95+
96+
$this->assertEquals($expectedCatalogue, $catalogue->all());
97+
}
98+
7899
public function resourcesProvider()
79100
{
80101
$directory = __DIR__.'/../fixtures/extractor/';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This template is used for translation message extraction tests
2+
<?php echo $view['translator']->trans(<<<EOF
3+
heredoc
4+
indented
5+
further
6+
EOF
7+
); ?>
8+
<?php echo $view['translator']->trans(<<<'EOF'
9+
nowdoc
10+
indented
11+
further
12+
EOF
13+
); ?>

0 commit comments

Comments
 (0)
0