Html::removeInvalidChildren()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

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