OptionalElementMinifier   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 52
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 4 1
A process() 0 11 2
A removeElement() 0 12 4
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\OptionalElementsRepository;
9
10
class OptionalElementMinifier implements MinifierInterface
11
{
12
    /**
13
     * Execute the minification rule.
14
     *
15
     * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
16
     *
17
     * @return \ArjanSchouten\HtmlMinifier\MinifyContext
18
     */
19 8
    public function process(MinifyContext $context)
20
    {
21 8
        $elements = (new OptionalElementsRepository())->getElements();
22 8
        $contents = $context->getContents();
23
24 8
        foreach ($elements as $element) {
25 8
            $contents = $this->removeElement($element, $contents);
26
        }
27
28 8
        return $context->setContents($contents);
29
    }
30
31
    /**
32
     * Remove an optional element.
33
     *
34
     * @param object $element
35
     * @param string $contents
36
     *
37
     * @return string
38
     */
39 8
    protected function removeElement($element, $contents)
40
    {
41 8
        $newContents = preg_replace('@'.$element->regex.'@xi', '', $contents);
42
43 8
        if ($newContents !== $contents && isset($element->elements)) {
44
            foreach ($element->elements as $element) {
45
                $newContents = $this->removeElement($element, $newContents);
46
            }
47
        }
48
49 8
        return $newContents;
50
    }
51
52
    /**
53
     * Indicates if minification rules depends on command options.
54
     *
55
     * @return string
56
     */
57 3
    public function provides()
58
    {
59 3
        return Options::OPTIONAL_ELEMENTS;
60
    }
61
}
62