Completed
Push — master ( 64fc42...5cbacf )
by Song
02:43
created

MinifyCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 159
Duplicated Lines 36.48 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 58
loc 159
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 30 3
A loadExcepts() 0 8 2
A clearMinifiedFiles() 0 12 1
A minifyCSS() 29 29 4
A minifyJS() 29 29 4
A generateManifest() 0 13 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
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Facades\Admin as AdminFacade;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Str;
9
use MatthiasMullie\Minify;
10
11
class MinifyCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'admin:minify {--clear}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Minify the CSS and JS';
26
27
    /**
28
     * @var array
29
     */
30
    protected $assets = [
31
        'css' => [],
32
        'js'  => [],
33
    ];
34
35
    /**
36
     * @var array
37
     */
38
    protected $excepts = [];
39
40
    /**
41
     * Execute the console command.
42
     */
43
    public function handle()
44
    {
45
        if (!class_exists(Minify\Minify::class)) {
46
            $this->error('To use `admin:minify` command, please install [matthiasmullie/minify] first.');
47
48
            return;
49
        }
50
51
        if ($this->option('clear')) {
52
            return $this->clearMinifiedFiles();
53
        }
54
55
        $this->loadExcepts();
56
57
        AdminFacade::bootstrap();
58
59
        $this->minifyCSS();
60
        $this->minifyJS();
61
62
        $this->generateManifest();
63
64
        $this->comment('JS and CSS are successfully minified:');
65
        $this->line('  '.Admin::$min['js']);
66
        $this->line('  '.Admin::$min['css']);
67
68
        $this->line('');
69
70
        $this->comment('Manifest successfully generated:');
71
        $this->line('  '.Admin::$manifest);
72
    }
73
74
    protected function loadExcepts()
75
    {
76
        $excepts = config('admin.minify_assets.excepts');
77
78
        if (is_array($excepts)) {
79
            $this->excepts = array_filter($excepts);
80
        }
81
    }
82
83
    protected function clearMinifiedFiles()
84
    {
85
        @unlink(public_path(Admin::$manifest));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
86
        @unlink(public_path(Admin::$min['js']));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
        @unlink(public_path(Admin::$min['css']));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
88
89
        $this->comment('Following files are cleared:');
90
91
        $this->line('  '.Admin::$min['js']);
92
        $this->line('  '.Admin::$min['css']);
93
        $this->line('  '.Admin::$manifest);
94
    }
95
96 View Code Duplication
    protected function minifyCSS()
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...
97
    {
98
        $css = collect(array_merge(Admin::$css, Admin::baseCss()))
99
            ->unique()->map(function ($css) {
100
                if (url()->isValidUrl($css)) {
101
                    $this->assets['css'][] = $css;
102
103
                    return;
104
                }
105
106
                if (in_array($css, $this->excepts)) {
107
                    $this->assets['css'][] = $css;
108
109
                    return;
110
                }
111
112
                if (Str::contains($css, '?')) {
113
                    $css = substr($css, 0, strpos($css, '?'));
114
                }
115
116
                return public_path($css);
117
            })->filter();
118
119
        $minifier = new Minify\CSS();
120
121
        $minifier->add(...$css);
122
123
        $minifier->minify(public_path(Admin::$min['css']));
124
    }
125
126 View Code Duplication
    protected function minifyJS()
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...
127
    {
128
        $js = collect(array_merge(Admin::$js, Admin::baseJs()))
129
            ->unique()->map(function ($js) {
130
                if (url()->isValidUrl($js)) {
131
                    $this->assets['js'][] = $js;
132
133
                    return;
134
                }
135
136
                if (in_array($js, $this->excepts)) {
137
                    $this->assets['js'][] = $js;
138
139
                    return;
140
                }
141
142
                if (Str::contains($js, '?')) {
143
                    $js = substr($js, 0, strpos($js, '?'));
144
                }
145
146
                return public_path($js);
147
            })->filter();
148
149
        $minifier = new Minify\JS();
150
151
        $minifier->add(...$js);
152
153
        $minifier->minify(public_path(Admin::$min['js']));
154
    }
155
156
    protected function generateManifest()
157
    {
158
        $min = collect(Admin::$min)->mapWithKeys(function ($path, $type) {
159
            return [$type => sprintf('%s?id=%s', $path, md5(uniqid()))];
160
        });
161
162
        array_unshift($this->assets['css'], $min['css']);
163
        array_unshift($this->assets['js'], $min['js']);
164
165
        $json = json_encode($this->assets, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
166
167
        file_put_contents(public_path(Admin::$manifest), $json);
168
    }
169
}
170