Completed
Push — master ( 371f09...65d63b )
by Revin
03:01
created

CSS   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 220
Duplicated Lines 8.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 35
c 4
b 2
f 0
lcom 1
cbo 4
dl 19
loc 220
ccs 110
cts 115
cp 0.9565
rs 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
C export() 19 34 7
C process() 0 66 10
A removeCssComments() 0 6 2
B expandImports() 0 17 6
A collectCharsets() 0 6 1
A collectImports() 0 6 1
A collectFonts() 0 6 1
A _collect() 0 14 2
B _getImportContent() 0 22 5

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
 * CSS.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
8
namespace rmrevin\yii\minify\components;
9
10
use yii\helpers\Html;
11
12
/**
13
 * Class CSS
14
 * @package rmrevin\yii\minify\components
15
 */
16
class CSS extends MinifyComponent
17
{
18
19
    public function export()
20 4
    {
21
        $cssFiles = $this->view->cssFiles;
22 4
23
        $this->view->cssFiles = [];
24 4
25
        $toMinify = [];
26 4
27 View Code Duplication
        if (!empty($cssFiles)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28 4
            foreach ($cssFiles as $file => $html) {
29 4
                if ($this->thisFileNeedMinify($file, $html)) {
30 4
                    if ($this->view->concatCss) {
31 4
                        $toMinify[$file] = $html;
32
                    } else {
33
                        $this->process([$file => $html]);
34
                    }
35
                } else {
36
                    if (!empty($toMinify)) {
37
                        $this->process($toMinify);
38
39
                        $toMinify = [];
40 4
                    }
41
42 4
                    $this->view->cssFiles[$file] = $html;
43 4
                }
44 4
            }
45
        }
46 4
47 4
        if (!empty($toMinify)) {
48
            $this->process($toMinify);
49
        }
50
51
        unset($toMinify);
52 4
    }
53
54 4
    /**
55
     * @param array $files
56 4
     */
57 4
    protected function process(array $files)
58
    {
59 4
        $resultFile = $this->view->minify_path . '/' . $this->_getSummaryFilesHash($files) . '.css';
60 4
61 4
        if (!file_exists($resultFile)) {
62
            $css = '';
63 4
64
            foreach ($files as $file => $html) {
65 4
                $path = dirname($file);
66
                $file = $this->getAbsoluteFilePath($file);
67 4
68 4
                $content = file_get_contents($file);
69 4
70 4
                $result = [];
71 4
72
                preg_match_all('|url\(([^)]+)\)|is', $content, $m);
73
                if (!empty($m[0])) {
74 4
                    foreach ($m[0] as $k => $v) {
75
                        if (in_array(strpos($m[1][$k], 'data:'), [0, 1], true)) {
76 4
                            continue;
77 4
                        }
78 4
79 4
                        $url = str_replace(['\'', '"'], '', $m[1][$k]);
80
81 4
                        if ($this->isUrl($url)) {
82
                            $result[$m[1][$k]] = $url;
83 4
                        } else {
84 4
                            $result[$m[1][$k]] = $path . '/' . $url;
85
                        }
86 4
                    }
87 4
88
                    $content = strtr($content, $result);
89 4
                }
90
91 4
                $css .= $content;
92
            }
93 4
94 4
            $this->expandImports($css);
95
96 4
            $this->removeCssComments($css);
97 2
98 2
            if ($this->view->minifyCss) {
99 2
                $css = (new \CSSmin())
100
                    ->run($css, $this->view->css_linebreak_pos);
101
            }
102 4
103 4
            if (false !== $this->view->force_charset) {
104
                $charsets = '@charset "' . (string)$this->view->force_charset . '";' . "\n";
105 4
            } else {
106
                $charsets = $this->collectCharsets($css);
107 4
            }
108 4
109 4
            $imports = $this->collectImports($css);
110 4
            $fonts = $this->collectFonts($css);
111
112 4
            file_put_contents($resultFile, $charsets . $imports . $fonts . $css);
113
114 4
            if (false !== $this->view->file_mode) {
115 4
                @chmod($resultFile, $this->view->file_mode);
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...
116
            }
117
        }
118
119
        $file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile));
120 4
121
        $this->view->cssFiles[$file] = Html::cssFile($file);
122 4
    }
123 4
124 4
    /**
125 4
     * @param string $code
126
     */
127
    protected function removeCssComments(&$code)
128
    {
129
        if (true === $this->view->removeComments) {
130 4
            $code = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $code);
131
        }
132 4
    }
133 2
134 2
    /**
135 2
     * @param string $code
136 2
     */
137 2
    protected function expandImports(&$code)
138 2
    {
139 2
        if (true === $this->view->expand_imports) {
140 2
            preg_match_all('|\@import\s([^;]+);|is', str_replace('&amp;', '&', $code), $m);
141 2
            if (!empty($m[0])) {
142 2
                foreach ($m[0] as $k => $v) {
143 2
                    $import_url = $m[1][$k];
144 2
                    if (!empty($import_url)) {
145 2
                        $import_content = $this->_getImportContent($import_url);
146 4
                        if (!empty($import_content)) {
147
                            $code = str_replace($m[0][$k], $import_content, $code);
148
                        }
149
                    }
150
                }
151
            }
152 2
        }
153
    }
154
155 2
    /**
156 2
     * @param string $code
157
     * @return string
158
     */
159
    protected function collectCharsets(&$code)
160
    {
161
        return $this->_collect($code, '|\@charset[^;]+|is', function ($string) {
162
            return $string . ';';
163 4
        });
164
    }
165
166 2
    /**
167 4
     * @param string $code
168
     * @return string
169
     */
170
    protected function collectImports(&$code)
171
    {
172
        return $this->_collect($code, '|\@import[^;]+|is', function ($string) {
173
            return $string . ';';
174
        });
175
    }
176 4
177 2
    /**
178 4
     * @param string $code
179
     * @return string
180
     */
181
    protected function collectFonts(&$code)
182
    {
183
        return $this->_collect($code, '|\@font-face\{[^}]+\}|is', function ($string) {
184
            return $string;
185
        });
186
    }
187 4
188
    /**
189 4
     * @param string $code
190
     * @param string $pattern
191 4
     * @param callable $handler
192 4
     * @return string
193 4
     */
194 4
    protected function _collect(&$code, $pattern, $handler)
195
    {
196 4
        $result = '';
197 4
198
        preg_match_all($pattern, $code, $m);
199 4
        foreach ($m[0] as $string) {
200
            $string = $handler($string);
201
            $code = str_replace($string, '', $code);
202
203
            $result .= $string . PHP_EOL;
204
        }
205
206 2
        return $result;
207
    }
208 2
209
    /**
210 2
     * @param string $url
211 2
     * @return null|string
212
     */
213 2
    protected function _getImportContent($url)
214 2
    {
215 2
        $result = null;
216
217 2
        if ('url(' === mb_substr($url, 0, 4)) {
218 2
            $url = str_replace(['url(\'', 'url("', 'url(', '\')', '")', ')'], '', $url);
219 2
220 2
            if (mb_substr($url, 0, 2) === '//') {
221
                $url = preg_replace('|^//|', 'http://', $url, 1);
222 2
            }
223 2
224 2
            if (!empty($url)) {
225
                if (!in_array(mb_substr($url, 0, 4), ['http', 'ftp:'], true)) {
226 2
                    $url = \Yii::getAlias($this->view->base_path . $url);
227
                }
228
229
                $result = file_get_contents($url);
230
            }
231
        }
232
233
        return $result;
234
    }
235
}