OptionalNode   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 23
c 2
b 0
f 0
dl 0
loc 41
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 24 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Twig\Node;
5
6
use Shoot\Shoot\SuppressedException;
7
use Twig\Compiler;
8
use Twig\Error\RuntimeError;
9
use Twig\Node\Node;
10
11
/**
12
 * Represents the optional tag used to suppress runtime exceptions in templates.
13
 *
14
 * @internal
15
 */
16
final class OptionalNode extends Node
17
{
18
    /**
19
     * @param Node   $body
20
     * @param int    $lineNumber
21
     * @param string $tag
22
     */
23 1
    public function __construct(Node $body, int $lineNumber, string $tag)
24
    {
25 1
        parent::__construct(['body' => $body], [], $lineNumber, $tag);
26 1
    }
27
28
    /**
29
     * @param Compiler $compiler
30
     *
31
     * @return void
32
     */
33 1
    public function compile(Compiler $compiler): void
34
    {
35 1
        $runtimeErrorClass = RuntimeError::class;
36 1
        $suppressedExceptionClass = SuppressedException::class;
37
38
        $compiler
39 1
            ->write("try {\n")
40 1
            ->indent()
41 1
            ->write("ob_start();\n\n")
42 1
            ->subcompile($this->getNode('body'))
43 1
            ->raw("\n")
44 1
            ->write("echo ob_get_clean();\n")
45 1
            ->outdent()
46 1
            ->write("} catch ({$runtimeErrorClass} \$exception) {\n")
47 1
            ->indent()
48 1
            ->write("ob_end_clean();\n\n")
49 1
            ->write("if (\$exception->getPrevious() === null) {\n")
50 1
            ->indent()
51 1
            ->write("throw \$exception;\n")
52 1
            ->outdent()
53 1
            ->write("}\n\n")
54 1
            ->write("\$suppressedException = new {$suppressedExceptionClass}(\$exception->getPrevious());\n")
55 1
            ->outdent()
56 1
            ->write("}\n\n");
57 1
    }
58
}
59