Completed
Push — master ( b839dd...8b4745 )
by Revin
13:10 queued 06:30
created

View::isUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * View.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\minify;
9
10
use yii\helpers;
11
12
/**
13
 * Class View
14
 * @package rmrevin\yii\minify
15
 */
16
class View extends \yii\web\View
17
{
18
19
    /** @var bool */
20
    public $enableMinify = true;
21
22
    /** @var bool */
23
    public $minifyCss = true;
24
25
    /** @var bool */
26
    public $minifyJs = true;
27
28
    /** @var bool */
29
    public $removeComments = true;
30
31
    /** @var string path alias to web base (in url) */
32
    public $web_path = '@web';
33
34
    /** @var string path alias to web base (absolute) */
35
    public $base_path = '@webroot';
36
37
    /** @var string path alias to save minify result */
38
    public $minify_path = '@webroot/minify';
39
40
    /** @var array positions of js files to be minified */
41
    public $js_position = [self::POS_END, self::POS_HEAD];
42
43
    /** @var bool|string charset forcibly assign, otherwise will use all of the files found charset */
44
    public $force_charset = false;
45
46
    /** @var bool whether to change @import on content */
47
    public $expand_imports = true;
48
49
    /** @var int */
50
    public $css_linebreak_pos = 2048;
51
52
    /** @var int|bool chmod of minified file. If false chmod not set */
53
    public $file_mode = 0664;
54
55
    /** @var array schemes that will be ignored during normalization url */
56
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
57
58
    /** @var bool do I need to compress the result html page. */
59
    public $compress_output = false;
60
61
    /**
62
     * @throws \rmrevin\yii\minify\Exception
63
     */
64 6
    public function init()
65
    {
66 6
        parent::init();
67
68 6
        $minify_path = $this->minify_path = (string)\Yii::getAlias($this->minify_path);
69 6
        if (!file_exists($minify_path)) {
70 6
            helpers\FileHelper::createDirectory($minify_path);
71 6
        }
72
73 6
        if (!is_readable($minify_path)) {
74
            throw new Exception('Directory for compressed assets is not readable.');
75
        }
76
77 6
        if (!is_writable($minify_path)) {
78
            throw new Exception('Directory for compressed assets is not writable.');
79
        }
80
81 6
        if (true === $this->compress_output) {
82
            \Yii::$app->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function (\yii\base\Event $Event) {
83
                /** @var \yii\web\Response $Response */
84
                $Response = $Event->sender;
85
                if ($Response->format === \yii\web\Response::FORMAT_HTML) {
86 View Code Duplication
                    if (!empty($Response->data)) {
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...
87
                        $Response->data = HtmlCompressor::compress($Response->data, ['extra' => true]);
88
                    }
89
90 View Code Duplication
                    if (!empty($Response->content)) {
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...
91
                        $Response->content = HtmlCompressor::compress($Response->content, ['extra' => true]);
0 ignored issues
show
Documentation Bug introduced by
It seems like \rmrevin\yii\minify\Html...array('extra' => true)) can also be of type false. However, the property $content is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
92
                    }
93
                }
94
            });
95
        }
96 6
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 4
    public function endPage($ajaxMode = false)
102
    {
103 4
        $this->trigger(self::EVENT_END_PAGE);
104
105 4
        $content = ob_get_clean();
106 4
        foreach (array_keys($this->assetBundles) as $bundle) {
107 4
            $this->registerAssetFiles($bundle);
108 4
        }
109
110 4
        if (true === $this->enableMinify) {
111 4
            if (true === $this->minifyCss) {
112 4
                $this->minifyCSS();
113 3
            }
114
115 3
            if (true === $this->minifyJs) {
116 3
                $this->minifyJS();
117 3
            }
118 3
        }
119
120 3
        echo strtr(
121 3
            $content,
122
            [
123 3
                self::PH_HEAD => $this->renderHeadHtml(),
124 3
                self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
125 3
                self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
126
            ]
127 3
        );
128
129 3
        $this->clear();
130 3
    }
