Test Failed
Push — dev ( 17f6d8...392257 )
by Janko
10:17 queued 12s
created

TwigPage::resetVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Twig;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Twig\Environment;
10
use Twig\TemplateWrapper;
11
12
final class TwigPage implements TwigPageInterface
13
{
14
    private ?string $template = null;
15
16
    /** @var array<mixed> */
17
    private array $variables = [];
18
19 5
    public function __construct(private Environment $environment) {}
20
21 5
    #[Override]
22
    public function setVar(string $var, mixed $value, bool $isGlobal = false): void
23 2
    {
24
        if ($isGlobal) {
25
            $this->environment->addGlobal($var, $value);
26 2
        } else {
27 1
            $this->variables[$var] = $value;
28
        }
29 1
    }
30
31
    #[Override]
32
    public function setTemplate(string $file): void
33 3
    {
34
        $this->template = $file;
35
    }
36 3
37
    #[Override]
38
    public function isTemplateSet(): bool
39 1
    {
40
        return $this->template !== null;
41
    }
42 1
43
    #[Override]
44
    public function render(): string
45 2
    {
46
        return $this->loadTemplate()->render($this->variables);
47
    }
48 2
49
    private function loadTemplate(): TemplateWrapper
50
    {
51 2
        if ($this->template === null) {
52
            throw new RuntimeException('render not possible if template is not set');
53 2
        }
54
55
        return  $this->environment->load($this->template);
56
    }
57 2
58
    public function resetVariables(): void
59
    {
60
        $this->template = null;
61
        $this->variables = [];
62
    }
63
}
64