Passed
Pull Request — master (#22)
by Théo
02:53
created

Javascript   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 34
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 7 2
A compactContent() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Compactor;
16
17
use Exception;
18
use JShrink\Minifier;
19
20
/**
21
 * Compacts Javascript files using JShrink.
22
 *
23
 * @author Robert Hafner <[email protected]>
24
 */
25
final class Javascript extends FileExtensionCompactor
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(array $extensions = ['js'])
31
    {
32
        parent::__construct($extensions);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function compactContent(string $contents): string
39
    {
40
        try {
41
            return Minifier::minify($contents);
42
        } catch (Exception $e) {
43
            // Returns unchanged content
44
45
            return $contents;
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function supports(string $file): bool
53
    {
54
        if (!parent::supports($file)) {
55
            return false;
56
        }
57
58
        return '.min.js' !== substr($file, -7);
59
    }
60
}
61