Test Setup Failed
Push — master ( a3536c...4068f0 )
by Revin
03:28
created

View::init()   D

Complexity

Conditions 15
Paths 324

Size

Total Lines 62
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 16.2058

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 62
ccs 33
cts 40
cp 0.825
rs 4.5487
c 5
b 0
f 0
cc 15
eloc 32
nc 324
nop 0
crap 16.2058

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 = 'hash';
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 string path alias to web base (in url)
59
     */
60
    public $webPath = '@web';
61
62
    /**
63
     * @var string path alias to web base (absolute)
64
     */
65
    public $basePath = '@webroot';
66
67
    /**
68
     * @var string path alias to save minify result
69
     */
70
    public $minifyPath = '@webroot/minify';
71
72
    /**
73
     * @var array positions of js files to be minified
74
     */
75
    public $jsPosition = [self::POS_END, self::POS_HEAD];
76
77
    /**
78
     * @var array options of minified js files
79
     */
80
    public $jsOptions = [];
81
82
    /**
83
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
84
     */
85
    public $forceCharset = false;
86
87
    /**
88
     * @var bool whether to change @import on content
89
     */
90
    public $expandImports = true;
91
92
    /**
93
     * @var int|bool chmod of minified file. If false chmod not set
94
     */
95
    public $fileMode = 0664;
96
97
    /**
98
     * @var array schemes that will be ignored during normalization url
99
     */
100
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
101
102
    /**
103
     * @var array options for compressing output result
104
     *
105
     * 'cssMinifier' : (optional) callback function to process content of STYLE
106
     * elements.
107
     *
108
     * 'jsMinifier' : (optional) callback function to process content of SCRIPT
109
     * elements. Note: the type attribute is ignored.
110
     *
111
     * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
112
     * unset, minify will sniff for an XHTML doctype.
113
     */
114
    public $compressOptions = [];
115
116
    /**
117
     * @var array
118
     */
119
    public $excludeBundles = [];
120
121
    /**
122
     * @var array
123
     */
124
    public $excludeFiles = [];
125
126
    /**
127
     * @var array
128
     */
129
    public $hashAlgos = ['md5', 'tiger160,3', 'sha1', 'tiger192,4'];
130
131
    /**
132
     * @var null|string
133
     */
134
    public $currentHashAlgo;
135
136
    /**
137
     * @var \yii\caching\CacheInterface|string|null
138
     */
139
    public $cache;
140
141
    /**
142
     * @throws \rmrevin\yii\minify\Exception
143
     * @throws \yii\base\InvalidConfigException
144
     * @throws \yii\base\InvalidParamException
145
     * @throws \yii\base\Exception
146
     */
147 11
    public function init()
148
    {
149 11
        parent::init();
150
151 11
        $this->basePath = \Yii::getAlias($this->basePath);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->basePath) can also be of type boolean. However, the property $basePath 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...
152 11
        $this->webPath = \Yii::getAlias($this->webPath);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->webPath) can also be of type boolean. However, the property $webPath 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...
153 11
        $this->minifyPath = \Yii::getAlias($this->minifyPath);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->minifyPath) can also be of type boolean. However, the property $minifyPath 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...
154
155 11
        if (null !== $this->cache && is_string($this->cache)) {
156 11
            $this->cache = \Yii::$app->get($this->cache);
157 11
        }
158
159 11
        foreach ($this->excludeBundles as $bundleClass) {
160 1
            if (!class_exists($bundleClass)) {
161
                continue;
162
            }
163
164
            /** @var AssetBundle $Bundle */
165 1
            $Bundle = new $bundleClass;
166
167 1
            if (!empty($Bundle->css)) {
168 1
                $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->css);
169 1
            }
170
171 1
            if (!empty($Bundle->js)) {
172 1
                $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->js);
173 1
            }
174 11
        }
175
176 11
        $hashAlgos = hash_algos();
177
178 11
        foreach ($this->hashAlgos as $alog) {
179 11
            if (!in_array($alog, $hashAlgos, true)) {
180
                continue;
181
            }
182
183 11
            $this->currentHashAlgo = $alog;
184 11
            break;
185 11
        }
186
187 11
        if (null === $this->currentHashAlgo) {
188
            throw new Exception('Unable to determine the hash algorithm.');
189
        }
190
191 11
        $minifyPath = $this->minifyPath;
192
193 11
        if (!file_exists($minifyPath)) {
194 11
            FileHelper::createDirectory($minifyPath);
0 ignored issues
show
Bug introduced by
It seems like $minifyPath defined by $this->minifyPath on line 191 can also be of type boolean; however, yii\helpers\BaseFileHelper::createDirectory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
195 11
        }
196
197 11
        if (!is_readable($minifyPath)) {
198
            throw new Exception('Directory for compressed assets is not readable.');
199
        }
200
201 11
        if (!is_writable($minifyPath)) {
202
            throw new Exception('Directory for compressed assets is not writable.');
203
        }
204
205 11
        if (true === $this->enableMinify && true === $this->minifyOutput) {
206
            \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, [$this, 'compressOutput']);
207
        }
208 11
    }
209
210
    /**
211
     * @param \yii\base\Event $event
212
     * @codeCoverageIgnore
213
     */
214
    public function compressOutput(Event $event)
215
    {
216
        /** @var Response $Response */
217
        $Response = $event->sender;
218
219
        if (Response::FORMAT_HTML !== $Response->format) {
220
            return;
221
        }
222
223
        if (!empty($Response->data)) {
224
            $Response->data = \Minify_HTML::minify($Response->data, $this->compressOptions);
225
        }
226
227
        if (!empty($Response->content)) {
228
            $Response->content = \Minify_HTML::minify($Response->content, $this->compressOptions);
229
        }
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235 9
    public function endBody()
236
    {
237 9
        $this->trigger(self::EVENT_END_BODY);
238
239 9
        echo self::PH_BODY_END;
240
241 9
        foreach (array_keys($this->assetBundles) as $bundle) {
242 9
            $this->registerAssetFiles($bundle);
243 9
        }
244
245 9
        if (true === $this->enableMinify) {
246 9
            (new components\CSS($this))->export();
247 9
            (new components\JS($this))->export();
248 9
        }
249 9
    }
250
}
251