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

JS::process()   C

Complexity

Conditions 8
Paths 3

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8.2037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 52
ccs 29
cts 34
cp 0.8529
rs 6.8493
c 1
b 0
f 0
cc 8
eloc 29
nc 3
nop 3
crap 8.2037

How to fix   Long Method   

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
 * JS.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
8
namespace rmrevin\yii\minify\components;
9
10
use JSMin\JSMin;
11
use yii\helpers\Html;
12
13
/**
14
 * Class JS
15
 * @package rmrevin\yii\minify\components
16
 */
17
class JS extends MinifyComponent
18
{
19
20 9
    public function export()
21
    {
22 9
        $jsFiles = $this->view->jsFiles;
23
24 9
        $jsPosition = $this->view->jsPosition;
25 9
        $jsOptions = $this->view->jsOptions;
26
27 9
        if (!empty($jsFiles)) {
28 7
            foreach ($jsFiles as $position => $files) {
29 7
                if (false === in_array($position, $jsPosition, true)) {
30 6
                    $this->view->jsFiles[$position] = [];
31
32 6
                    foreach ($files as $file => $html) {
33 6
                        $this->view->jsFiles[$position][$file] = $html;
34 6
                    }
35 6
                } else {
36 7
                    $this->view->jsFiles[$position] = [];
37
38 7
                    $toMinify = [];
39
40 7
                    foreach ($files as $file => $html) {
41 7
                        if ($this->thisFileNeedMinify($file, $html)) {
42 7
                            if ($this->view->concatJs) {
43 7
                                $toMinify[$file] = $html;
44 7
                            } else {
45
                                $this->process($position, $jsOptions, [$file => $html]);
46
                            }
47 7
                        } else {
48 6
                            if (!empty($toMinify)) {
49
                                $this->process($position, $jsOptions, $toMinify);
50
51
                                $toMinify = [];
52
                            }
53
54 6
                            $this->view->jsFiles[$position][$file] = $html;
55
                        }
56 7
                    }
57
58 7
                    if (!empty($toMinify)) {
59 7
                        $this->process($position, $jsOptions, $toMinify);
60 7
                    }
61
62 7
                    unset($toMinify);
63
                }
64 7
            }
65 7
        }
66 9
    }
67
68
    /**
69
     * @param integer $position
70
     * @param array $options
71
     * @param array $files
72
     */
73 7
    protected function process($position, $options, $files)
74
    {
75 7
        $minifyPath = $this->view->minifyPath;
76 7
        $hash = $this->_getSummaryFilesHash($files);
77
78 7
        $resultFile = $minifyPath . DIRECTORY_SEPARATOR . $hash . '.js';
79
80 7
        if (!file_exists($resultFile)) {
81 7
            $js = '';
82
83 7
            foreach ($files as $file => $html) {
84 7
                $cacheKey = $this->buildCacheKey($file);
85
86 7
                $content = $this->getFromCache($cacheKey);
87
88 7
                if (false !== $content) {
89
                    $js .= $content;
90
                    continue;
91
                }
92
93 7
                $file = $this->getAbsoluteFilePath($file);
94
95 7
                $content = '';
96
97 7
                if (!file_exists($file)) {
98
                    \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
99 7
                } elseif (!is_readable($file)) {
100
                    \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
101
                } else {
102 7
                    $content .= file_get_contents($file) . ';' . "\n";
103
                }
104
105 7
                if ($this->view->minifyJs) {
106 7
                    $content = JSMin::minify($content);
107 7
                }
108
109 7
                $this->saveToCache($cacheKey, $content);
110
111 7
                $js .= $content;
112 7
            }
113
114 7
            file_put_contents($resultFile, $js);
115
116 7
            if (false !== $this->view->fileMode) {
117 7
                @chmod($resultFile, $this->view->fileMode);
118 7
            }
119 7
        }
120
121 7
        $file = $this->prepareResultFile($resultFile);
122
123 7
        $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options);
124 7
    }
125
}
126