MustacheEngine   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 193
Duplicated Lines 20.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 39
loc 193
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 4 1
A __construct() 0 8 2
B setHelpers() 20 20 6
A mergeHelpers() 19 19 6
A addHelper() 0 12 2
A helpers() 0 4 1
A render() 0 4 1
A renderTemplate() 0 4 1
A mustache() 0 8 2
A createMustache() 0 12 1
A setCache() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Charcoal\View\Mustache;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
use Traversable;
8
9
// From Mustache
10
use Mustache_Engine;
11
12
// From 'charcoal-view'
13
use Charcoal\View\AbstractEngine;
14
15
/**
16
 * Mustache view rendering engine.
17
 */
18
class MustacheEngine extends AbstractEngine
19
{
20
    const DEFAULT_CACHE_PATH = '../cache/mustache';
21
22
    /**
23
     * A collection of helpers.
24
     *
25
     * @var array
26
     */
27
    private $helpers = [];
28
29
    /**
30
     * The renderering framework.
31
     *
32
     * @var Mustache_Engine
33
     */
34
    private $mustache;
35
36
    /**
37
     * @return string
38
     */
39
    public function type(): string
40
    {
41
        return 'mustache';
42
    }
43
44
    /**
45
     * Build the Mustache Engine with an array of dependencies.
46
     *
47
     * @param array $data Engine dependencie.
48
     */
49
    public function __construct(array $data)
50
    {
51
        parent::__construct($data);
52
53
        if (isset($data['helpers'])) {
54
            $this->setHelpers($data['helpers']);
55
        }
56
    }
57
58
    /**
59
     * Set the engine's helpers.
60
     *
61
     * @param  array|Traversable|HelpersInterface $helpers Mustache helpers.
62
     * @throws InvalidArgumentException If the given helper(s) are invalid.
63
     * @return self
64
     */
65 View Code Duplication
    public function setHelpers($helpers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        if ($helpers instanceof HelpersInterface) {
68
            $helpers = $helpers->toArray();
69
        }
70
71
        if (!is_array($helpers) && !$helpers instanceof Traversable) {
72
            throw new InvalidArgumentException(sprintf(
73
                'setHelpers expects an array of helpers, received %s',
74
                (is_object($helpers) ? get_class($helpers) : gettype($helpers))
75
            ));
76
        }
77
78
        $this->helpers = [];
79
        foreach ($helpers as $name => $helper) {
80
            $this->addHelper($name, $helper);
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Merge (replacing or adding) the engine's helpers.
88
     *
89
     * @param  array|Traversable|HelpersInterface $helpers Mustache helpers.
90
     * @throws InvalidArgumentException If the given helper(s) are invalid.
91
     * @return self
92
     */
93 View Code Duplication
    public function mergeHelpers($helpers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        if ($helpers instanceof HelpersInterface) {
96
            $helpers = $helpers->toArray();
97
        }
98
99
        if (!is_array($helpers) && !$helpers instanceof Traversable) {
100
            throw new InvalidArgumentException(sprintf(
101
                'mergeHelpers expects an array of helpers, received %s',
102
                (is_object($helpers) ? get_class($helpers) : gettype($helpers))
103
            ));
104
        }
105
106
        foreach ($helpers as $name => $helper) {
107
            $this->addHelper($name, $helper);
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * Add a helper.
115
     *
116
     * @param  string $name   The tag name.
117
     * @param  mixed  $helper The tag value.
118
     * @throws RuntimeException If the mustache engine was already initialized.
119
     * @return self
120
     */
121
    public function addHelper(string $name, $helper)
122
    {
123
        if ($this->mustache !== null) {
124
            throw new RuntimeException(
125
                'Can not add helper to Mustache engine: the engine has already been initialized.'
126
            );
127
        }
128
129
        $this->helpers[$name] = $helper;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Retrieve the engine's helpers.
136
     *
137
     * @return array
138
     */
139
    public function helpers(): array
140
    {
141
        return $this->helpers;
142
    }
143
144
    /**
145
     * @param string $templateIdent The template identifier to load and render.
146
     * @param mixed  $context       The rendering context.
147
     * @return string The rendered template string.
148
     */
149
    public function render(string $templateIdent, $context): string
150
    {
151
        return $this->mustache()->render($templateIdent, $context);
152
    }
153
154
    /**
155
     * @param string $templateString The template string to render.
156
     * @param mixed  $context        The rendering context.
157
     * @return string The rendered template string.
158
     */
159
    public function renderTemplate(string $templateString, $context): string
160
    {
161
        return $this->mustache()->render($templateString, $context);
162
    }
163
164
    /**
165
     * @return Mustache_Engine
166
     */
167
    protected function mustache(): Mustache_Engine
168
    {
169
        if ($this->mustache === null) {
170
            $this->mustache = $this->createMustache();
171
        }
172
173
        return $this->mustache;
174
    }
175
176
    /**
177
     * @return Mustache_Engine
178
     */
179
    protected function createMustache(): Mustache_Engine
180
    {
181
        $mustache = new Mustache_Engine([
182
            'cache'             => $this->cache(),
183
            'loader'            => $this->loader(),
184
            'partials_loader'   => $this->loader(),
185
            'strict_callables'  => true,
186
            'helpers'           => $this->helpers()
187
        ]);
188
189
        return $mustache;
190
    }
191
192
    /**
193
     * Set the engine's cache implementation.
194
     *
195
     * @param  mixed $cache A Mustache cache option.
196
     * @return void
197
     */
198
    protected function setCache($cache): void
199
    {
200
        /**
201
         * If FALSE is specified, the value is converted to NULL
202
         * because Mustache internally requires NULL to disable the cache.
203
         */
204
        if ($cache === false) {
205
            $cache = null;
206
        }
207
208
        parent::setCache($cache);
209
    }
210
}
211