Completed
Push — master ( 8137b1...e31cbc )
by Revin
05:54 queued 02:12
created

JS::export()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 44
Code Lines 25

Duplication

Lines 15
Ratio 34.09 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 0
Metric Value
dl 15
loc 44
ccs 17
cts 17
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 25
nc 14
nop 0
crap 10

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