From 024b9367ebf895f0cb45292bcfc18d6d8cc379b4 Mon Sep 17 00:00:00 2001 From: Leon van der Ree Date: Mon, 27 Dec 2010 14:16:09 +0100 Subject: [PATCH] improved error messages --- lib/Twig/ExpressionParser.php | 2 +- lib/Twig/Template.php | 7 ++++++- test/Twig/Tests/TemplateTest.php | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 6fe0634..204f422 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -344,7 +344,7 @@ class Twig_ExpressionParser while (true) { $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to'); if (in_array($token->getValue(), array('true', 'false', 'none'))) { - throw new Twig_Error_Syntax($token->getValue() . ' cannot be assigned to'); + throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $token->getValue())); } $targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine()); diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index bf0378e..87b29f4 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -128,7 +128,12 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist', $item, $object), -1, $this->getTemplateName()); + if (is_object($object)) { + throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName()); + // array + } else { + throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), -1, $this->getTemplateName()); + } } } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index f01c5e9..6492e88 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -10,6 +10,28 @@ */ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase { + public function getUnkownPropertyOnArrayTests() + { + $tests = array( + array(array('foo' => 'foo', 'bar' => 'value')), + array(new Twig_TemplateObjectArrayAccess()), + ); + + return $tests; + } + + /** + * @dataProvider getUnkownPropertyOnArrayTests + * @expectedException Twig_Error_Runtime + */ + public function testUnkownPropertyOnArray($array) + { + $env = new Twig_Environment(null, array('strict_variables' => true)); + $template = new Twig_TemplateTest($env); + + $template->getAttribute($array, 'unknown', array(), Twig_Node_Expression_GetAttr::TYPE_ARRAY); + } + /** * @dataProvider getGetAttributeTests */ -- 1.7.2.5