Completed
Push — master ( 9fbe45...817727 )
by Kevin
03:24
created

Html::removeInvalidChildren()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 44
Code Lines 25

Duplication

Lines 11
Ratio 25 %

Code Coverage

Tests 27
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 44
ccs 27
cts 27
cp 1
rs 5.3846
cc 8
eloc 25
nc 8
nop 1
crap 8
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
/**
12
 * "html" element
13
 *
14
 * https://html.spec.whatwg.org/multipage/semantics.html#the-html-element
15
 */
16
class Html extends OpenElement
17
{
18 1
    protected function getAllowedAttributes()
19
    {
20
        $htmlAllowedAttributes = array(
21
            '/^manifest$/i' => Element::ATTR_URI
22 1
        );
23
24 1
        return array_merge(
25 1
            $htmlAllowedAttributes,
26 1
            parent::getAllowedAttributes()
27 1
        );
28
    }
29
30 31
    protected function fixSelf(LoggerInterface $logger)
31
    {
32 31
        $bodyCount = 0;
33 31
        $headCount = 0;
34 31
        $headIsFirst = false;
35 31
        foreach ($this->children as $key => $child) {
36
            // Check for HEAD and BODY
37 31
            if ($child instanceof Head) {
38 31
                ++$headCount;
39 31
                if ($bodyCount == 0) {
40 31
                    $headIsFirst = true;
41 31
                }
42 31
            } elseif ($child instanceof Body) {
43 31
                ++$bodyCount;
44 31
            }
45 31
        }
46
47
        // Handle missing HEAD element child.
48 31
        if ($headCount == 0) {
49 2
            $logger->debug('Missing "head" element added.');
50 2
            $head = new Head($this->configuration, 'head');
51 2
            $this->prependChild($head);
52 2
        }
53
54
        // Handle missing BODY element child.
55 31
        if ($bodyCount == 0) {
56 2
            $logger->debug('Missing "body" element added.');
57 2
            $body = new Body($this->configuration, 'body');
58 2
            $this->appendChild($body);
59 2
        }
60
61
        // Handle BODY before HEAD.
62 31
        if (!$headIsFirst && $bodyCount > 0 && $headCount > 0) {
63 1
            foreach ($this->children as $key => $child) {
64 1
                if ($child->getType() == Token::ELEMENT && $child->getName() == 'body') {
65 1
                    $logger->debug('Moved "body" element to end of "html" element children.');
66 1
                    unset($this->children[$key]);
67 1
                    $this->appendChild($child);
68
69 1
                    break;
70
                }
71 1
            }
72 1
        }
73 31
    }
74
75 30
    protected function removeInvalidChildren(LoggerInterface $logger)
76
    {
77 30
        $bodyCount = 0;
78 30
        $headCount = 0;
79 30
        foreach ($this->children as $key => $child) {
80 30
            if ($child->getType() == Token::COMMENT) {
81 1
                continue;
82
            }
83
84
            // Invalid token.
85 30
            if ($child->getType() != Token::ELEMENT) {
86 1
                $logger->debug('Removing ' . $child . '. "html" element only allows "head" and "body" elements children.');
87 1
                $this->removeChild($child);
88
89 1
                continue;
90
            }
91
92
            // Check for HEAD and BODY
93 30
            if ($child instanceof Head) {
94 30
                ++$headCount;
95
96
                // Remove extraneous HEAD elements.
97 30
                if ($headCount > 1) {
98 1
                    $logger->debug('Removing ' . $child . '. Only one "head" element allowed.');
99 1
                    $this->removeChild($child);
100
101 1
                    continue;
102
                }
103 30 View Code Duplication
            } elseif ($child instanceof 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...
104 30
                ++$bodyCount;
105
106
                // Remove extraneous BODY elements.
107 30
                if ($bodyCount > 1) {
108 1
                    $logger->debug('Removing ' . $child . '. Only one BODY element allowed.');
109 1
                    $this->removeChild($child);
110
111 1
                    continue;
112
                }
113 30
            } else {
114 2
                $logger->debug('Removing ' . $child . '. Only "head" or "body" elements are allowed as "html" element children.');
115 2
                $this->removeChild($child);
116
            }
117 30
        }
118 30
    }
119
120 31
    protected function removeInvalidSelf(LoggerInterface $logger)
121
    {
122
        // HTML element must not have parent elements.
123 31
        if ($this->getParent() !== null) {
124 1
            $logger->debug($this . ' must not have a parent element.');
125
126 1
            return true;
127
        }
128
129 30
        return false;
130
    }
131
}
132