From 413be4167b09fbc5212e1544f64d1d34f114ff0e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 18 Apr 2017 16:40:36 -0600 Subject: [PATCH] fixed edge case in the method cache for Twig attributes --- CHANGELOG | 2 +- lib/Twig/Template.php | 13 ++++++++----- test/Twig/Tests/TemplateTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e8009702b..b335d7f3e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.33.2 (2017-XX-XX) - * n/a + * fixed edge case in the method cache for Twig attributes * 1.33.1 (2017-04-18) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index f1855f117..c631befe2 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -627,11 +627,14 @@ abstract class Twig_Template implements Twig_TemplateInterface continue; } - if (!isset($cache[$name])) { - $cache[$name] = $method; - } - if (!isset($cache[$lcName])) { - $cache[$lcName] = $method; + // skip get() and is() methods (in which case, $name is empty) + if ($name) { + if (!isset($cache[$name])) { + $cache[$name] = $method; + } + if (!isset($cache[$lcName])) { + $cache[$lcName] = $method; + } } } self::$cache[$class] = $cache; diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index faa68c6c0..4da2d5750 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -424,6 +424,19 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase return $tests; } + + /** + * @expectedException Twig_Error_Runtime + */ + public function testGetIsMethods() + { + $getIsObject = new Twig_TemplateGetIsMethods(); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true))); + // first time should not create a cache for "get" + $this->assertNull($template->getAttribute($getIsObject, 'get')); + // 0 should be in the method cache now, so this should fail + $this->assertNull($template->getAttribute($getIsObject, 0)); + } } class Twig_TemplateTest extends Twig_Template @@ -669,6 +682,17 @@ class Twig_TemplateMethodObject } } +class Twig_TemplateGetIsMethods +{ + public function get() + { + } + + public function is() + { + } +} + class Twig_TemplateMethodAndPropObject { private $a = 'a_prop';