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

Html   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 114
Duplicated Lines 12.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 3
Metric Value
wmc 28
c 6
b 2
f 3
lcom 1
cbo 5
dl 14
loc 114
ccs 72
cts 72
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 11 1
F doClean() 14 99 27

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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