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

JS   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.39%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 4
dl 15
loc 110
ccs 59
cts 66
cp 0.8939
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C export() 15 47 10
C process() 0 43 8
A removeJsComments() 0 4 1

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
 * 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