DeferReference::compile()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 27
ccs 20
cts 20
cp 1
rs 8.8571
cc 3
eloc 20
nc 4
nop 1
crap 3
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Twig\Node\Expression;
3
4
use Twig_Compiler;
5
use Twig_Error_Syntax;
6
use Twig_Node;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class DeferReference extends \Twig_Node_Expression_BlockReference
12
{
13 2
    public function __construct($name, Twig_Node $args, $line, $tag = null)
14
    {
15 2
        if ($args->count() !== 1) {
16 1
            throw new Twig_Error_Syntax(sprintf('Only one argument is allowed for "%s".', $name));
17
        }
18 1
        parent::__construct($args->nodes[0], false, $line, $tag);
19 1
    }
20
21
    /**
22
     * Compiles the node to PHP.
23
     *
24
     * @param Twig_Compiler $compiler A Twig_Compiler instance
25
     */
26 3
    public function compile(Twig_Compiler $compiler)
27
    {
28 3
        if ($this->getAttribute('as_string')) {
29 1
            $compiler->raw('(string) ');
30 1
        }
31
32 3
        if ($this->getAttribute('output')) {
33
            $compiler
34 1
                ->addDebugInfo($this)
35 1
                ->write("\$_defer_block_references = \$this->env->getExtension('defer')->retrieve(")
36 1
                    ->subcompile($this->getNode('name'))
37 1
                    ->raw(");\n")
38 1
                ->write("foreach (\$_defer_block_references as \$_defer_block_reference) {\n")
39 1
                ->indent()
40 1
                    ->write("echo \$_defer_block_reference;\n")
41 1
                ->outdent()
42 1
                ->write("}\n")
43 1
                ->write("unset(\$_defer_block_references,\$_defer_block_reference);\n")
44
            ;
45 1
        } else {
46
            $compiler
47 2
                ->raw("implode('', \$this->env->getExtension('defer')->retrieve(")
48 2
                    ->subcompile($this->getNode('name'))
49 2
                    ->raw("))\n")
50
            ;
51
        }
52 3
    }
53
}
54