Completed
Push — master ( e31cbc...a61c01 )
by Revin
02:14
created

View::init()   F

Complexity

Conditions 24
Paths 8192

Size

Total Lines 64
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 42.7155

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 64
ccs 32
cts 47
cp 0.6808
rs 2.9427
c 4
b 0
f 0
cc 24
eloc 36
nc 8192
nop 0
crap 42.7155

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\base\Event;
11
use yii\helpers\FileHelper;
12
use yii\web\AssetBundle;
13
use yii\web\Response;
14
15
/**
16
 * Class View
17
 * @package rmrevin\yii\minify
18
 */
19
class View extends \yii\web\View
20
{
21
22
    /**
23
     * @var bool
24
     */
25
    public $enableMinify = true;
26
27
    /**
28
     * @var string filemtime or sha1
29
     */
30
    public $fileCheckAlgorithm = 'sha1';
31
32
    /**
33
     * @var bool
34
     */
35
    public $concatCss = true;
36
37
    /**
38
     * @var bool
39
     */
40
    public $minifyCss = true;
41
42
    /**
43
     * @var bool
44
     */
45
    public $concatJs = true;
46
47
    /**
48
     * @var bool
49
     */
50
    public $minifyJs = true;
51
52
    /**
53
     * @var bool
54
     */
55
    public $minifyOutput = false;
56
57
    /**
58
     * @var bool
59
     */
60
    public $removeComments = true;
61
62
    /**
63
     * @deprecated
64
     * @var string path alias to web base (in url)
65
     */
66
    public $web_path = '@web';
67
68
    /**
69
     * @var string path alias to web base (in url)
70
     */
71
    public $webPath;
72
73
    /**
74
     * @deprecated
75
     * @var string path alias to web base (absolute)
76
     */
77
    public $base_path = '@webroot';
78
79
    /**
80
     * @var string path alias to web base (absolute)
81
     */
82
    public $basePath;
83
84
    /**
85
     * @deprecated
86
     * @var string path alias to save minify result
87
     */
88
    public $minify_path = '@webroot/minify';
89
90
    /**
91
     * @var string path alias to save minify result
92
     */
93
    public $minifyPath;
94
95
    /**
96
     * @deprecated
97
     * @var array positions of js files to be minified
98
     */
99
    public $js_position = [self::POS_END, self::POS_HEAD];
100
101
    /**
102
     * @var array positions of js files to be minified
103
     */
104
    public $jsPosition;
105
106
    /**
107
     * @deprecated
108
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
109
     */
110
    public $force_charset = false;
111
112
    /**
113
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
114
     */
115
    public $forceCharset;
116
117
    /**
118
     * @deprecated
119
     * @var bool whether to change @import on content
120
     */
121
    public $expand_imports = true;
122
123
    /**
124
     * @var bool whether to change @import on content
125
     */
126
    public $expandImports;
127
128
    /**
129
     * @deprecated
130
     * @var int
131
     */
132
    public $css_linebreak_pos = 2048;
133
134
    /**
135
     * @var int
136
     */
137
    public $cssLinebreakPos;
138
139
    /**
140
     * @deprecated
141
     * @var int|bool chmod of minified file. If false chmod not set
142
     */
143
    public $file_mode = 0664;
144
145
    /**
146
     * @var int|bool chmod of minified file. If false chmod not set
147
     */
148
    public $fileMode;
149
150
    /**
151
     * @var array schemes that will be ignored during normalization url
152
     */
153
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
154
155
    /**
156
     * @deprecated
157
     * @var bool do I need to compress the result html page.
158
     */
159
    public $compress_output = false;
160
161
    /**
162
     * @deprecated
163
     * @var array options for compressing output result
164
     *   * extra - use more compact algorithm
165
     *   * no-comments - cut all the html comments
166
     */
167
    public $compress_options = ['extra' => true];
168
169
    /**
170
     * @var array options for compressing output result
171
     *   * extra - use more compact algorithm
172
     *   * no-comments - cut all the html comments
173
     */
174
    public $compressOptions;
175
176
    /**
177
     * @var array
178
     */
179
    public $excludeBundles = [];
180
181
    /**
182
     * @var array
183
     */
184
    public $excludeFiles = [];
185
186
    /**
187
     * @throws \rmrevin\yii\minify\Exception
188
     */
189 10
    public function init()
190
    {
191 10
        parent::init();
192
193 10
        $this->webPath = empty($this->webPath) ? $this->web_path : $this->webPath;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$web_path has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
194 10
        $this->basePath = empty($this->basePath) ? $this->base_path : $this->basePath;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$base_path has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
195 10
        $this->minifyPath = empty($this->minifyPath) ? $this->minify_path : $this->minifyPath;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$minify_path has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
196 10
        $this->jsPosition = empty($this->jsPosition) ? $this->js_position : $this->jsPosition;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$js_position has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
197 10
        $this->forceCharset = empty($this->forceCharset) ? $this->force_charset : $this->forceCharset;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$force_charset has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
198 10
        $this->expandImports = empty($this->expandImports) ? $this->expand_imports : $this->expandImports;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$expand_imports has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
199 10
        $this->cssLinebreakPos = empty($this->cssLinebreakPos) ? $this->css_linebreak_pos : $this->cssLinebreakPos;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$css_linebreak_pos has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
200 10
        $this->fileMode = empty($this->fileMode) ? $this->file_mode : $this->fileMode;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$file_mode has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
201 10
        $this->compressOptions = empty($this->compressOptions) ? $this->compress_options : $this->compressOptions;
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$compress_options has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
202
203 10
        $excludeBundles = $this->excludeBundles;
204 10
        if (!empty($excludeBundles)) {
205 1
            foreach ($excludeBundles as $bundle) {
206 1
                if (!class_exists($bundle)) {
207
                    continue;
208
                }
209
210
                /** @var AssetBundle $Bundle */
211 1
                $Bundle = new $bundle;
212
213 1
                if (!empty($Bundle->css)) {
214 1
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->css);
215 1
                }
216
217 1
                if (!empty($Bundle->js)) {
218 1
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->js);
219 1
                }
220 1
            }
221 1
        }
