DisplayEndNode   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 26
c 1
b 0
f 0
dl 0
loc 50
ccs 26
cts 26
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A compile() 0 28 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Twig\Node;
5
6
use Shoot\Shoot\Extension;
7
use Shoot\Shoot\SuppressedException;
8
use Shoot\Shoot\View;
9
use Twig\Compiler;
10
use Twig\Node\ModuleNode;
11
use Twig\Node\Node;
12
use Twig\Source;
13
14
/**
15
 * This node is added to the bottom of the display method of a Twig template and is used by Shoot to wrap its contents
16
 * in a callback.
17
 *
18
 * @internal
19
 */
20
final class DisplayEndNode extends Node
21
{
22
    /** @var ModuleNode */
23
    private $module;
24
25
    /**
26
     * @param ModuleNode $module
27
     */
28 6
    public function __construct(ModuleNode $module)
29
    {
30 6
        parent::__construct();
31
32 6
        $this->module = $module;
33
34 6
        $this->setSourceContext($module->getSourceContext());
0 ignored issues
show
Bug introduced by
It seems like $module->getSourceContext() can also be of type null; however, parameter $source of Twig\Node\Node::setSourceContext() does only seem to accept Twig\Source, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->setSourceContext(/** @scrutinizer ignore-type */ $module->getSourceContext());
Loading history...
35 6
    }
36
37
    /**
38
     * @param Compiler $compiler
39
     *
40
     * @return void
41
     */
42 6
    public function compile(Compiler $compiler): void
43
    {
44 6
        if ($this->module->hasAttribute('is_embedded')) {
45 1
            return;
46
        }
47
48
        /** @var Source $source */
49 6
        $source = $this->getSourceContext();
50 6
        $templateName = $source->getName();
51
52 6
        $extensionClass = Extension::class;
53 6
        $suppressedExceptionClass = SuppressedException::class;
54 6
        $viewClass = View::class;
55
56
        $compiler
57 6
            ->raw("\n")
58 6
            ->write("if (\$suppressedException instanceof {$suppressedExceptionClass}) {\n")
59 6
            ->indent()
60 6
            ->write("throw \$suppressedException;\n")
61 6
            ->outdent()
62 6
            ->write("}\n")
63 6
            ->outdent()
64 6
            ->write("};\n\n")
65 6
            ->write("\$this->env\n")
66 6
            ->indent()
67 6
            ->write("->getExtension({$extensionClass}::class)\n")
68 6
            ->write("->process(new {$viewClass}('{$templateName}', \$presentationModel, \$callback));\n")
69 6
            ->outdent();
70 6
    }
71
}
72