Completed
Push — master ( a17ead...35004c )
by Kevin
02:36
created

Html::getAllowedAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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