From: Fabien Potencier Date: Thu, 12 Apr 2012 12:36:25 +0000 (+0200) Subject: fixed a PHP notice when trying to access a key on a non-object/array variable X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=4e28408ce1fced95251e5f8c8299aa43e9f40f4d;p=web%2Fkonrad%2Ftwig.git fixed a PHP notice when trying to access a key on a non-object/array variable --- diff --git a/CHANGELOG b/CHANGELOG index b7c6694..1613419 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 3cce8bb..9357781 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 a4e658f..4213e2b 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -11,6 +11,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 */ public function testGetAttribute($defined, $value, $object, $item, $arguments, $type, $useExt = false)