Completed
Push — master ( 2fd39c...5d8d2a )
by Revin
02:32
created

View::minifyJS()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 41
Code Lines 23

Duplication

Lines 13
Ratio 31.71 %

Code Coverage

Tests 15
CRAP Score 9

Importance

Changes 11
Bugs 2 Features 1
Metric Value
c 11
b 2
f 1
dl 13
loc 41
ccs 15
cts 15
cp 1
rs 4.909
cc 9
eloc 23
nc 2
nop 0
crap 9
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\FileHelper;
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 string filemtime or sha1 */
23
    public $fileCheckAlgorithm = 'filemtime';
24
25
    /** @var bool */
26
    public $minifyCss = true;
27
28
    /** @var bool */
29
    public $minifyJs = true;
30
31
    /** @var bool */
32
    public $removeComments = true;
33
34
    /** @var string path alias to web base (in url) */
35
    public $web_path = '@web';
36
37
    /** @var string path alias to web base (absolute) */
38
    public $base_path = '@webroot';
39
40
    /** @var string path alias to save minify result */
41
    public $minify_path = '@webroot/minify';
42
43
    /** @var array positions of js files to be minified */
44
    public $js_position = [self::POS_END, self::POS_HEAD];
45
46
    /** @var bool|string charset forcibly assign, otherwise will use all of the files found charset */
47
    public $force_charset = false;
48
49
    /** @var bool whether to change @import on content */
50
    public $expand_imports = true;
51
52
    /** @var int */
53
    public $css_linebreak_pos = 2048;
54
55
    /** @var int|bool chmod of minified file. If false chmod not set */
56
    public $file_mode = 0664;
57
58
    /** @var array schemes that will be ignored during normalization url */
59
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
60
61
    /** @var bool do I need to compress the result html page. */
62
    public $compress_output = false;
63
64
    /**
65
     * @var array options for compressing output result
66
     *   * extra - use more compact algorithm
67
     *   * no-comments - cut all the html comments
68
     */
69
    public $compress_options = ['extra' => true];
70
71
    /**
72
     * @throws \rmrevin\yii\minify\Exception
73
     */
74 6
    public function init()
75
    {
76 6
        parent::init();
77
78 6
        $minify_path = $this->minify_path = (string)\Yii::getAlias($this->minify_path);
79 6
        if (!file_exists($minify_path)) {
80 6
            FileHelper::createDirectory($minify_path);
81 6
        }
82
83 6
        if (!is_readable($minify_path)) {
84
            throw new Exception('Directory for compressed assets is not readable.');
85
        }
86
87 6
        if (!is_writable($minify_path)) {
88
            throw new Exception('Directory for compressed assets is not writable.');
89
        }
90
91 6
        if (true === $this->compress_output) {
92
            \Yii::$app->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function (\yii\base\Event $Event) {
93
                /** @var \yii\web\Response $Response */
94
                $Response = $Event->sender;
95
                if ($Response->format === \yii\web\Response::FORMAT_HTML) {
96
                    if (!empty($Response->data)) {
97
                        $Response->data = HtmlCompressor::compress($Response->data, $this->compress_options);
98
                    }
99
100
                    if (!empty($Response->content)) {
101
                        $Response->content = HtmlCompressor::compress($Response->content, $this->compress_options);
0 ignored issues
show
Documentation Bug introduced by
It seems like \rmrevin\yii\minify\Html...this->compress_options) 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...
102
                    }
103
                }
104
            });
105
        }
106 6
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 4
    public function endPage($ajaxMode = false)
112
    {
113 4
        $this->trigger(self::EVENT_END_PAGE);
114
115 4
        $content = ob_get_clean();
116 4
        foreach (array_keys($this->assetBundles) as $bundle) {
117 4
            $this->registerAssetFiles($bundle);
118 4
        }
119
120 4
        if (true === $this->enableMinify) {
121 4
            if (true === $this->minifyCss) {
122 4
                (new components\CSS($this))->minify();
123 4
            }
124
125 4
            if (true === $this->minifyJs) {
126 4
                (new components\JS($this))->minify();
127 4
            }
128 4
        }
129
130 4
        echo strtr(
131 4
            $content,
132
            [
133 4
                self::PH_HEAD => $this->renderHeadHtml(),
134 4
                self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
135 4
                self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
136
            ]
137 4
        );
138
139 4
        $this->clear();
140 4
    }
141
}
142