diff --git a/CHANGELOG b/CHANGELOG index b7c669418..161341990 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.7.0 (2012-XX-XX) + * fixed a PHP notice when trying to access a key on a non-object/array variable * enhanced error reporting when the template file is an instance of SplFileInfo * added Twig_Environment::mergeGlobals() * added compilation checks to avoid misuses of the sandbox tag diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 3cce8bbfd..93577819a 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -351,9 +351,10 @@ abstract class Twig_Template implements Twig_TemplateInterface 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))); - // array - } else { + } elseif (is_array($object)) { throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object)))); + } else { + throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $item, gettype($object))); } } } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index a4e658f93..4213e2b65 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -10,6 +10,20 @@ */ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase { + /** + * @expectedException Twig_Error_Runtime + * @expectedExceptionMessage Impossible to access a key ("a") on a "string" variable + */ + public function testAttributeOnAString() + { + $template = new Twig_TemplateTest( + new Twig_Environment(null, array('strict_variables' => true)), + false + ); + + $template->getAttribute('string', 'a', array(), Twig_TemplateInterface::ARRAY_CALL, false); + } + /** * @dataProvider getGetAttributeTests */