EmptyAttributeMinifier   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 66
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isBooleanAttribute() 0 4 2
A setRepository() 0 4 1
A provides() 0 4 1
A process() 0 19 4
1
<?php
2
3
namespace ArjanSchouten\HtmlMinifier\Minifiers\Html;
4
5
use ArjanSchouten\HtmlMinifier\Constants;
6
use ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface;
7
use ArjanSchouten\HtmlMinifier\MinifyContext;
8
use ArjanSchouten\HtmlMinifier\Options;
9
use ArjanSchouten\HtmlMinifier\Repositories\HtmlBooleanAttributeRepository;
10
use ArjanSchouten\HtmlMinifier\Util\Html;
11
12
class EmptyAttributeMinifier implements MinifierInterface
13
{
14
    protected $repository;
15
16 7
    public function __construct()
17
    {
18 7
        $this->repository = new HtmlBooleanAttributeRepository();
19 7
    }
20
21
    /**
22
     * Execute the minification rule.
23
     *
24
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
25
     *
26
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
27
     */
28 7
    public function process(MinifyContext $context)
29
    {
30 7
        return $context->setContents(preg_replace_callback(
31
            '/
32 7
                (\s*'.Constants::ATTRIBUTE_NAME_REGEX.'\s*)     # Match the attribute name
33
                =\s*                                            # Match the equal sign with optional whitespaces
34
                (["\'])                                         # Match quotes and capture for backreferencing
35
                \s*                                             # Strange but possible to have a whitespace in an attribute
36
                \2                                              # Backreference to the matched quote
37
                \s*
38
            /x',
39 7
            function ($match) {
40 6
                if ($this->isBooleanAttribute($match[1])) {
41 2
                    return Html::isLastAttribute($match[0]) ? $match[1] : $match[1].' ';
42
                }
43
44 4
                return Html::hasSurroundingAttributes($match[0]) ? ' ' : '';
45 7
            }, $context->getContents()));
46
    }
47
48
    /**
49
     * Check if an attribute is a boolean attribute.
50
     *
51
     * @param string $attribute
52
     *
53
     * @return bool
54
     */
55 6
    private function isBooleanAttribute($attribute)
56
    {
57 6
        return array_search(trim($attribute), $this->repository->getAttributes()) || Html::isDataAttribute($attribute);
58
    }
59
60
    /**
61
     * @param \ArjanSchouten\HtmlMinifier\Minifiers\Html\HtmlBooleanAttributeRepository $repository
62
     */
63 1
    public function setRepository($repository)
64
    {
65 1
        $this->repository = $repository;
66 1
    }
67
68
    /**
69
     * Indicates if minification rules depends on command options.
70
     *
71
     * @return string
72
     */
73 3
    public function provides()
74
    {
75 3
        return Options::EMPTY_ATTRIBUTES;
76
    }
77
}
78