131
132
    /**
133
     * @return self
134
     */
135 4
    protected function minifyCSS()
136
    {
137 4
        if (!empty($this->cssFiles)) {
138 4
            $cssFiles = $this->cssFiles;
139
140 4
            $this->cssFiles = [];
141
142 4
            $toMinify = [];
143
144 4 View Code Duplication
            foreach ($cssFiles as $file => $html) {
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...
145 4
                if ($this->thisFileNeedMinify($file, $html)) {
146 4
                    $toMinify[$file] = $html;
147 4
                } else {
148
                    if (!empty($toMinify)) {
149
                        $this->processMinifyCss($toMinify);
150
151
                        $toMinify = [];
152
                    }
153
154
                    $this->cssFiles[$file] = $html;
155
                }
156 4
            }
157
158 4
            if (!empty($toMinify)) {
159 4
                $this->processMinifyCss($toMinify);
160 3
            }
161
162 3
            unset($toMinify);
163 3
        }
164
165 3
        return $this;
166
    }
167
168
    /**
169
     * @param array $files
170
     */
171 4
    protected function processMinifyCss($files)
172
    {
173 4
        $resultFile = $this->minify_path . '/' . $this->_getSummaryFilesHash($files) . '.css';
174
175 4
        if (!file_exists($resultFile)) {
176 4
            $css = '';
177
178 4
            foreach ($files as $file => $html) {
179 4
                $file = (strpos($file, "?")) ? parse_url($file, PHP_URL_PATH) : $file;
180 4
                $file = str_replace(\Yii::getAlias($this->web_path), '', $file);
181
182 4
                $content = file_get_contents(\Yii::getAlias($this->base_path) . $file);
183
184 4
                preg_match_all('|url\(([^)]+)\)|is', $content, $m);
185 4
                if (!empty($m[0])) {
186 4
                    $path = dirname($file);
187 4
                    $result = [];
188 4
                    foreach ($m[0] as $k => $v) {
189 4
                        if (in_array(strpos($m[1][$k], 'data:'), [0, 1], true)) {
190 4
                            continue;
191
                        }
192 4
                        $url = str_replace(['\'', '"'], '', $m[1][$k]);
193 4
                        if ($this->isUrl($url)) {
194 4
                            $result[$m[1][$k]] = '\'' . $url . '\'';
195 4
                        } else {
196 4
                            $result[$m[1][$k]] = '\'' . \Yii::getAlias($this->web_path) . $path . '/' . $url . '\'';
197
                        }
198 4
                    }
199 4
                    $content = str_replace(array_keys($result), array_values($result), $content);
200 4
                }
201
202 4
                $css .= $content;
203 4
            }
204
205 4
            $this->expandImports($css);
206
207 3
            $this->removeCssComments($css);
208
209 3
            $css = (new \CSSmin())
210 3
                ->run($css, $this->css_linebreak_pos);
211
212 3
            if (false !== $this->force_charset) {
213 1
                $charsets = '@charset "' . (string)$this->force_charset . '";' . "\n";
214 1
            } else {
215 2
                $charsets = $this->collectCharsets($css);
216
            }
217
218 3
            $imports = $this->collectImports($css);
219 3
            $fonts = $this->collectFonts($css);
220
221 3
            file_put_contents($resultFile, $charsets . $imports . $fonts . $css);
222
223 3
            if (false !== $this->file_mode) {
224 3
                @chmod($resultFile, $this->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...
225 3
            }
226 3
        }
227
228 3
        $file = sprintf('%s%s', \Yii::getAlias($this->web_path), str_replace(\Yii::getAlias($this->base_path), '', $resultFile));
229
230 3
        $this->cssFiles[$file] = helpers\Html::cssFile($file);
231 3
    }
232
233
    /**
234
     * @return self
235
     */
236 3
    protected function minifyJS()
237
    {
238 3
        if (!empty($this->jsFiles)) {
239 3
            $jsFiles = $this->jsFiles;
240
241 3
            foreach ($jsFiles as $position => $files) {
242 3
                if (false === in_array($position, $this->js_position, true)) {
243 3
                    $this->jsFiles[$position] = [];
244 3
                    foreach ($files as $file => $html) {
245 3
                        $this->jsFiles[$position][$file] = $html;
246 3
                    }
247 3
                } else {
248 3
                    $this->jsFiles[$position] = [];
249
250 3
                    $toMinify = [];
251
252 3 View Code Duplication
                    foreach ($files as $file => $html) {
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...
253 3
                        if ($this->thisFileNeedMinify($file, $html)) {
254 3
                            $toMinify[$file] = $html;
255 3
                        } else {
256 3
                            if (!empty($toMinify)) {
257
                                $this->processMinifyJs($position, $toMinify);
258
259
                                $toMinify = [];
260
                            }
261
262 3
                            $this->jsFiles[$position][$file] = $html;
263
                        }
264 3
                    }
265
266 3
                    if (!empty($toMinify)) {
267 3
                        $this->processMinifyJs($position, $toMinify);
268 3
                    }
269
270 3
                    unset($toMinify);
271
                }
272 3
            }
273 3
        }
274
275 3
        return $this;
276
    }
277
278
    /**
279
     * @param integer $position
280
     * @param array $files
281
     */
282 3
    protected function processMinifyJs($position, $files)
283
    {
284 3
        $resultFile = sprintf('%s/%s.js', $this->minify_path, $this->_getSummaryFilesHash($files));
285 3
        if (!file_exists($resultFile)) {
286 3
            $js = '';
287 3
            foreach ($files as $file => $html) {
288 3
                $file = (strpos($file, "?")) ? parse_url($file, PHP_URL_PATH) : $file;
289 3
                $file = $this->getAbsoluteFilePath($file);
290 3
                $js .= file_get_contents($file) . ';' . PHP_EOL;
291 3
            }
292
293 3
            $this->removeJsComments($js);
0 ignored issues
show
Unused Code introduced by
The call to the method rmrevin\yii\minify\View::removeJsComments() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
294
295 3
            $compressedJs = (new \JSMin($js))
296 3
                ->min();
297
298 3
            file_put_contents($resultFile, $compressedJs);
299
300 3
            if (false !== $this->file_mode) {
301 3
                @chmod($resultFile, $this->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...
302 3
            }
303 3
        }
304
305 3
        $file = sprintf('%s%s', \Yii::getAlias($this->web_path), str_replace(\Yii::getAlias($this->base_path), '', $resultFile));
306
307 3
        $this->jsFiles[$position][$file] = helpers\Html::jsFile($file);
308 3
    }
309
310
    /**
311
     * @param string $url
312
     * @param boolean $checkSlash
313
     * @return bool
314
     */
315 4
    protected function isUrl($url, $checkSlash = true)
316
    {
317 4
        $regexp = '#^(' . implode('|', $this->schemas) . ')#is';
318 4
        if ($checkSlash) {
319 4
            $regexp = '#^(/|\\\\|' . implode('|', $this->schemas) . ')#is';
320 4
        }
321
322 4
        return (bool)preg_match($regexp, $url);
323
    }
324
325
    /**
326
     * @param string $string
327
     * @return bool
328
     */
329 4
    protected function isContainsConditionalComment($string)
330
    {
331 4
        return strpos($string, '<![endif]-->') !== false;
332
    }
333
334
    /**
335
     * @param string $file
336
     * @param string $html
337
     * @return bool
338
     */
339 4
    protected function thisFileNeedMinify($file, $html)
340
    {
341 4
        return !$this->isUrl($file, false) && !$this->isContainsConditionalComment($html);
342
    }
343
344
    /**
345
     * @param string $code
346
     * @return string
347
     */
348 2
    protected function collectCharsets(&$code)
349
    {
350
        return $this->_collect($code, '|\@charset[^;]+|is', function ($string) {
351 2
            return $string . ';';
352 2
        });
353
    }
354
355
    /**
356
     * @param string $code
357
     * @return string
358
     */
359 3
    protected function collectImports(&$code)
360
    {
361
        return $this->_collect($code, '|\@import[^;]+|is', function ($string) {
362 2
            return $string . ';';
363 3
        });
364
    }
365
366
    /**
367
     * @param string $code
368
     * @return string
369
     */
370
    protected function collectFonts(&$code)
371
    {
372 3
        return $this->_collect($code, '|\@font-face\{[^}]+\}|is', function ($string) {
373 1
            return $string;
374 3
        });
375
    }
376
377
    /**
378
     * @param string $code
379
     */
380 3
    protected function removeCssComments(&$code)
381
    {
382 3
        if (true === $this->removeComments) {
383 3
            $code = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $code);
384 3
        }
385 3
    }
386
387
    /**
388
     * @param string $code
389
     */
390 3
    protected function removeJsComments(&$code)
0 ignored issues
show
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
391
    {
392
        // @todo
393 3
        if (true === $this->removeComments) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
394
            //$code = preg_replace('', '', $code);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
395 3
        }
396 3
    }
