Completed
Push — master ( 63fdc4...c54d38 )
by Revin
03:07
created

View   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 163
Duplicated Lines 6.13 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 66.67%

Importance

Changes 49
Bugs 11 Features 8
Metric Value
wmc 14
c 49
b 11
f 8
lcom 2
cbo 9
dl 10
loc 163
ccs 28
cts 42
cp 0.6667
rs 10

2 Methods

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