Completed
Push — master ( 92395f...ee477a )
by Revin
38s
created

JS   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 109
Duplicated Lines 13.76 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.39%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
C export() 15 47 10
C process() 0 41 7
A removeJsComments() 0 6 2

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 yii\helpers\Html;
11
12
/**
13
 * Class JS
14
 * @package rmrevin\yii\minify\components
15
 */
16
class JS extends MinifyComponent
17
{
18
19 8
    public function export()
20
    {
21 8
        $jsFiles = $this->view->jsFiles;
22
23 8
        $jsPosition = $this->view->jsPosition;
24 8
        $jsOptions = $this->view->jsOptions;
25
26 8
        if (!empty($jsFiles)) {
27 7
            foreach ($jsFiles as $position => $files) {
28 7
                if (false === in_array($position, $jsPosition, true)) {
29 7
                    $this->view->jsFiles[$position] = [];
30
31 7
                    foreach ($files as $file => $html) {
32 7
                        $this->view->jsFiles[$position][$file] = $html;
33 7
                    }
34 7
                } else {
35 7
                    $this->view->jsFiles[$position] = [];
36
37 7
                    $toMinify = [];
38
39 7
                    foreach ($files as $file => $html) {
40 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...
41 7
                            if ($this->view->concatJs) {
42 7
                                $toMinify[$file] = $html;
43 7
                            } else {
44
                                $this->process($position, $jsOptions, [$file => $html]);
45
                            }
46 7
                        } else {
47 7
                            if (!empty($toMinify)) {
48
                                $this->process($position, $jsOptions, $toMinify);
49
50
                                $toMinify = [];
51
                            }
52
53 7
                            $this->view->jsFiles[$position][$file] = $html;
54
                        }
55 7
                    }
56
57 7
                    if (!empty($toMinify)) {
58 7
                        $this->process($position, $jsOptions, $toMinify);
59 7
                    }
60
61 7
                    unset($toMinify);
62
                }
63 7
            }
64 7
        }
65 8
    }
66
67
    /**
68
     * @param integer $position
69
     * @param array $options
70
     * @param array $files
71
     */
72 7
    protected function process($position, $options, $files)
73
    {
74 7
        $resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $this->_getSummaryFilesHash($files));
75
76 7
        if (!file_exists($resultFile)) {
77 7
            $js = '';
78
79 7
            foreach ($files as $file => $html) {
80 7
                $file = $this->getAbsoluteFilePath($file);
81
82 7
                $content = '';
83
84 7
                if (!file_exists($file)) {
85
                    \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
86 7
                } elseif (!is_readable($file)) {
87
                    \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
88
                } else {
89 7
                    $content .= file_get_contents($file) . ';' . "\n";
90
                }
91
92 7
                $js .= $content;
93 7
            }
94
95 7
            $this->removeJsComments($js);
0 ignored issues
show
Unused Code introduced by
The call to the method rmrevin\yii\minify\compo...\JS::removeJsComments() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
96
97 7
            if ($this->view->minifyJs) {
98 7
                $js = (new \JSMin($js))
99 7
                    ->min();
100 7
            }
101
102 7
            file_put_contents($resultFile, $js);
103
104 7
            if (false !== $this->view->fileMode) {
105 7
                @chmod($resultFile, $this->view->fileMode);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
106 7
            }
107 7
        }
108
109 7
        $file = $this->prepareResultFile($resultFile);
110
111 7
        $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options);
112 7
    }
113
114
    /**
115
     * @todo
116
     * @param string $code
117
     */
118 7
    protected function removeJsComments(&$code)
0 ignored issues
show
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120 7
        if (true === $this->view->removeComments) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
121
            //$code = preg_replace('', '', $code);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122 7
        }
123 7
    }
124
}
125