From 08c51964e24b78f58fddfa4d699e3a48d7983352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Thu, 23 Oct 2014 17:29:14 +0200 Subject: [PATCH] Fixed wrong line numbers in error messages for multi-line statements --- lib/Twig/Node/Expression/Call.php | 10 +++++++++- lib/Twig/Node/Expression/Name.php | 2 ++ lib/Twig/Test/NodeTestCase.php | 8 +++++--- .../multiline_array_with_undefined_variable.test | 13 +++++++++++++ .../multiline_function_with_undefined_variable.test | 12 ++++++++++++ .../multiline_function_with_unknown_argument.test | 9 +++++++++ .../multiline_tag_with_undefined_variable.test | 12 ++++++++++++ test/Twig/Tests/Node/Expression/FilterTest.php | 2 +- test/Twig/Tests/Node/Expression/GetAttrTest.php | 6 +++--- test/Twig/Tests/Node/Expression/NameTest.php | 8 ++++---- 10 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/exceptions/multiline_array_with_undefined_variable.test create mode 100644 test/Twig/Tests/Fixtures/exceptions/multiline_function_with_undefined_variable.test create mode 100644 test/Twig/Tests/Fixtures/exceptions/multiline_function_with_unknown_argument.test create mode 100644 test/Twig/Tests/Fixtures/exceptions/multiline_tag_with_undefined_variable.test diff --git a/lib/Twig/Node/Expression/Call.php b/lib/Twig/Node/Expression/Call.php index 5703b61df..4e4ba1172 100644 --- a/lib/Twig/Node/Expression/Call.php +++ b/lib/Twig/Node/Expression/Call.php @@ -164,10 +164,18 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression } if (!empty($parameters)) { + $unknownParameter = null; + foreach ($parameters as $parameter) { + if ($parameter instanceof Twig_Node) { + $unknownParameter = $parameter; + break; + } + } + throw new Twig_Error_Syntax(sprintf( 'Unknown argument%s "%s" for %s "%s(%s)".', count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $this->getAttribute('type'), $this->getAttribute('name'), implode(', ', $names) - )); + ), $unknownParameter ? $unknownParameter->getLine() : -1); } return $arguments; diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 3b8fae01d..4cbdea975 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -26,6 +26,8 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression { $name = $this->getAttribute('name'); + $compiler->addDebugInfo($this); + if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); diff --git a/lib/Twig/Test/NodeTestCase.php b/lib/Twig/Test/NodeTestCase.php index b15c85ffd..9eb44614f 100644 --- a/lib/Twig/Test/NodeTestCase.php +++ b/lib/Twig/Test/NodeTestCase.php @@ -38,13 +38,15 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase return new Twig_Environment(); } - protected function getVariableGetter($name) + protected function getVariableGetter($name, $line = false) { + $line = $line > 0 ? "// line {$line}\n" : ''; + if (version_compare(phpversion(), '5.4.0RC1', '>=')) { - return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name); + return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name); } - return sprintf('$this->getContext($context, "%s")', $name); + return sprintf('%s$this->getContext($context, "%s")', $line, $name); } protected function getAttributeGetter() diff --git a/test/Twig/Tests/Fixtures/exceptions/multiline_array_with_undefined_variable.test b/test/Twig/Tests/Fixtures/exceptions/multiline_array_with_undefined_variable.test new file mode 100644 index 000000000..3b04468e8 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/multiline_array_with_undefined_variable.test @@ -0,0 +1,13 @@ +--TEST-- +Exception for multiline array with undefined variable +--TEMPLATE-- +{% set foo = { + foo: 'foo', + bar: 'bar', + foobar: foobar, + foo2: 'foo2' +} %} +--DATA-- +return array() +--EXCEPTION-- +Twig_Error_Runtime: Variable "foobar" does not exist in "index.twig" at line 5 diff --git a/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_undefined_variable.test b/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_undefined_variable.test new file mode 100644 index 000000000..d799a3906 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_undefined_variable.test @@ -0,0 +1,12 @@ +--TEST-- +Exception for multile function with undefined variable +--TEMPLATE-- +{{ include('foo', + with_context=with_context +) }} +--TEMPLATE(foo)-- +Foo +--DATA-- +return array() +--EXCEPTION-- +Twig_Error_Runtime: Variable "with_context" does not exist in "index.twig" at line 3 diff --git a/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_unknown_argument.test b/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_unknown_argument.test new file mode 100644 index 000000000..64761fcf1 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/multiline_function_with_unknown_argument.test @@ -0,0 +1,9 @@ +--TEST-- +Exception for multiline function with unknown argument +--TEMPLATE-- +{{ include('foo', + with_context=True, + invalid=False +) }} +--EXCEPTION-- +Twig_Error_Syntax: Unknown argument "invalid" for function "include(template, variables, with_context, ignore_missing, sandboxed)" in "index.twig" at line 4. diff --git a/test/Twig/Tests/Fixtures/exceptions/multiline_tag_with_undefined_variable.test b/test/Twig/Tests/Fixtures/exceptions/multiline_tag_with_undefined_variable.test new file mode 100644 index 000000000..096a5dbf5 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/multiline_tag_with_undefined_variable.test @@ -0,0 +1,12 @@ +--TEST-- +Exception for multiline tag with undefined variable +--TEMPLATE-- +{% include 'foo' + with vars +%} +--TEMPLATE(foo)-- +Foo +--DATA-- +return array() +--EXCEPTION-- +Twig_Error_Runtime: Variable "vars" does not exist in "index.twig" at line 3 diff --git a/test/Twig/Tests/Node/Expression/FilterTest.php b/test/Twig/Tests/Node/Expression/FilterTest.php index d825863e6..2b85141cd 100644 --- a/test/Twig/Tests/Node/Expression/FilterTest.php +++ b/test/Twig/Tests/Node/Expression/FilterTest.php @@ -74,7 +74,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase /** * @expectedException Twig_Error_Syntax - * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)". + * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1. */ public function testCompileWithWrongNamedArgumentName() { diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index 6d061b0e3..2764478c4 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -34,16 +34,16 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Test_NodeTestCase $attr = new Twig_Node_Expression_Constant('bar', 1); $args = new Twig_Node_Expression_Array(array(), 1); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ANY_CALL, 1); - $tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ARRAY_CALL, 1); - $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))); $args = new Twig_Node_Expression_Array(array(), 1); $args->addElement(new Twig_Node_Expression_Name('foo', 1)); $args->addElement(new Twig_Node_Expression_Constant('bar', 1)); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::METHOD_CALL, 1); - $tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo'))); return $tests; } diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index e8d4c96f5..d013affad 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -28,10 +28,10 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase $env1 = new Twig_Environment(null, array('strict_variables' => false)); return array( - version_compare(PHP_VERSION, '5.4.0') >= 0 ? array($node, '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env) : array($node, '$this->getContext($context, "foo")', $env), - array($node, $this->getVariableGetter('foo'), $env1), - array($self, '$this'), - array($context, '$context'), + array($node, "// line 1\n".(version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env), + array($node, $this->getVariableGetter('foo', 1), $env1), + array($self, "// line 1\n\$this"), + array($context, "// line 1\n\$context"), ); } }