FlashesNode   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A compile() 0 71 3
1
<?php
2
3
namespace Knp\RadBundle\Twig;
4
5
use Twig_Node;
6
use Twig_NodeInterface;
7
use Twig_Node_Expression;
8
use Twig_Compiler;
9
10
class FlashesNode extends Twig_Node
11
{
12
    public function __construct(Twig_Node_Expression $types = null, Twig_Node_Expression $catalog = null, Twig_NodeInterface $body, $lineno)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
13
    {
14
        $nodes = array('body' => $body);
15
        if (null !== $types) {
16
            $nodes['types'] = $types;
17
        }
18
        if (null !== $catalog) {
19
            $nodes['catalog'] = $catalog;
20
        }
21
22
        parent::__construct($nodes, array(), $lineno, 'flashes');
23
    }
24
25
    public function compile(Twig_Compiler $compiler)
26
    {
27
        $compiler->addDebugInfo($this);
28
29
        $compiler->write('$types   = ');
30
        if ($this->hasNode('types')) {
31
            $compiler->subcompile($this->getNode('types'));
32
        } else {
33
            $compiler->repr(null);
34
        }
35
        $compiler->raw(";\n");
36
37
        $compiler->write('$catalog = ');
38
        if ($this->hasNode('catalog')) {
39
            $compiler->subcompile($this->getNode('catalog'));
40
        } else {
41
            $compiler->repr(null);
42
        }
43
        $compiler->raw(";\n");
44
45
        $compiler
46
            ->write("\$savedContext   = null;\n")
47
            ->write("\$flashesByTypes = array();\n")
48
            ->write("\$numFlashes     = 0;\n")
49
            ->write("foreach (\$this->env->getExtension('flash')->getFlashes(\$types) as \$type => \$flashes) {\n")
50
            ->indent()
51
            ->write("\$flashesByTypes[\$type] = \$flashes;\n")
52
            ->write("\$numFlashes+= count(\$flashes);\n")
53
            ->outdent()
54
            ->write("}\n")
55
            ->write("\$index = 0;\n")
56
            ->write("foreach (\$flashesByTypes as \$type => \$flashes) {\n")
57
            ->indent()
58
            ->write("foreach (\$flashes as \$flash) {\n")
59
            ->indent()
60
            ->write("if (null === \$savedContext) {\n")
61
            ->indent()
62
            ->write("\$savedContext = \$context;\n")
63
            ->outdent()
64
            ->write("}\n")
65
            ->write('$context = array(')
66
            ->raw("\n")
67
            ->indent()
68
            ->write("'type'    => \$type,\n")
69
            ->write("'message' => \$this->env->getExtension('flash')->renderMessage(\$flash, \$catalog),\n")
70
            ->write("'loop'    => array(\n")
71
            ->indent()
72
            ->write("'first'  => 0 === \$index,\n")
73
            ->write("'last'   => \$numFlashes === \$index,\n")
74
            ->write("'length' => \$numFlashes,\n")
75
            ->write("'index0' => \$index,\n")
76
            ->write("'index'  => \$index + 1\n")
77
            ->outdent()
78
            ->write(")\n")
79
            ->outdent()
80
            ->write(");\n")
81
            ->subcompile($this->getNode('body'))
82
            ->write("\$index++;\n")
83
            ->outdent()
84
            ->write("}\n")
85
            ->outdent()
86
            ->write("}\n")
87
            ->write("if (null !== \$savedContext) {\n")
88
            ->indent()
89
            ->write("\$context = \$savedContext;\n")
90
            ->write("unset(\$savedContext);\n")
91
            ->outdent()
92
            ->write("}\n")
93
            ->write("unset(\$index, \$flashesByTypes, \$types, \$catalog);\n")
94
        ;
95
    }
96
}
97