DeferReference   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 0
cbo 4
dl 0
loc 43
ccs 25
cts 25
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B compile() 0 27 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