diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 64563419e..1275c131e 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -568,6 +568,8 @@ abstract class Twig_Template implements Twig_TemplateInterface if (null === $object) { $message = sprintf('Impossible to invoke a method ("%s") on a null variable.', $item); + } elseif (is_array($object)) { + $message = sprintf('Impossible to invoke a method ("%s") on an array.', $item); } else { $message = sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s").', $item, gettype($object), $object); } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index c22a43336..336fc1f56 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -58,6 +58,7 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1.'), array('{{ null.a }}', 'Impossible to access an attribute ("a") on a null variable in "%s" at line 1.'), array('{{ null.a() }}', 'Impossible to invoke a method ("a") on a null variable in "%s" at line 1.'), + array('{{ array.a() }}', 'Impossible to invoke a method ("a") on an array in "%s" at line 1.'), array('{{ empty_array.a }}', 'Key "a" does not exist as the array is empty in "%s" at line 1.'), array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1.'), array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1.'),