Skip to content

Commit dc23833

Browse files
authored
Merge pull request #271 from swissspidy/add-comments-cache
v4: Prevent adding the same translator comment to multiple functions
2 parents 58bc0f7 + 625e8b3 commit dc23833

16 files changed

Lines changed: 176 additions & 8 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"gettext/languages": "^2.3"
2323
},
2424
"require-dev": {
25-
"illuminate/view": "*",
25+
"illuminate/view": "^5.0.x-dev",
2626
"twig/twig": "^1.31|^2.0",
2727
"twig/extensions": "*",
2828
"symfony/yaml": "~2",

src/Utils/FunctionsScanner.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function saveGettextFunctions($translations, array $options)
3838
$functions = $options['functions'];
3939
$file = $options['file'];
4040

41+
/**
42+
* List of source code comments already associated with a function.
43+
*
44+
* Prevents associating the same comment to multiple functions.
45+
*
46+
* @var ParsedComment[] $commentsCache
47+
*/
48+
$commentsCache = [];
49+
4150
foreach ($this->getFunctions($options['constants']) as $function) {
4251
list($name, $line, $args) = $function;
4352

@@ -78,8 +87,13 @@ public function saveGettextFunctions($translations, array $options)
7887
$translation->addReference($file, $line);
7988

8089
if (isset($function[3])) {
90+
/* @var ParsedComment $extractedComment */
8191
foreach ($function[3] as $extractedComment) {
82-
$translation->addExtractedComment($extractedComment);
92+
if (in_array($extractedComment, $commentsCache, true)) {
93+
continue;
94+
}
95+
$translation->addExtractedComment($extractedComment->getComment());
96+
$commentsCache[] = $extractedComment;
8397
}
8498
}
8599
}

src/Utils/ParsedFunction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ParsedFunction
4545
/**
4646
* Extracted comments.
4747
*
48-
* @var string[]|null
48+
* @var ParsedComment[]|null
4949
*/
5050
protected $comments;
5151

@@ -112,7 +112,7 @@ public function addArgumentChunk($chunk)
112112
/**
113113
* Add a comment associated to this function.
114114
*
115-
* @param string $comment
115+
* @param ParsedComment $comment
116116
*/
117117
public function addComment($comment)
118118
{

src/Utils/PhpFunctionsScanner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function getFunctions(array $constants = [])
129129
$comment = $bufferComments[0];
130130

131131
if ($comment->isRelatedWith($newFunction)) {
132-
$newFunction->addComment($comment->getComment());
132+
$newFunction->addComment($comment);
133133
}
134134
}
135135

@@ -148,7 +148,7 @@ public function getFunctions(array $constants = [])
148148

149149
// The comment is inside the function call.
150150
if (isset($bufferFunctions[0])) {
151-
$bufferFunctions[0]->addComment($comment->getComment());
151+
$bufferFunctions[0]->addComment($comment);
152152
}
153153
}
154154
break;

tests/AssetsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function testPhpCode2()
308308
'CONTEXT' => 'my-context',
309309
],
310310
]);
311-
$countTranslations = 14;
311+
$countTranslations = 19;
312312
$countTranslated = 0;
313313
$countHeaders = 8;
314314

tests/assets/phpcode2/Csv.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ my-context,"All comments",
2121
,foo,
2222
,bar,
2323
,"foo bar",
24+
,World,
25+
,"No comment",
26+
,"Within printf: %s",
27+
,placeholder,
28+
,"After printf",

tests/assets/phpcode2/CsvDictionary.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ plain,
1212
foo,
1313
bar,
1414
"foo bar",
15+
World,
16+
"No comment",
17+
"Within printf: %s",
18+
placeholder,
19+
"After printf",

tests/assets/phpcode2/Jed.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@
4646
],
4747
"foo bar": [
4848
""
49+
],
50+
"World": [
51+
""
52+
],
53+
"No comment": [
54+
""
55+
],
56+
"Within printf: %s": [
57+
""
58+
],
59+
"placeholder": [
60+
""
61+
],
62+
"After printf": [
63+
""
4964
]
5065
}
5166
}

tests/assets/phpcode2/Json.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@
4444
],
4545
"foo bar": [
4646
""
47+
],
48+
"World": [
49+
""
50+
],
51+
"No comment": [
52+
""
53+
],
54+
"Within printf: %s": [
55+
""
56+
],
57+
"placeholder": [
58+
""
59+
],
60+
"After printf": [
61+
""
4762
]
4863
},
4964
"my-context": {

tests/assets/phpcode2/JsonDictionary.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"i18n tagged %s": "",
1313
"foo": "",
1414
"bar": "",
15-
"foo bar": ""
15+
"foo bar": "",
16+
"World": "",
17+
"No comment": "",
18+
"Within printf: %s": "",
19+
"placeholder": "",
20+
"After printf": ""
1621
}

0 commit comments

Comments
 (0)