TwigEngine   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 4 1
A twig() 0 7 2
A render() 0 5 1
A renderTemplate() 0 6 1
A createTwig() 0 10 1
A setCache() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\View\Twig;
6
7
// From Twig
8
use Twig_Environment;
9
10
// From 'charcoal-view'
11
use Charcoal\View\AbstractEngine;
12
13
/**
14
 *
15
 */
16
class TwigEngine extends AbstractEngine
17
{
18
    const DEFAULT_CACHE_PATH = '../cache/twig';
19
20
    /**
21
     * @var Twig_Environment $twig
22
     */
23
    private $twig;
24
25
    /**
26
     * @return string
27
     */
28
    public function type(): string
29
    {
30
        return 'twig';
31
    }
32
33
    /**
34
     * @return Twig_Environment
35
     */
36
    public function twig(): Twig_Environment
37
    {
38
        if ($this->twig === null) {
39
            $this->twig = $this->createTwig();
40
        }
41
        return $this->twig;
42
    }
43
44
    /**
45
     * @param string $templateIdent The template identifier to load and render.
46
     * @param mixed  $context       The rendering context.
47
     * @return string The rendered template string.
48
     */
49
    public function render(string $templateIdent, $context): string
50
    {
51
        $arrayContext = json_decode(json_encode($context), true);
52
        return $this->twig()->render($templateIdent, $arrayContext);
53
    }
54
55
    /**
56
     * @param string $templateString The template string to render.
57
     * @param mixed  $context        The rendering context.
58
     * @return string The rendered template string.
59
     */
60
    public function renderTemplate(string $templateString, $context): string
61
    {
62
        $template = $this->twig()->createTemplate($templateString);
63
        $arrayContext = json_decode(json_encode($context), true);
64
        return $template->render($arrayContext);
65
    }
66
67
    /**
68
     * @return Twig_Environment
69
     */
70
    protected function createTwig(): Twig_Environment
71
    {
72
        $twig = new Twig_Environment($this->loader(), [
0 ignored issues
show
Documentation introduced by
$this->loader() is of type object<Charcoal\View\LoaderInterface>, but the function expects a null|object<Twig\Loader\LoaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
            'cache'     => $this->cache(),
74
            'charset'   => 'utf-8',
75
            'debug'     => false
76
        ]);
77
78
        return $twig;
79
    }
80
81
    /**
82
     * Set the engine's cache implementation.
83
     *
84
     * @param  mixed $cache A Twig cache option.
85
     * @return void
86
     */
87
    protected function setCache($cache): void
88
    {
89
        /**
90
         * If NULL is specified, the value is converted to FALSE
91
         * because Twig internally requires FALSE to disable the cache.
92
         */
93
        if ($cache === null) {
94
            $cache = false;
95
        }
96
97
        parent::setCache($cache);
98
    }
99
}
100