Skip to content

Commit

Permalink
refactored Twig_Node_Expression_GetAttr for better readability of com…
Browse files Browse the repository at this point in the history
…piled templates
  • Loading branch information
fabpot committed Oct 8, 2011
1 parent a684251 commit dd0546e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
42 changes: 24 additions & 18 deletions lib/Twig/Node/Expression/GetAttr.php
Expand Up @@ -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()),
Expand All @@ -31,23 +31,29 @@ public function compile(Twig_Compiler $compiler)
$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(')');
}
}
6 changes: 3 additions & 3 deletions lib/Twig/Node/Expression/Name.php
Expand Up @@ -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 {
Expand All @@ -50,6 +50,6 @@ public function isSpecial()

public function isSimple()
{
return !$this->isSpecial() && !$this->hasAttribute('is_defined_test');
return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
}
}
6 changes: 3 additions & 3 deletions test/Twig/Tests/Node/Expression/GetAttrTest.php
Expand Up @@ -49,18 +49,18 @@ public function getTests()
$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(
new Twig_Node_Expression_Name('foo', 0),
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;
}
Expand Down

0 comments on commit dd0546e

Please sign in to comment.