BooleanAttributeMinifier   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2
rs 10
ccs 13
cts 13
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 26 3
A provides() 0 4 1
1
<?php
2
3
namespace ArjanSchouten\HtmlMinifier\Minifiers\Html;
4
5
use ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface;
6
use ArjanSchouten\HtmlMinifier\MinifyContext;
7
use ArjanSchouten\HtmlMinifier\Options;
8
use ArjanSchouten\HtmlMinifier\Repositories\HtmlBooleanAttributeRepository;
9
10
class BooleanAttributeMinifier implements MinifierInterface
11
{
12
    /**
13
     * Execute the minification rule.
14
     *
15
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
16
     *
17
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
18
     */
19 4
    public function process(MinifyContext $context)
20
    {
21 4
        $booleanAttributes = implode('|', (new HtmlBooleanAttributeRepository())->getAttributes());
22
23 4
        return $context->setContents(preg_replace_callback(
24
            '/
25
                \s                          # first match a whitespace which is an indication if its an attribute
26 4
                ('.$booleanAttributes.')    # match and capture a boolean attribute
27
                \s*
28
                =
29
                \s*
30
                ([\'"])?                    # optional to use a quote
31
                (\1|true|false|([\s>"\']))    # match the boolean attribute name again or true|false
32
                \2?                         # match the quote again
33 4
            /xi', function ($match) {
34 1
                if (isset($match[4])) {
35 1
                    return ' '.$match[1];
36
                }
37
38 1
                if ($match[3] == 'false') {
39 1
                    return '';
40
                }
41
42 1
                return ' '.$match[1];
43 4
            }, $context->getContents()));
44
    }
45
46
    /**
47
     * Indicates if minification rules depends on command options.
48
     *
49
     * @return string
50
     */
51 3
    public function provides()
52
    {
53 3
        return Options::BOOLEAN_ATTRIBUTES;
54
    }
55
}
56