Completed
Push — master ( 371f09...65d63b )
by Revin
03:01
created

JS   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 16.48 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 4
dl 15
loc 91
ccs 47
cts 50
cp 0.94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D export() 15 43 10
B process() 0 28 5
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 4
    public function export()
20
    {
21 4
        $jsFiles = $this->view->jsFiles;
22
23 4
        if (!empty($jsFiles)) {
24 4
            foreach ($jsFiles as $position => $files) {
25 4
                if (false === in_array($position, $this->view->js_position, true)) {
26 4
                    $this->view->jsFiles[$position] = [];
27 4
                    foreach ($files as $file => $html) {
28 4
                        $this->view->jsFiles[$position][$file] = $html;
29 4
                    }
30 4
                } else {
31
                    $this->view->jsFiles[$position] = [];
32 4
33
                    $toMinify = [];
34 4
35 4
                    foreach ($files as $file => $html) {
36 4 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...
37 4
                            if ($this->view->concatJs) {
38 4
                                $toMinify[$file] = $html;
39
                            } else {
40
                                $this->process($position, [$file => $html]);
41
                            }
42
                        } else {
43
                            if (!empty($toMinify)) {
44 4
                                $this->process($position, $toMinify);
45
46 4
                                $toMinify = [];
47
                            }
48 4
49 4
                            $this->view->jsFiles[$position][$file] = $html;
50 4
                        }
51
                    }
52 4
53
                    if (!empty($toMinify)) {
54 4
                        $this->process($position, $toMinify);
55 4
                    }
56
57
                    unset($toMinify);
58
                }
59
            }
60
        }
61 4
    }
62
63 4
    /**
64 4
     * @param integer $position
65 4
     * @param array $files
66 4
     */
67 4
    protected function process($position, $files)
68 4
    {
69 4
        $resultFile = sprintf('%s/%s.js', $this->view->minify_path, $this->_getSummaryFilesHash($files));
70
        if (!file_exists($resultFile)) {
71 4
            $js = '';
72
            foreach ($files as $file => $html) {
73 4
                $file = $this->getAbsoluteFilePath($file);
74 4
                $js .= file_get_contents($file) . ';' . PHP_EOL;
75
            }
76 4
77
            $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...
78 4
79 4
            if ($this->view->minifyJs) {
80 4
                $js = (new \JSMin($js))
81 4
                    ->min();
82
            }
83 4
84
            file_put_contents($resultFile, $js);
85 4
86 4
            if (false !== $this->view->file_mode) {
87
                @chmod($resultFile, $this->view->file_mode);
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...
88
            }
89
        }
90
91 4
        $file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile));
92
93
        $this->view->jsFiles[$position][$file] = Html::jsFile($file);
94 4
    }
95
96 4
    /**
97 4
     * @todo
98
     * @param string $code
99
     */
100
    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...
101
    {
102
        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...
103
            //$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...
104
        }
105
    }
106
}