diff --git a/src/Error/Error.php b/src/Error/Error.php index 07987536c..e89400833 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -61,7 +61,7 @@ class Error extends \Exception * @param Source|string|null $source The source context where the error occurred * @param \Exception $previous The previous exception */ - public function __construct($message, $lineno = -1, $source = null, \Exception $previous = null) + public function __construct($message, $lineno = -1, $source = null, \Exception $previous = null, $autoGuess = true) { parent::__construct('', 0, $previous); @@ -79,7 +79,7 @@ class Error extends \Exception $this->lineno = $lineno; $this->name = $name; - if (-1 === $lineno || null === $name || null === $this->sourcePath) { + if ($autoGuess && (-1 === $lineno || null === $name || null === $this->sourcePath)) { $this->guessTemplateInfo(); } diff --git a/src/Error/LoaderError.php b/src/Error/LoaderError.php index f63069a59..5a1cd1ecc 100644 --- a/src/Error/LoaderError.php +++ b/src/Error/LoaderError.php @@ -28,9 +28,7 @@ class LoaderError extends Error { public function __construct($message, $lineno = -1, $source = null, \Exception $previous = null) { - \Exception::__construct('', 0, $previous); - $this->appendMessage($message); - $this->setTemplateLine(-2); + parent::__construct($message, $lineno, $source, $previous, false); } } diff --git a/src/Node/Expression/CallExpression.php b/src/Node/Expression/CallExpression.php index 905c074ce..59be15b21 100644 --- a/src/Node/Expression/CallExpression.php +++ b/src/Node/Expression/CallExpression.php @@ -125,7 +125,7 @@ abstract class CallExpression extends AbstractExpression $named = true; $name = $this->normalizeName($name); } elseif ($named) { - throw new SyntaxError(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName), $this->getTemplateLine()); + throw new SyntaxError(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $callType, $callName), $this->getTemplateLine(), null, null, false); } $parameters[$name] = $node; @@ -157,14 +157,14 @@ abstract class CallExpression extends AbstractExpression if (\array_key_exists($name, $parameters)) { if (\array_key_exists($pos, $parameters)) { - throw new SyntaxError(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName), $this->getTemplateLine()); + throw new SyntaxError(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), null, null, false); } if (\count($missingArguments)) { throw new SyntaxError(sprintf( '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".', $name, $callType, $callName, implode(', ', $names), \count($missingArguments) > 1 ? 's' : '', implode('", "', $missingArguments) - ), $this->getTemplateLine()); + ), $this->getTemplateLine(), null, null, false); } $arguments = array_merge($arguments, $optionalArguments); @@ -186,7 +186,7 @@ abstract class CallExpression extends AbstractExpression $missingArguments[] = $name; } } else { - throw new SyntaxError(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName), $this->getTemplateLine()); + throw new SyntaxError(sprintf('Value for argument "%s" is required for %s "%s".', $name, $callType, $callName), $this->getTemplateLine(), null, null, false); } } @@ -219,7 +219,7 @@ abstract class CallExpression extends AbstractExpression throw new SyntaxError(sprintf( 'Unknown argument%s "%s" for %s "%s(%s)".', \count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names) - ), $unknownParameter ? $unknownParameter->getTemplateLine() : $this->getTemplateLine()); + ), $unknownParameter ? $unknownParameter->getTemplateLine() : $this->getTemplateLine(), null, null, false); } return $arguments; diff --git a/src/Node/Expression/Test/DefinedTest.php b/src/Node/Expression/Test/DefinedTest.php index 645ea5d54..5217a9707 100644 --- a/src/Node/Expression/Test/DefinedTest.php +++ b/src/Node/Expression/Test/DefinedTest.php @@ -48,7 +48,7 @@ class DefinedTest extends TestExpression } elseif ($node instanceof ConstantExpression || $node instanceof ArrayExpression) { $node = new ConstantExpression(true, $node->getTemplateLine()); } else { - throw new SyntaxError('The "defined" test only works with simple variables.', $this->getTemplateLine()); + throw new SyntaxError('The "defined" test only works with simple variables.', $this->getTemplateLine(), null, null, false); } parent::__construct($node, $name, $arguments, $lineno); diff --git a/src/Node/MacroNode.php b/src/Node/MacroNode.php index 105768b10..11020946e 100644 --- a/src/Node/MacroNode.php +++ b/src/Node/MacroNode.php @@ -27,7 +27,7 @@ class MacroNode extends Node { foreach ($arguments as $argumentName => $argument) { if (self::VARARGS_NAME === $argumentName) { - throw new SyntaxError(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine()); + throw new SyntaxError(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine(), null, null, false); } } diff --git a/src/Template.php b/src/Template.php index 669d36054..5919b74e5 100644 --- a/src/Template.php +++ b/src/Template.php @@ -189,8 +189,7 @@ abstract class Template // this is mostly useful for \Twig\Error\LoaderError exceptions // see \Twig\Error\LoaderError - if (-2 === $e->getTemplateLine()) { - $e->setTemplateLine(-1); + if (-1 === $e->getTemplateLine()) { $e->guess(); } @@ -376,8 +375,7 @@ abstract class Template // this is mostly useful for \Twig\Error\LoaderError exceptions // see \Twig\Error\LoaderError - if (-2 === $e->getTemplateLine()) { - $e->setTemplateLine(-1); + if (-1 === $e->getTemplateLine()) { $e->guess(); }