Completed
Push — master ( 36b366...8137b1 )
by Revin
02:41
created

View   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 170
Duplicated Lines 5.88 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 66.67%

Importance

Changes 24
Bugs 1 Features 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 10
loc 170
ccs 28
cts 42
cp 0.6667
rs 10
c 24
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D init() 0 34 10
B endBody() 10 22 6

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