MinifyService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 5
b 0
f 0
dl 0
loc 85
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A jsMin() 0 7 2
A cssMin() 0 7 2
A init() 0 9 4
A minify() 0 11 2
A htmlMin() 0 7 2
1
<?php
2
/**
3
 * Minify plugin for Craft CMS
4
 *
5
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
6
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @license   MIT License https://opensource.org/licenses/MIT
8
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
9
10
namespace nystudio107\minify\services;
11
12
use Craft;
13
use craft\base\Component;
14
use JSMin\JSMin;
15
use Minify_CSSmin;
16
use Minify_HTML;
17
use nystudio107\minify\Minify;
18
19
/**
20
 * Minify service
21
 *
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   Minify
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
class MinifyService extends Component
27
{
28
    private bool $shouldMinify = true;
0 ignored issues
show
Coding Style introduced by
Private member variable "shouldMinify" must be prefixed with an underscore
Loading history...
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @inheritdoc
32
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
33
    public function init(): void
34
    {
35
        if (Minify::$plugin->getSettings()->disableTemplateMinifying) {
36
            $this->shouldMinify = false;
37
        }
38
39
        if (Craft::$app->getConfig()->getGeneral()->devMode
40
            && Minify::$plugin->getSettings()->disableDevModeMinifying) {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
41
            $this->shouldMinify = false;
42
        }
43
    }
44
45
    /**
46
     * Minify all the things
47
     *
48
     * @param string $htmlText
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     *
50
     * @return string
51
     */
52
    public function minify(string $htmlText = ""): string
53
    {
54
        if ($this->shouldMinify) {
55
            $options = [
56
                'cssMinifier' => '\Minify_CSSmin::minify',
57
                'jsMinifier' => '\JSMin\JSMin::minify',
58
            ];
59
            $htmlText = Minify_HTML::minify($htmlText, $options);
60
        }
61
62
        return $htmlText;
63
    }
64
65
    /**
66
     * Minify the passed in HTML
67
     *
68
     * @param string $htmlText
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     *
70
     * @return string
71
     */
72
    public function htmlMin(string $htmlText = ""): string
73
    {
74
        if ($this->shouldMinify) {
75
            $htmlText = Minify_HTML::minify($htmlText);
76
        }
77
78
        return $htmlText;
79
    }
80
81
    /**
82
     * Minify the passed in CSS
83
     *
84
     * @param string $cssText
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     *
86
     * @return string
87
     */
88
    public function cssMin(string $cssText = ""): string
89
    {
90
        if ($this->shouldMinify) {
91
            $cssText = Minify_CSSmin::minify($cssText);
92
        }
93
94
        return $cssText;
95
    }
96
97
    /**
98
     * Minify the passed in JS
99
     *
100
     * @param string $jsText
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     *
102
     * @return string
103
     */
104
    public function jsMin(string $jsText = ""): string
105
    {
106
        if ($this->shouldMinify) {
107
            $jsText = JSMin::minify($jsText);
108
        }
109
110
        return $jsText;
111
    }
112
}
113