Passed
Push — main ( 880ab4...c427fc )
by Thierry
01:41
created

FileMinifier::minifyCssFile()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 9.9666
1
<?php
2
3
/**
4
 * FileMinifier.php
5
 *
6
 * Minify javascript or css code.
7
 *
8
 * @package jaxon-utils
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Utils\File;
16
17
use JShrink\Minifier as JsMinifier;
18
use MatthiasMullie\Minify\CSS as CssMinifier;
19
use Exception;
20
21
use function file_get_contents;
22
use function file_put_contents;
23
use function is_file;
24
use function trim;
25
26
class FileMinifier
27
{
28
    /**
29
     * @var CssMinifier
30
     */
31
    private $xCssMinifier = null;
32
33
    /**
34
     * @return CssMinifier
35
     */
36
    private function css(): CssMinifier
37
    {
38
        return $this->xCssMinifier ?? $this->xCssMinifier = new CssMinifier();
39
    }
40
41
    /**
42
     * Minify javascript or css code
43
     *
44
     * @param string $sCode The javascript or css code to be minified
45
     *
46
     * @return string|false
47
     */
48
    public function minifyJsCode(string $sCode): string|false
49
    {
50
        try
51
        {
52
            $sMinCode = trim(JsMinifier::minify($sCode));
0 ignored issues
show
Bug introduced by
JShrink\Minifier::minify($sCode) of type boolean is incompatible with the type string expected by parameter $string of trim(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            $sMinCode = trim(/** @scrutinizer ignore-type */ JsMinifier::minify($sCode));
Loading history...
53
            return $sMinCode === '' ? false : $sMinCode;
54
        }
55
        catch(Exception $e)
56
        {
57
            return false;
58
        }
59
    }
60
61
    /**
62
     * Minify javascript or css code
63
     *
64
     * @param string $sCode The javascript or css code to be minified
65
     *
66
     * @return string|false
67
     */
68
    public function minifyCssCode(string $sCode): string|false
69
    {
70
        try
71
        {
72
            $sMinCode = trim($this->css()->add($sCode)->minify());
73
            return $sMinCode === '' ? false : $sMinCode;
74
        }
75
        catch(Exception $e)
76
        {
77
            return false;
78
        }
79
    }
80
81
    /**
82
     * Minify javascript file
83
     *
84
     * @param string $sFile The javascript file to be minified
85
     * @param string $sMinFile The minified javascript file
86
     *
87
     * @return bool
88
     */
89
    public function minifyJsFile(string $sFile, string $sMinFile): bool
90
    {
91
        try
92
        {
93
            $sCode = file_get_contents($sFile);
94
            $sMinCode = trim(JsMinifier::minify($sCode));
0 ignored issues
show
Bug introduced by
JShrink\Minifier::minify($sCode) of type boolean is incompatible with the type string expected by parameter $string of trim(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            $sMinCode = trim(/** @scrutinizer ignore-type */ JsMinifier::minify($sCode));
Loading history...
95
            if($sMinCode === '')
96
            {
97
                return false;
98
            }
99
100
            file_put_contents($sMinFile, $sMinCode);
101
            return is_file($sMinFile);
102
        }
103
        catch(Exception $e)
104
        {
105
            return false;
106
        }
107
    }
108
109
    /**
110
     * Minify css file
111
     *
112
     * @param string $sFile The css file to be minified
113
     * @param string $sMinFile The minified css file
114
     *
115
     * @return bool
116
     */
117
    public function minifyCssFile(string $sFile, string $sMinFile): bool
118
    {
119
        try
120
        {
121
            $sCode = file_get_contents($sFile);
122
            $sMinCode = trim($this->css()->add($sCode)->minify());
123
            if($sMinCode === '')
124
            {
125
                return false;
126
            }
127
128
            file_put_contents($sMinFile, $sMinCode);
129
            return is_file($sMinFile);
130
        }
131
        catch(Exception $e)
132
        {
133
            return false;
134
        }
135
    }
136
137
    /**
138
     * Minify javascript file
139
     *
140
     * @param string $sFile The javascript file to be minified
141
     * @param string $sMinFile The minified javascript file
142
     *
143
     * @return bool
144
     */
145
    public function minify(string $sFile, string $sMinFile): bool
146
    {
147
        return $this->minifyJsFile($sFile, $sMinFile);
148
    }
149
}
150