CommentBlockNode   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 33
c 3
b 0
f 0
dl 0
loc 47
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 35 3
A __construct() 0 4 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) Fabien Potencier
7
 * (c) Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace nystudio107\templatecomments\web\twig\nodes;
14
15
use Twig\Compiler;
16
use Twig\Node\BlockNode;
17
use Twig\Node\Node;
18
use function in_array;
19
20
/**
21
 * Represents a block node.
22
 *
23
 * @author Fabien Potencier <[email protected]>
24
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
class CommentBlockNode extends BlockNode
26
{
27
    private string $blockName;
0 ignored issues
show
Coding Style introduced by
Private member variable "blockName" must be prefixed with an underscore
Loading history...
28
29
    private array $excludeBlocks = ['attr'];
0 ignored issues
show
Coding Style introduced by
Private member variable "excludeBlocks" must be prefixed with an underscore
Loading history...
30
31
    public function __construct(string $name, Node $body, int $lineno, string $tag = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
32
    {
33
        parent::__construct($name, $body, $lineno, $tag);
34
        $this->blockName = $name;
35
    }
36
37
    public function compile(Compiler $compiler): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function compile()
Loading history...
38
    {
39
        $compiler
40
            ->addDebugInfo($this)
41
            ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")
42
            ->indent()
43
            ->write("\$macros = \$this->macros;\n");
44
        if (!in_array($this->blockName, $this->excludeBlocks, false)) {
45
            $compiler
46
                ->write('$_blockTimer = microtime(true)')
47
                ->raw(";\n")
48
                ->write('$_templateName = ')
49
                ->write("'" . $this->getTemplateName() . "'")
50
                ->raw(";\n")
51
                ->write('$_blockName = ')
52
                ->write("'" . $this->blockName . "'")
53
                ->raw(";\n")
54
                ->write('echo PHP_EOL."<!-- >>> BLOCK BEGIN >>> ".$_blockName." FROM ".$_templateName." -->".PHP_EOL')
55
                ->raw(";\n");
56
        }
57
58
        $compiler
59
            ->subcompile($this->getNode('body'))
60
            ->outdent();
61
        if (!in_array($this->blockName, $this->excludeBlocks, false)) {
62
            $compiler
63
                ->write('echo PHP_EOL."<!-- <<< BLOCK END <<< ".$_blockName." FROM ".$_templateName." TIME ".number_format((microtime(true)-$_blockTimer)*1000,2)."ms -->".PHP_EOL')
64
                ->raw(";\n")
65
                ->write("unset(\$_blockTimer);\n")
66
                ->write("unset(\$_blockName);\n")
67
                ->write("unset(\$_templateName);\n");
68
        }
69
70
        $compiler
71
            ->write("}\n\n");
72
    }
73
}
74