222
223 10
        $minify_path = $this->minifyPath = (string)\Yii::getAlias($this->minifyPath);
224 10
        if (!file_exists($minify_path)) {
225 10
            FileHelper::createDirectory($minify_path);
226 10
        }
227
228 10
        if (!is_readable($minify_path)) {
229
            throw new Exception('Directory for compressed assets is not readable.');
230
        }
231
232 10
        if (!is_writable($minify_path)) {
233
            throw new Exception('Directory for compressed assets is not writable.');
234
        }
235
236 10
        if (true === $this->enableMinify && (true === $this->minifyOutput || true === $this->compress_output)) {
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\minify\View::$compress_output has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
237
            \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function (Event $Event) {
238
                /** @var Response $Response */
239
                $Response = $Event->sender;
240
241
                if ($Response->format === Response::FORMAT_HTML) {
242
                    if (!empty($Response->data)) {
243
                        $Response->data = HtmlCompressor::compress($Response->data, $this->compressOptions);
244
                    }
245
246
                    if (!empty($Response->content)) {
247
                        $Response->content = HtmlCompressor::compress($Response->content, $this->compressOptions);
248
                    }
249
                }
250
            });
251
        }
252 10
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257 8
    public function endBody()
258
    {
259 8
        $this->trigger(self::EVENT_END_BODY);
260 8
        echo self::PH_BODY_END;
261
262 8
        foreach (array_keys($this->assetBundles) as $bundle) {
263 8
            $this->registerAssetFiles($bundle);
264 8
        }
265
266 8
        if (true === $this->enableMinify) {
267 8
            (new components\CSS($this))->export();
268 8
            (new components\JS($this))->export();
269 8
        }
270 8
    }
271
}
272