further improved exception message and distinguish array_call and any_call access
authorTobias Schultze <webmaster@tubo-world.de>
Wed, 31 Oct 2012 20:00:51 +0000 (21:00 +0100)
committerTobias Schultze <webmaster@tubo-world.de>
Fri, 15 Mar 2013 11:00:26 +0000 (12:00 +0100)
lib/Twig/Template.php
test/Twig/Tests/TemplateTest.php

index 507c612..6f3d87d 100644 (file)
@@ -363,8 +363,10 @@ abstract class Twig_Template implements Twig_TemplateInterface
                     throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $arrayItem, get_class($object)), -1, $this->getTemplateName());
                 } elseif (is_array($object)) {
                     throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName());
+                } elseif (Twig_TemplateInterface::ARRAY_CALL === $type) {
+                    throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
                 } else {
-                    throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
+                    throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
                 }
             }
         }
@@ -378,7 +380,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                 return null;
             }
 
-            throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
+            throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
         }
 
         $class = get_class($object);
index f82319c..4a0c3f6 100644 (file)
@@ -43,11 +43,11 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
     public function getAttributeExceptions()
     {
         $tests = array(
-            array('{{ string["a"] }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false),
+            array('{{ string["a"] }}', 'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1', false),
             array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
             array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
-            array('{{ string.a }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false),
-            array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a "string" variable in "%s" at line 1', false),
+            array('{{ string.a }}', 'Impossible to access an item ("a") on a string variable ("foo") in "%s" at line 1', false),
+            array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1', false),
             array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
             array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1', false),
             array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
@@ -344,8 +344,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
 
         // tests when input is not an array or object
         $tests = array_merge($tests, array(
-            array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "integer" variable'),
-            array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "string" variable'),
+            array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a integer variable ("42")'),
+            array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a string variable ("string")'),
             array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'),
         ));