AssetsHelpers   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 235
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 40
loc 235
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 38 1
A purgeJs() 0 5 1
A addJs() 7 7 2
A js() 0 6 1
A addJsRequirement() 13 13 3
A jsRequirements() 0 6 1
A purgeCss() 0 5 1
A addCss() 7 7 2
A css() 0 6 1
A addCssRequirement() 13 13 3
A cssRequirements() 0 6 1
A purgeAssets() 0 5 1

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
declare(strict_types=1);
4
5
namespace Charcoal\View\Mustache;
6
7
// From Mustache
8
use Mustache_LambdaHelper as LambdaHelper;
9
10
/**
11
 * Mustache helpers for rendering CSS and JavaScript.
12
 */
13
class AssetsHelpers implements HelpersInterface
14
{
15
    /**
16
     * A string concatenation of inline `<script>` elements.
17
     *
18
     * @var string
19
     */
20
    private static $js = '';
21
22
    /**
23
     * An array of `<script>` elements referencing external scripts.
24
     *
25
     * @var array
26
     */
27
    private static $jsRequirements = [];
28
29
    /**
30
     * A string concatenation of inline `<style>` elements.
31
     *
32
     * @var string
33
     */
34
    private static $css = '';
35
36
    /**
37
     * An array of `<link>` elements referencing external style sheets.
38
     *
39
     * @var array
40
     */
41
    private static $cssRequirements = [];
42
43
    /**
44
     * Retrieve the collection of helpers.
45
     *
46
     * @return array
47
     */
48
    public function toArray(): array
49
    {
50
        return [
51
            'purgeJs' => function() {
52
                $this->purgeJs();
53
            },
54
            'addJs' => function($js, LambdaHelper $helper) {
55
                $this->addJs($js, $helper);
56
            },
57
            'js' => function() {
58
                return $this->js();
59
            },
60
            'addJsRequirement' => function($js, LambdaHelper $helper) {
61
                $this->addJsRequirement($js, $helper);
62
            },
63
            'jsRequirements' => function() {
64
                return $this->jsRequirements();
65
            },
66
            'addCss' => function($css, LambdaHelper $helper) {
67
                $this->addCss($css, $helper);
68
            },
69
            'purgeCss' => function() {
70
                $this->purgeCss();
71
            },
72
            'css' => function() {
73
                return $this->css();
74
            },
75
            'addCssRequirement' => function($css, LambdaHelper $helper) {
76
                $this->addCssRequirement($css, $helper);
77
            },
78
            'cssRequirements' => function() {
79
                return $this->cssRequirements();
80
            },
81
            'purgeAssets' => function() {
82
                $this->purgeAssets();
83
            },
84
        ];
85
    }
86
87
    /**
88
     * Empty the JS assets queue.
89
     *
90
     * @return void
91
     */
92
    public function purgeJs(): void
93
    {
94
        self::$js = '';
95
        self::$jsRequirements = [];
96
    }
97
98
    /**
99
     * Enqueue (concatenate) inline JavaScript content.
100
     *
101
     * Must include `<script>` surrounding element.
102
     *
103
     * @param string       $js     The JavaScript to add.
104
     * @param LambdaHelper $helper For rendering strings in the current context.
105
     * @return void
106
     */
107 View Code Duplication
    public function addJs(string $js, LambdaHelper $helper = null): void
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...
108
    {
109
        if ($helper !== null) {
110
            $js = $helper->render($js);
111
        }
112
        self::$js .= $js;
113
    }
114
115
    /**
116
     * Get the saved inline JavaScript content and purge the store.
117
     *
118
     * @return string
119
     */
120
    public function js(): string
121
    {
122
        $js = self::$js;
123
        self::$js = '';
124
        return $js;
125
    }
126
127
    /**
128
     * Enqueue an external JavaScript file.
129
     *
130
     * Must include `<script>` surrounding element.
131
     *
132
     * @param string       $js     The JavaScript to add.
133
     * @param LambdaHelper $helper For rendering strings in the current context.
134
     * @return void
135
     */
136 View Code Duplication
    public function addJsRequirement(string $js, LambdaHelper $helper = null): void
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...
137
    {
138
        $js  = trim($js);
139
        $key = md5($js);
140
141
        if (!isset(self::$jsRequirements[$key])) {
142
            if ($helper !== null) {
143
                $js = $helper->render($js);
144
            }
145
146
            self::$jsRequirements[$key] = $js;
147
        }
148
    }
149
150
    /**
151
     * Get the JavaScript requirements and purge the store.
152
     *
153
     * @return string
154
     */
155
    public function jsRequirements(): string
156
    {
157
        $req = implode("\n", self::$jsRequirements);
158
        self::$jsRequirements = [];
159
        return $req;
160
    }
161
162
    /**
163
     * Empty the CSS assets queue.
164
     *
165
     * @return void
166
     */
167
    public function purgeCss(): void
168
    {
169
        self::$css = '';
170
        self::$cssRequirements = [];
171
    }
172
173
    /**
174
     * Enqueue (concatenate) inline CSS content.
175
     *
176
     * Must include `<style>` surrounding element.
177
     *
178
     * @param string       $css    The CSS string to add.
179
     * @param LambdaHelper $helper For rendering strings in the current context.
180
     * @return void
181
     */
182 View Code Duplication
    public function addCss(string $css, LambdaHelper $helper = null): void
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...
183
    {
184
        if ($helper !== null) {
185
            $css = $helper->render($css);
186
        }
187
        self::$css .= $css;
188
    }
189
190
    /**
191
     * Get the saved inline CSS content and purge the store.
192
     *
193
     * @return string
194
     */
195
    public function css(): string
196
    {
197
        $css = self::$css;
198
        self::$css = '';
199
        return $css;
200
    }
201
202
    /**
203
     * Enqueue an external CSS file.
204
     *
205
     * Must include `<link />` or surrounding `<style>` element.
206
     *
207
     * @param string       $css    The CSS requirements.
208
     * @param LambdaHelper $helper For rendering strings in the current context.
209
     * @return void
210
     */
211 View Code Duplication
    public function addCssRequirement(string $css, LambdaHelper $helper = null): void
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...
212
    {
213
        $css = trim($css);
214
        $key = md5($css);
215
216
        if (!isset(self::$cssRequirements[$key])) {
217
            if ($helper !== null) {
218
                $css = $helper->render($css);
219
            }
220
221
            self::$cssRequirements[$key] = $css;
222
        }
223
    }
224
225
    /**
226
     * Get the CSS requirements and purge the store.
227
     *
228
     * @return string
229
     */
230
    public function cssRequirements(): string
231
    {
232
        $req = implode("\n", self::$cssRequirements);
233
        self::$cssRequirements = [];
234
        return $req;
235
    }
236
237
    /**
238
     * Empty the all asset queues.
239
     *
240
     * @return void
241
     */
242
    public function purgeAssets(): void
243
    {
244
        $this->purgeJs();
245
        $this->purgeCss();
246
    }
247
}
248