Completed
Push — master ( 3f79e5...1ec99b )
by Kevin
02:42
created

Html::doClean()   F

Complexity

Conditions 27
Paths 676

Size

Total Lines 99
Code Lines 52

Duplication

Lines 14
Ratio 14.14 %

Code Coverage

Tests 66
CRAP Score 27

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 14
loc 99
ccs 66
cts 66
cp 1
rs 2.1969
cc 27
eloc 52
nc 676
nop 1
crap 27

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Tokens\Element;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\Token;
9
use Psr\Log\LoggerInterface;
10
11
class Html extends OpenElement
12
{
13 1
    protected function getAllowedAttributes()
14
    {
15
        $htmlAllowedAttributes = array(
16
            '/^manifest$/i' => Element::ATTR_CS_STRING
17 1
        );
18
19 1
        return array_merge(
20 1
            $htmlAllowedAttributes,
21 1
            parent::getAllowedAttributes()
22 1
        );
23
    }
24
25 16
    protected function doClean(LoggerInterface $logger = null)
26
    {
27
        // HTML element must not have parent elements.
28 16
        if ($this->getParent() !== null) {
29 1
            return false;
30
        }
31
32
        // Only chidlren allowed are HEAD element followed by BODY element.
33 15
        $bodyCount = 0;
34 15
        $headCount = 0;
35 15
        $headIsFirst = false;
36 15
        foreach ($this->children as $key => $child) {
37
            // Ignore comments.
38 15
            if ($child->getType() == Token::COMMENT) {
39 1
                continue;
40
            }
41
42
            // Invalid token.
43 15
            if ($child->getType() != Token::ELEMENT) {
44 1
                if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
45 1
                    $this->removeChild($child);
46 1
                    if ($logger !== null) {
47 1
                        $logger->debug('Removing ' . $child . '. HTML element only allows HEAD and BODY child elements.');
48 1
                    }
49 1
                }
50
51 1
                continue;
52
            }
53
54
            // Check for HEAD and BODY
55 15
            if ($child->getName() == 'head') {
56 15
                $headCount++;
57 15
                if ($bodyCount == 0) {
58 15
                    $headIsFirst = true;
59 15
                }
60
61
                // Remove extraneous HEAD elements.
62 15
                if ($headCount > 1 &&
63 15
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
64 1
                    $this->removeChild($child);
65 1
                    if ($logger !== null) {
66 1
                        $logger->debug('Removing ' . $child . '. Only one HEAD element allowed.');
67 1
                    }
68
69 1
                    continue;
70
                }
71 15 View Code Duplication
            } elseif ($child->getName() == 'body') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 15
                $bodyCount++;
73
74
                // Remove extraneous BODY elements.
75 15
                if ($bodyCount > 1 &&
76 15
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
77 1
                    $this->removeChild($child);
78 1
                    if ($logger !== null) {
79 1
                        $logger->debug('Removing ' . $child . '. Only one BODY element allowed.');
80 1
                    }
81
82 1
                    continue;
83
                }
84 15
            }
85 15
        }
86
87
        // Handle missing HEAD element child.
88 15
        if ($headCount == 0) {
89 1
            if ($logger !== null) {
90 1
                $logger->debug('Missing "head" element added.');
91 1
            }
92
93 1
            $head = new Head($this->configuration, 'head');
94 1
            $this->prependChild($head);
95 1
        }
96
97
        // Handle missing BODY element child.
98 15
        if ($bodyCount == 0) {
99 1
            if ($logger !== null) {
100 1
                $logger->debug('Missing "body" element added.');
101 1
            }
102
103 1
            $body = new Body($this->configuration, 'body');
104 1
            $this->appendChild($body);
105 1
        }
106
107
        // Handle BODY before HEAD.
108 15
        if (!$headIsFirst && $bodyCount > 0 && $headCount > 0) {
109 1
            foreach ($this->children as $key => $child) {
110 1
                if ($child->getType() == Token::ELEMENT && $child->getName() == 'body') {
111 1
                    unset($this->children[$key]);
112 1
                    $this->appendChild($child);
113 1
                    if ($logger !== null) {
114 1
                        $logger->debug('Moved "body" element to end of "html" element children.');
115 1
                    }
116
117 1
                    break;
118
                }
119 1
            }
120 1
        }
121
122 15
        return true;
123
    }
124
}
125