DisplayEndNode::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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