Passed
Push — master ( 2cd729...512691 )
by Volodymyr
03:34
created

MinifyJs::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
4
 * @author: <mailto:[email protected]>
5
 * @github: <https://github.com/hryvinskyi>
6
 */
7
8
declare(strict_types=1);
9
10
namespace Hryvinskyi\DeferJs\Model\Minify;
11
12
use JShrink\Minifier;
13
14
/**
15
 * Class MinifyJs
16
 */
17
class MinifyJs implements MinifyJsInterface
18
{
19
    /**
20
     * @param array $scripts
21
     *
22
     * @return array
23
     * @throws \Exception
24
     */
25
    public function execute(array $scripts): array
26
    {
27
        foreach ($scripts as &$script) {
28
            try {
29
                $script = Minifier::minify($script);
30
            } catch (\Exception $exception) {
31
                // Remove comments
32
                $script = preg_replace("/[^:']\/\/.*/", '', $script);
33
            }
34
35
            $script = preg_replace('!\s+!', ' ', $script);
36
        }
37
38
        return $scripts;
39
    }
40
}