397
398
    /**
399
     * @param string $code
400
     */
401 4
    protected function expandImports(&$code)
402
    {
403 4
        if (true === $this->expand_imports) {
404 2
            preg_match_all('|\@import\s([^;]+);|is', str_replace('&amp;', '&', $code), $m);
405 2
            if (!empty($m[0])) {
406 2
                foreach ($m[0] as $k => $v) {
407 2
                    $import_url = $m[1][$k];
408 2
                    if (!empty($import_url)) {
409 2
                        $import_content = $this->_getImportContent($import_url);
410 2
                        if (!empty($import_content)) {
411 2
                            $code = str_replace($m[0][$k], $import_content, $code);
412 2
                        }
413 2
                    }
414 2
                }
415 1
            }
416 1
        }
417 3
    }
418
419
    /**
420
     * @param string $url
421
     * @return null|string
422
     */
423 2
    protected function _getImportContent($url)
424
    {
425 2
        $result = null;
426
427 2
        if ('url(' === helpers\StringHelper::byteSubstr($url, 0, 4)) {
428 2
            $url = str_replace(['url(\'', 'url("', 'url(', '\')', '")', ')'], '', $url);
429
430 2
            if (helpers\StringHelper::byteSubstr($url, 0, 2) === '//') {
431 2
                $url = preg_replace('|^//|', 'http://', $url, 1);
432 2
            }
433
434 2
            if (!empty($url)) {
435 2
                $result = file_get_contents($url);
436 2
            }
437 2
        }
438
439 2
        return $result;
440
    }
