From: Fabien Potencier Date: Thu, 6 Oct 2011 17:22:19 +0000 (+0200) Subject: refactored Twig_Node_Expression_GetAttr for better readability of compiled templates X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=dd0546eaa9736d5898eddca6141d2c6270674382;p=web%2Fkonrad%2Ftwig.git refactored Twig_Node_Expression_GetAttr for better readability of compiled templates --- diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index eb9e605..c3be2f8 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -13,14 +13,14 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression { public function __construct(Twig_Node_Expression $node, Twig_Node_Expression $attribute, Twig_NodeInterface $arguments, $type, $lineno) { - parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type), $lineno); + parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type, 'is_defined_test' => false), $lineno); } public function compile(Twig_Compiler $compiler) { $compiler->raw('$this->getAttribute('); - if ($this->hasAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) { + if ($this->getAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) { $compiler->subcompile(new Twig_Node_Expression_Filter( $this->getNode('node'), new Twig_Node_Expression_Constant('default', $this->getLine()), @@ -31,23 +31,29 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression $compiler->subcompile($this->getNode('node')); } - $compiler - ->raw(', ') - ->subcompile($this->getNode('attribute')) - ->raw(', array(') - ; - - foreach ($this->getNode('arguments') as $node) { - $compiler - ->subcompile($node) - ->raw(', ') - ; + $compiler->raw(', ')->subcompile($this->getNode('attribute')); + + if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) { + $compiler->raw(', array('); + + foreach ($this->getNode('arguments') as $node) { + $compiler + ->subcompile($node) + ->raw(', ') + ; + } + + $compiler->raw(')'); + + if (Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) { + $compiler->raw(', ')->repr($this->getAttribute('type')); + } + + if ($this->getAttribute('is_defined_test')) { + $compiler->raw(', true'); + } } - $compiler - ->raw('), ') - ->repr($this->getAttribute('type')) - ->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false') - ->raw(')'); + $compiler->raw(')'); } } diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 94d6135..632270a 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -19,14 +19,14 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression public function __construct($name, $lineno) { - parent::__construct(array(), array('name' => $name), $lineno); + parent::__construct(array(), array('name' => $name, 'is_defined_test' => false), $lineno); } public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); - if ($this->hasAttribute('is_defined_test')) { + if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { $compiler->repr(true); } else { @@ -50,6 +50,6 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression public function isSimple() { - return !$this->isSpecial() && !$this->hasAttribute('is_defined_test'); + return !$this->isSpecial() && !$this->getAttribute('is_defined_test'); } } diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index a683969..e3c2eaa 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase $attr = new Twig_Node_Expression_Constant('bar', 0); $args = new Twig_Node(); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array(), "any", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar")'); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array(), "array", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array(), "array")'); $args = new Twig_Node(array( @@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase new Twig_Node_Expression_Constant('bar', 0), )); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array($this->getContext($context, "foo"), "bar", ), "method", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array($this->getContext($context, "foo"), "bar", ), "method")'); return $tests; }