Skip to content

Commit 3290fbe

Browse files
authored
Merge pull request #531 from franmomu/not_use_node_index
Use array access to twig argument nodes
2 parents bfafd6c + 687f51f commit 3290fbe

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

Translation/Extractor/File/TwigFileExtractor.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ protected function doEnterNode(Node $node, Environment $env)
9999
}
100100
$id = $idNode->getAttribute('value');
101101

102-
$index = 'trans' === $name ? 1 : 2;
103-
$domain = 'messages';
104-
$arguments = $node->getNode('arguments');
105-
if ($arguments->hasNode($index)) {
106-
$argument = $arguments->getNode($index);
107-
if (!$argument instanceof ConstantExpression) {
102+
$index = $name === 'trans' ? 1 : 2;
103+
$domain = 'messages';
104+
$arguments = iterator_to_array($node->getNode('arguments'));
105+
if (isset($arguments[$index])) {
106+
$argument = $arguments[$index];
107+
if (! $argument instanceof ConstantExpression) {
108108
return $node;
109109
// FIXME: Throw exception if there is some way for the user to turn this off
110110
// on a case-by-case basis, similar to @Ignore in PHP
@@ -122,14 +122,14 @@ protected function doEnterNode(Node $node, Environment $env)
122122
}
123123

124124
$name = $this->stack[$i]->getNode('filter')->getAttribute('value');
125-
if ('desc' === $name || 'meaning' === $name) {
126-
$arguments = $this->stack[$i]->getNode('arguments');
127-
if (!$arguments->hasNode(0)) {
125+
if ($name === 'desc' || $name === 'meaning') {
126+
$arguments = iterator_to_array($this->stack[$i]->getNode('arguments'));
127+
if (! isset($arguments[0])) {
128128
throw new RuntimeException(sprintf('The "%s" filter requires exactly one argument, the description text.', $name));
129129
}
130130

131-
$text = $arguments->getNode(0);
132-
if (!$text instanceof ConstantExpression) {
131+
$text = $arguments[0];
132+
if (! $text instanceof ConstantExpression) {
133133
throw new RuntimeException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name));
134134
}
135135

Twig/DefaultApplyingNodeVisitor.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ public function doEnterNode(Node $node, Environment $env)
7575
}
7676

7777
$wrappingNode = $node->getNode('node');
78-
$testNode = clone $wrappingNode;
79-
$defaultNode = $node->getNode('arguments')->getNode(0);
78+
79+
$testNode = clone $wrappingNode;
80+
$arguments = iterator_to_array($node->getNode('arguments'));
81+
$defaultNode = $arguments[0];
8082

8183
// if the |transchoice filter is used, delegate the call to the TranslationExtension
8284
// so that we can catch a possible exception when the default translation has not yet
@@ -95,22 +97,23 @@ public function doEnterNode(Node $node, Environment $env)
9597
return $node;
9698
}
9799

100+
$wrappingNodeArguments = iterator_to_array($wrappingNode->getNode('arguments'));
101+
98102
// if the |trans filter has replacements parameters
99103
// (e.g. |trans({'%foo%': 'bar'}))
100-
if ($wrappingNode->getNode('arguments')->hasNode(0)) {
104+
if (isset($wrappingNodeArguments[0])) {
101105
$lineno = $wrappingNode->getTemplateLine();
102106

103107
// remove the replacements from the test node
104-
$testNode->setNode('arguments', clone $testNode->getNode('arguments'));
105-
$testNode->getNode('arguments')->setNode(0, new ArrayExpression(array(), $lineno));
108+
$testNodeArguments = iterator_to_array($testNode->getNode('arguments'));
109+
$testNodeArguments[0] = new ArrayExpression([], $lineno);
110+
$testNode->setNode('arguments', new Node($testNodeArguments));
106111

107112
// wrap the default node in a |replace filter
108113
$defaultNode = new FilterExpression(
109-
clone $node->getNode('arguments')->getNode(0),
114+
$arguments[0],
110115
new ConstantExpression('replace', $lineno),
111-
new Node(array(
112-
clone $wrappingNode->getNode('arguments')->getNode(0)
113-
)),
116+
new Node([$wrappingNodeArguments[0]]),
114117
$lineno
115118
);
116119
}

0 commit comments

Comments
 (0)