Completed
Pull Request — master (#59)
by
unknown
02:51 queued 01:00
created

JS::export()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 47
Code Lines 27

Duplication

Lines 15
Ratio 31.91 %

Code Coverage

Tests 30
CRAP Score 10.1626

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 47
ccs 30
cts 34
cp 0.8824
rs 5.1578
cc 10
eloc 27
nc 14
nop 0
crap 10.1626

How to fix   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
 * 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 8
    public function export()
21
    {
22 8
        $jsFiles = $this->view->jsFiles;
23
24 8
        $jsPosition = $this->view->jsPosition;
25 8
        $jsOptions = $this->view->jsOptions;
26
27 8
        if (!empty($jsFiles)) {
28 7
            foreach ($jsFiles as $position => $files) {
29 7
                if (false === in_array($position, $jsPosition, true)) {
30 7
                    $this->view->jsFiles[$position] = [];
31
32 7
                    foreach ($files as $file => $html) {
33 7
                        $this->view->jsFiles[$position][$file] = $html;
34 7
                    }
35 7
                } else {
36 7
                    $this->view->jsFiles[$position] = [];
37
38 7
                    $toMinify = [];
39
40 7
                    foreach ($files as $file => $html) {
41 7 View Code Duplication
                        if ($this->thisFileNeedMinify($file, $html)) {
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...
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 7
                            if (!empty($toMinify)) {
49
                                $this->process($position, $jsOptions, $toMinify);
50
51
                                $toMinify = [];
52
                            }
53
54 7
                            $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 8
    }
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
        $resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $this->_getSummaryFilesHash($files));
76
77 7
        if (!file_exists($resultFile)) {
78 7
            $js = '';
79
80 7
            foreach ($files as $file => $html) {
81 7
                $file = $this->getAbsoluteFilePath($file);
82
83 7
                $content = '';
84
85 7
                if (!file_exists($file)) {
86
                    \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
87 7
                } elseif (!is_readable($file)) {
88
                    \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
89
                } else {
90 7
                    $content .= file_get_contents($file) . ';' . "\n";
91
                }
92
93 7
                $js .= $content;
94 7
            }
95
96 7
            if ($this->view->removeComments) {
97 7
                $this->removeJsComments($js);
98 7
            }
99
100 7
            if ($this->view->minifyJs) {
101 7
                $js = (new JSMin($js))
102 7
                    ->min();
103 7
            }
104
105 7
            file_put_contents($resultFile, $js);
106
107 7
            if (false !== $this->view->fileMode) {
108 7
                @chmod($resultFile, $this->view->fileMode);
109 7
            }
110 7
        }
111
112 7
        $file = $this->prepareResultFile($resultFile);
113
114 7
        $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options);
115 7
    }
116
117
    /**
118
     * Remove JS comments
119
     * @param string $code
120
     * @return string Code without comments
121
     */
122 8
    public static function removeJsComments($code)
123
    {
124 8
        return preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $code);
125
    }
126
}
127