Skip to content

Commit 3b71efb

Browse files
committed
bug #2701 Ensure that syntax errors are triggered with the right line (stof)
This PR was merged into the 1.x branch. Discussion ---------- Ensure that syntax errors are triggered with the right line When throwing the syntax error without any line and source in these places, the guessing logic enters into action. For the main template, it won't find anything. But for included templates (or any other template loaded during the rendering of another one, even as main one), the guessing will find a template (the caller one) and set the source and line based on it. The source will then be replaced by the proper template by `\Twig_Environment::compileSource`, but the guessed line number will make no sense then. I searched for all places triggering a syntax error in Twig, to ensure that they always set the actual line number or set the source directly (so that the guessing logic knows that the template it found is the wrong one and so does not try to use it for guessing). There were only a few missing ones. Commits ------- 6fab6b0 Ensure that syntax errors are triggered with the right line
2 parents b9c6034 + 6fab6b0 commit 3b71efb

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

lib/Twig/Node/Expression/Call.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function getArguments($callable, $arguments)
111111
$named = true;
112112
$name = $this->normalizeName($name);
113113
} elseif ($named) {
114-
throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName));
114+
throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName), $this->getTemplateLine());
115115
}
116116

117117
$parameters[$name] = $node;
@@ -143,14 +143,14 @@ protected function getArguments($callable, $arguments)
143143

144144
if (array_key_exists($name, $parameters)) {
145145
if (array_key_exists($pos, $parameters)) {
146-
throw new Twig_Error_Syntax(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName));
146+
throw new Twig_Error_Syntax(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName), $this->getTemplateLine());
147147
}
148148

149149
if (count($missingArguments)) {
150150
throw new Twig_Error_Syntax(sprintf(
151151
'Argument "%s" could not be assigned for %s "%s(%s)" because it is mapped to an internal PHP function which cannot determine default value for optional argument%s "%s".',
152-
$name, $callType, $callName, implode(', ', $names), count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments))
153-
);
152+
$name, $callType, $callName, implode(', ', $names), count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments)
153+
), $this->getTemplateLine());
154154
}
155155

156156
$arguments = array_merge($arguments, $optionalArguments);
@@ -172,7 +172,7 @@ protected function getArguments($callable, $arguments)
172172
$missingArguments[] = $name;
173173
}
174174
} else {
175-
throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName));
175+
throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName), $this->getTemplateLine());
176176
}
177177
}
178178

@@ -205,7 +205,7 @@ protected function getArguments($callable, $arguments)
205205
throw new Twig_Error_Syntax(sprintf(
206206
'Unknown argument%s "%s" for %s "%s(%s)".',
207207
count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names)
208-
), $unknownParameter ? $unknownParameter->getTemplateLine() : -1);
208+
), $unknownParameter ? $unknownParameter->getTemplateLine() : $this->getTemplateLine());
209209
}
210210

211211
return $arguments;

test/Twig/Tests/Node/Expression/FilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testCompileWithWrongNamedArgumentName()
118118

119119
/**
120120
* @expectedException Twig_Error_Syntax
121-
* @expectedExceptionMessage Value for argument "from" is required for filter "replace".
121+
* @expectedExceptionMessage Value for argument "from" is required for filter "replace" at line 1.
122122
*/
123123
public function testCompileWithMissingNamedArgument()
124124
{

0 commit comments

Comments
 (0)