Completed
Push — master ( 4a68ab...01a40f )
by Song
03:24
created

HasAssets   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 368
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 4
Dependencies 0

Importance

Changes 0
Metric Value
dl 64
loc 368
rs 9.1199
c 0
b 0
f 0
wmc 41
lcom 4
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A css() 16 16 3
A baseCss() 0 14 2
A js() 16 16 3
A headerJs() 0 8 2
A baseJs() 0 10 2
A getMinifiedCss() 8 8 3
A getMinifiedJs() 8 8 3
A jQuery() 0 4 1
A ignoreMinify() 0 6 2
A script() 0 14 3
A style() 8 8 2
A html() 8 8 2
A getManifestData() 0 13 2
C component() 0 52 11

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like HasAssets often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HasAssets, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Encore\Admin\Traits;
4
5
trait HasAssets
6
{
7
    /**
8
     * @var array
9
     */
10
    public static $script = [];
11
12
    /**
13
     * @var array
14
     */
15
    public static $deferredScript = [];
16
17
    /**
18
     * @var array
19
     */
20
    public static $style = [];
21
22
    /**
23
     * @var array
24
     */
25
    public static $css = [];
26
27
    /**
28
     * @var array
29
     */
30
    public static $js = [];
31
32
    /**
33
     * @var array
34
     */
35
    public static $html = [];
36
37
    /**
38
     * @var array
39
     */
40
    public static $headerJs = [];
41
42
    /**
43
     * @var string
44
     */
45
    public static $manifest = 'vendor/laravel-admin/minify-manifest.json';
46
47
    /**
48
     * @var array
49
     */
50
    public static $manifestData = [];
51
52
    /**
53
     * @var array
54
     */
55
    public static $min = [
56
        'js'  => 'vendor/laravel-admin/laravel-admin.min.js',
57
        'css' => 'vendor/laravel-admin/laravel-admin.min.css',
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    public static $baseCss = [
64
        'vendor/laravel-admin/AdminLTE/bootstrap/css/bootstrap.min.css',
65
        'vendor/laravel-admin/font-awesome/css/font-awesome.min.css',
66
        'vendor/laravel-admin/laravel-admin/laravel-admin.css',
67
        'vendor/laravel-admin/nprogress/nprogress.css',
68
        'vendor/laravel-admin/sweetalert2/dist/sweetalert2.css',
69
        'vendor/laravel-admin/nestable/nestable.css',
70
        'vendor/laravel-admin/toastr/build/toastr.min.css',
71
        'vendor/laravel-admin/bootstrap3-editable/css/bootstrap-editable.css',
72
        'vendor/laravel-admin/google-fonts/fonts.css',
73
        'vendor/laravel-admin/AdminLTE/dist/css/AdminLTE.min.css',
74
    ];
75
76
    /**
77
     * @var array
78
     */
79
    public static $baseJs = [
80
        'vendor/laravel-admin/AdminLTE/bootstrap/js/bootstrap.min.js',
81
        'vendor/laravel-admin/AdminLTE/plugins/slimScroll/jquery.slimscroll.min.js',
82
        'vendor/laravel-admin/AdminLTE/dist/js/app.min.js',
83
        'vendor/laravel-admin/jquery-pjax/jquery.pjax.js',
84
        'vendor/laravel-admin/nprogress/nprogress.js',
85
        'vendor/laravel-admin/nestable/jquery.nestable.js',
86
        'vendor/laravel-admin/toastr/build/toastr.min.js',
87
        'vendor/laravel-admin/bootstrap3-editable/js/bootstrap-editable.min.js',
88
        'vendor/laravel-admin/sweetalert2/dist/sweetalert2.min.js',
89
        'vendor/laravel-admin/laravel-admin/laravel-admin.js',
90
    ];
91
92
    /**
93
     * @var string
94
     */
95
    public static $jQuery = 'vendor/laravel-admin/AdminLTE/plugins/jQuery/jQuery-2.1.4.min.js';
96
97
    /**
98
     * @var array
99
     */
100
    public static $minifyIgnores = [];
101
102
    /**
103
     * Add css or get all css.
104
     *
105
     * @param null $css
106
     * @param bool $minify
107
     *
108
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
109
     */
110 View Code Duplication
    public static function css($css = null, $minify = true)
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...
111
    {
112
        static::ignoreMinify($css, $minify);
113
114
        if (!is_null($css)) {
115
            return self::$css = array_merge(self::$css, (array) $css);
116
        }
117
118
        if (!$css = static::getMinifiedCss()) {
119
            $css = array_merge(static::$css, static::baseCss());
120
        }
121
122
        $css = array_filter(array_unique($css));
123
124
        return view('admin::partials.css', compact('css'));
125
    }
126
127
    /**
128
     * @param null $css
129
     * @param bool $minify
130
     *
131
     * @return array|null
132
     */
133
    public static function baseCss($css = null, $minify = true)
134
    {
135
        static::ignoreMinify($css, $minify);
136
137
        if (!is_null($css)) {
138
            return static::$baseCss = $css;
139
        }
140
141
        $skin = config('admin.skin', 'skin-blue-light');
142
143
        array_unshift(static::$baseCss, "vendor/laravel-admin/AdminLTE/dist/css/skins/{$skin}.min.css");
144
145
        return static::$baseCss;
146
    }
147
148
    /**
149
     * Add js or get all js.
150
     *
151
     * @param null $js
152
     * @param bool $minify
153
     *
154
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
155
     */
156 View Code Duplication
    public static function js($js = null, $minify = true)
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...
157
    {
158
        static::ignoreMinify($js, $minify);
159
160
        if (!is_null($js)) {
161
            return self::$js = array_merge(self::$js, (array) $js);
162
        }
163
164
        if (!$js = static::getMinifiedJs()) {
165
            $js = array_merge(static::baseJs(), static::$js);
166
        }
167
168
        $js = array_filter(array_unique($js));
169
170
        return view('admin::partials.js', compact('js'));
171
    }
172
173
    /**
174
     * Add js or get all js.
175
     *
176
     * @param null $js
177
     *
178
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
179
     */
180
    public static function headerJs($js = null)
181
    {
182
        if (!is_null($js)) {
183
            return self::$headerJs = array_merge(self::$headerJs, (array) $js);
184
        }
185
186
        return view('admin::partials.js', ['js' => array_unique(static::$headerJs)]);
187
    }
188
189
    /**
190
     * @param null $js
191
     * @param bool $minify
192
     *
193
     * @return array|null
194
     */
195
    public static function baseJs($js = null, $minify = true)
196
    {
197
        static::ignoreMinify($js, $minify);
198
199
        if (!is_null($js)) {
200
            return static::$baseJs = $js;
201
        }
202
203
        return static::$baseJs;
204
    }
205
206
    /**
207
     * @param string $assets
208
     * @param bool   $ignore
209
     */
210
    public static function ignoreMinify($assets, $ignore = true)
211
    {
212
        if (!$ignore) {
213
            static::$minifyIgnores[] = $assets;
214
        }
215
    }
216
217
    /**
218
     * @param string $script
219
     * @param bool   $deferred
220
     *
221
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
222
     */
223
    public static function script($script = '', $deferred = false)
224
    {
225
        if (!empty($script)) {
226
            if ($deferred) {
227
                return self::$deferredScript = array_merge(self::$deferredScript, (array) $script);
228
            }
229
230
            return self::$script = array_merge(self::$script, (array) $script);
231
        }
232
233
        $script = array_unique(array_merge(static::$script, static::$deferredScript));
234
235
        return view('admin::partials.script', compact('script'));
236
    }
237
238
    /**
239
     * @param string $style
240
     *
241
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
242
     */
243 View Code Duplication
    public static function style($style = '')
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...
244
    {
245
        if (!empty($style)) {
246
            return self::$style = array_merge(self::$style, (array) $style);
247
        }
248
249
        return view('admin::partials.style', ['style' => array_unique(self::$style)]);
250
    }
251
252
    /**
253
     * @param string $html
254
     *
255
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
256
     */
257 View Code Duplication
    public static function html($html = '')
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...
258
    {
259
        if (!empty($html)) {
260
            return self::$html = array_merge(self::$html, (array) $html);
261
        }
262
263
        return view('admin::partials.html', ['html' => array_unique(self::$html)]);
264
    }
265
266
    /**
267
     * @param string $key
268
     *
269
     * @return mixed
270
     */
271
    protected static function getManifestData($key)
272
    {
273
        if (!empty(static::$manifestData)) {
274
            return static::$manifestData[$key];
275
        }
276
277
        static::$manifestData = json_decode(
278
            file_get_contents(public_path(static::$manifest)),
279
            true
280
        );
281
282
        return static::$manifestData[$key];
283
    }
284
285
    /**
286
     * @return bool|mixed
287
     */
288 View Code Duplication
    protected static function getMinifiedCss()
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...
289
    {
290
        if (!config('admin.minify_assets') || !file_exists(public_path(static::$manifest))) {
291
            return false;
292
        }
293
294
        return static::getManifestData('css');
295
    }
296
297
    /**
298
     * @return bool|mixed
299
     */
300 View Code Duplication
    protected static function getMinifiedJs()
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...
301
    {
302
        if (!config('admin.minify_assets') || !file_exists(public_path(static::$manifest))) {
303
            return false;
304
        }
305
306
        return static::getManifestData('js');
307
    }
308
309
    /**
310
     * @return string
311
     */
312
    public function jQuery()
313
    {
314
        return admin_asset(static::$jQuery);
315
    }
316
317
    /**
318
     * @param $component
319
     */
320
    public static function component($component, $data = [])
321
    {
322
        $str = view($component, $data)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
323
324
        $dom = new \DOMDocument();
325
326
        libxml_use_internal_errors(true);
327
328
        $dom->loadHTML('<?xml encoding="utf-8" ?>' . $str);
329
330
        libxml_use_internal_errors(false);
331
332
        if ($dom->getElementsByTagName('body')->length <= 0) {
333
            return;
334
        }
335
336
        $body = $dom->getElementsByTagName('body')[0];
337
338
        $render = '';
339
340
        foreach ($body->childNodes as $child) {
341
342
            if ($child instanceof \DOMElement && $child->tagName == 'style') {
343
                static::style($child->nodeValue);
344
                continue;
345
            }
346
347
            if ($child instanceof \DOMElement && $child->tagName == 'script') {
348
                static::script(';(function () {' . $child->nodeValue . '})();');
349
                continue;
350
            }
351
352
            if ($child instanceof \DOMElement && $child->tagName == 'template') {
353
354
                $html = '';
355
356
                foreach ($child->childNodes as $childNode) {
357
                    $html .= $child->ownerDocument->saveHTML($childNode);
358
                }
359
360
                if ($html) {
361
                    static::html($html);
362
                }
363
364
                continue;
365
            }
366
367
            $render .= $body->ownerDocument->saveHTML($child);
368
        }
369
370
        return $render;
371
    }
372
}
373