Test Failed
Pull Request — master (#1)
by
unknown
38:28
created

components/JS.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * JS.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
namespace processfast\yii\minify\components;
8
//use werkint\jsmin;
9
use yii\helpers\Html;
10
/**
11
 * Class JS
12
 * @package rmrevin\yii\minify\components
13
 */
14
class JS extends MinifyComponent
15
{
16
    public function export()
17
    {
18
        $jsFiles = $this->view->jsFiles;
19
        $jsPosition = $this->view->jsPosition;
20
        $jsOptions = $this->view->jsOptions;
21
        if (!empty($jsFiles)) {
22
            foreach ($jsFiles as $position => $files) {
23
                if (false === in_array($position, $jsPosition, true)) {
24
                    $this->view->jsFiles[$position] = [];
25
                    foreach ($files as $file => $html) {
26
                        $this->view->jsFiles[$position][$file] = $html;
27
                    }
28
                } else {
29
                    $this->view->jsFiles[$position] = [];
30
                    $toMinify = [];
31
                    foreach ($files as $file => $html) {
32 View Code Duplication
                        if ($this->thisFileNeedMinify($file, $html)) {
33
                            if ($this->view->concatJs) {
34
                                $toMinify[$file] = $html;
35
                            } else {
36
                                $this->process($position, $jsOptions, [$file => $html]);
37
                            }
38
                        } else {
39
                            if (!empty($toMinify)) {
40
                                $this->process($position, $jsOptions, $toMinify);
41
                                $toMinify = [];
42
                            }
43
                            $this->view->jsFiles[$position][$file] = $html;
44
                        }
45
                    }
46
                    if (!empty($toMinify)) {
47
                        $this->process($position, $jsOptions, $toMinify);
48
                    }
49
                    unset($toMinify);
50
                }
51
            }
52
        }
53
    }
54
    /**
55
     * @param integer $position
56
     * @param array $options
57
     * @param array $files
58
     */
59
    protected function process($position, $options, $files)
60
    {
61
        $resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $this->_getSummaryFilesHash($files));
62
        if (!file_exists($resultFile)) {
63
            $js = '';
64
            foreach ($files as $file => $html) {
65
                $file = $this->getAbsoluteFilePath($file);
66
                $content = '';
67
                if (!file_exists($file)) {
68
                    \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
69
                } elseif (!is_readable($file)) {
70
                    \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
71
                } else {
72
                    $content .= file_get_contents($file) . ';' . "\n";
73
                }
74
                $js .= $content;
75
            }
76
            $this->removeJsComments($js);
77
            if ($this->view->minifyJs) {
78
                $js = (new \JSMin($js))
79
                    ->min();
80
            }
81
            file_put_contents($resultFile, $js);
82
            if (false !== $this->view->fileMode) {
83
                @chmod($resultFile, $this->view->fileMode);
84
            }
85
        }
86
        $file = $this->prepareResultFile($resultFile);
87
        $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options);
88
    }
89
    /**
90
     * @todo
91
     * @param string $code
92
     */
93
    protected function removeJsComments(&$code)
0 ignored issues
show
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...
94
    {
95
        if (true === $this->view->removeComments) {
96
            //$code = preg_replace('', '', $code);
97
        }
98
    }
99
}