Passed
Pull Request — master (#2)
by Victor
02:42
created

OptionalNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Twig\Node;
5
6
use Twig_Compiler as Compiler;
7
use Twig_Node as Node;
8
9
final class OptionalNode extends Node
10
{
11
    /**
12
     * @param Node   $body       The body of the suppressed tag.
13
     * @param int    $lineNumber The line number at which the tag occurs in the template.
14
     * @param string $tag        The name of the tag.
15
     */
16 2
    public function __construct(Node $body, int $lineNumber, string $tag)
17
    {
18 2
        parent::__construct(['body' => $body], [], $lineNumber, $tag);
19 2
    }
20
21
    /**
22
     * @param Compiler $compiler
23
     *
24
     * @return void
25
     */
26 2
    public function compile(Compiler $compiler)
27
    {
28
        $compiler
29 2
            ->write("try {\n")
30 2
            ->indent()
31 2
            ->subcompile($this->getNode('body'))
32 2
            ->outdent()
33 2
            ->write("} catch (\\Twig_Error_Runtime \$exception) {\n")
34 2
            ->indent()
35 2
            ->write("if (\$exception->getPrevious() === null) {\n")
36 2
            ->indent()
37 2
            ->write("throw \$exception;\n")
38 2
            ->outdent()
39 2
            ->write("}\n\n")
40 2
            ->write("\$suppressedException = new \\Shoot\\Shoot\\SuppressedException(\$exception->getPrevious());\n")
41 2
            ->outdent()
42 2
            ->write("}\n\n");
43 2
    }
44
}
45