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

MinifyJs   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 3
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
}