441
442
    /**
443
     * @param string $code
444
     * @param string $pattern
445
     * @param callable $handler
446
     * @return string
447
     */
448 3
    protected function _collect(&$code, $pattern, $handler)
449
    {
450 3
        $result = '';
451
452 3
        preg_match_all($pattern, $code, $m);
453 3
        foreach ($m[0] as $string) {
454 3
            $string = $handler($string);
455 3
            $code = str_replace($string, '', $code);
456
457 3
            $result .= $string . PHP_EOL;
458 3
        }
459
460 3
        return $result;
461
    }
462
463
    /**
464
     * @param string $file
465
     * @return string
466
     */
467 4
    protected function getAbsoluteFilePath($file)
468
    {
469 4
        return \Yii::getAlias($this->base_path) . str_replace(\Yii::getAlias($this->web_path), '', $file);
470
    }
471
472
    /**
473
     * @param array $files
474
     * @return string
475
     */
476 4
    protected function _getSummaryFilesHash($files)
477
    {
478 4
        $result = '';
479 4
        foreach ($files as $file => $html) {
480 4
            $path = $this->getAbsoluteFilePath($file);
481
482 4
            if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) {
483 2
                $result .= sha1_file($path);
484 2
            }
485 4
        }
486
487 4
        return sha1($result);
488
    }
489
}