Passed
Push — master ( 1416ae...b5a8b1 )
by Luis
54s queued 13s
created

TemplateEngine   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 3
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Templates;
9
10
use Twig\Environment as Twig;
11
use Twig\Error\LoaderError;
12
use Twig\Error\RuntimeError;
13
use Twig\Error\SyntaxError;
14
use Twig\Loader\FilesystemLoader;
15
use Twig\TwigFilter as Filter;
16
17
/** @noRector \Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector */
18
class TemplateEngine
19
{
20
    private readonly Twig $twig;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 20 at column 21
Loading history...
21
22 41
    public function __construct(Twig $twig = null)
23
    {
24 41
        $this->twig = $twig ?? new Twig(
25 39
            new FilesystemLoader(__DIR__ . '/../resources/templates'),
26 39
            ['strict_variables' => true]
27
        );
28 41
        $this->twig->addFilter(new Filter(
29
            'whitespace',
30 41
            static fn (string $html): ?string => preg_replace('/\s\s+/', '', $html)
31
        ));
32
    }
33
34
    /** @param mixed[] $values */
35 32
    public function render(string $template, array $values): string
36
    {
37
        try {
38 32
            return $this->twig->render($template, $values);
39 1
        } catch (LoaderError | RuntimeError  | SyntaxError $e) {
40 1
            throw new TemplateFailure($e);
41
        }
42
    }
43
}
44