Passed
Pull Request — master (#60)
by
unknown
01:52
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 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 64
ccs 32
cts 47
cp 0.6808
rs 2.9427
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
     * @var array options of minified js files
108
     */
109
    public $jsOptions = [];
110
111
    /**
112
     * @deprecated
113
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
114
     */
115
    public $force_charset = false;
116
117
    /**
118
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
119
     */
120
    public $forceCharset;
121
122
    /**
123
     * @deprecated
124
     * @var bool whether to change @import on content
125
     */
126
    public $expand_imports = true;
127
128
    /**
129
     * @var bool whether to change @import on content
130
     */
131
    public $expandImports;
132
133
    /**
134
     * @deprecated
135
     * @var int
136
     */
137
    public $css_linebreak_pos = 2048;
138
139
    /**
140
     * @var int
141
     */
142
    public $cssLinebreakPos;
143
144
    /**
145
     * @deprecated
146
     * @var int|bool chmod of minified file. If false chmod not set
147
     */
148
    public $file_mode = 0664;
149
150
    /**
151
     * @var int|bool chmod of minified file. If false chmod not set
152
     */
153
    public $fileMode;
154
155
    /**
156
     * @var array schemes that will be ignored during normalization url
157
     */
158
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
159
160
    /**
161
     * @deprecated
162
     * @var bool do I need to compress the result html page.
163
     */
164
    public $compress_output = false;
165
166
    /**
167
     * @deprecated
168
     * @var array options for compressing output result
169
     *   * extra - use more compact algorithm
170
     *   * no-comments - cut all the html comments
171
     */
172
    public $compress_options = ['extra' => true];
173
174
    /**
175
     * @var array options for compressing output result
176
     *   * extra - use more compact algorithm
177
     *   * no-comments - cut all the html comments
178
     */
179
    public $compressOptions;
180
181
    /**
182
     * @var array
183
     */
184
    public $excludeBundles = [];
185
186
    /**
187
     * @var array
188
     */
189
    public $excludeFiles = [];
190
191
    /**
192
     * @throws \rmrevin\yii\minify\Exception
193
     */
194 10
    public function init()
195
    {
196 10
        parent::init();
197
198 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...
199 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...
200 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...
201 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...
202 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...
203 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...
204 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...
205 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...
206 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...
207
208 10
        $excludeBundles = $this->excludeBundles;
209 10
        if (!empty($excludeBundles)) {
210 1
            foreach ($excludeBundles as $bundle) {
211 1
                if (!class_exists($bundle)) {
212
                    continue;
213
                }
214
215
                /** @var AssetBundle $Bundle */
216 1
                $Bundle = new $bundle;
217
218 1
                if (!empty($Bundle->css)) {
219 1
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->css);
220 1
                }
221
222 1
                if (!empty($Bundle->js)) {
223 1
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->js);
224 1
                }
225 1
            }
226 1
        }
227
228 10
        $minify_path = $this->minifyPath = (string)\Yii::getAlias($this->minifyPath);
229 10
        if (!file_exists($minify_path)) {
230 10
            FileHelper::createDirectory($minify_path);
231 10
        }
232
233 10
        if (!is_readable($minify_path)) {
234
            throw new Exception('Directory for compressed assets is not readable.');
235
        }
236
237 10
        if (!is_writable($minify_path)) {
238
            throw new Exception('Directory for compressed assets is not writable.');
239
        }
240
241 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...
242
            \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function (Event $Event) {
243
                /** @var Response $Response */
244
                $Response = $Event->sender;
245
246
                if ($Response->format === Response::FORMAT_HTML) {
247
                    if (!empty($Response->data)) {
248
                        $Response->data = HtmlCompressor::compress($Response->data, $this->compressOptions);
249
                    }
250
251
                    if (!empty($Response->content)) {
252
                        $Response->content = HtmlCompressor::compress($Response->content, $this->compressOptions);
253
                    }
254
                }
255
            });
256
        }
257 10
    }
258
259
    /**
260
     * @inheritdoc
261
     */
262 8
    public function endBody()
263
    {
264 8
        $this->trigger(self::EVENT_END_BODY);
265 8
        echo self::PH_BODY_END;
266
267 8
        foreach (array_keys($this->assetBundles) as $bundle) {
268 8
            $this->registerAssetFiles($bundle);
269 8
        }
270
271 8
        if (true === $this->enableMinify) {
272 8
            (new components\CSS($this))->export();
273 8
            (new components\JS($this))->export();
274 8
        }
275 8
    }
276
}
277