Completed
Push — master ( 6e9edd...3f79e5 )
by Kevin
02:25
created

Html::clean()   F

Complexity

Conditions 50
Paths 11614

Size

Total Lines 172
Code Lines 92

Duplication

Lines 86
Ratio 50 %

Code Coverage

Tests 109
CRAP Score 50.8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 86
loc 172
ccs 109
cts 117
cp 0.9316
rs 2
cc 50
eloc 92
nc 11614
nop 1
crap 50.8

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 Psr\Log\LoggerInterface;
7
8
class Html extends OpenElement
9
{
10
    protected function getAllowedAttributes()
11
    {
12
        $htmlAllowedAttributes = array(
13
            '/^manifest$/i' => Element::ATTR_CS_STRING
14
        );
15
16
        return array_merge(
17
            $htmlAllowedAttributes,
18
            parent::getAllowedAttributes()
19
        );
20
    }
21
22
    /**
23
     * Required by the Cleanable interface.
24
     */
25 14
    public function clean(LoggerInterface $logger = null)
26
    {
27 14
        if ($this->configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
28
            return true;
29
        }
30
31 14
        parent::clean($logger);
32
33 14
        if ($this->getParent() !== null) {
34
            return false;
35
        }
36
37
        // Contents: HEAD element followed by BODY element.
38 14
        $bodyCount = 0;
39 14
        $headCount = 0;
40 14
        $headIsFirst = false;
41 14
        foreach ($this->children as $key => $child) {
42
            // Ignore comments.
43 14
            if ($child->getType() == 'comment') {
44 2
                continue;
45
            }
46
47
            // Check for HEAD and BODY
48 14
            if ($child->getType() == 'element') {
49 13
                if ($child->getName() == 'head') {
50 11
                    $headCount++;
51 11
                    if ($bodyCount == 0) {
52 9
                        $headIsFirst = true;
53 9
                    }
54 13
                } elseif ($child->getName() == 'body') {
55 11
                    $bodyCount++;
56 11
                }
57 13
            } else {
58
                // Invalid token.
59 2
                if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
60
                    throw new ValidationException('Token (' . $child->getType() . ') should not be child of HTML element.');
61
                }
62
63 2
                if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
64 1
                    return false;
65
                }
66
67 1
                if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
68 1
                    unset($this->children[$key]);
69 1
                    if ($logger !== null) {
70 1
                        $logger->debug('Removing invalid token (' . $child->getType() . '). It should not be child of HTML element.');
71 1
                    }
72 1
                }
73
            }
74 13
        }
75
76
        // Handle missing HEAD element child.
77 13 View Code Duplication
        if ($headCount == 0) {
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...
78 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
79
                throw new ValidationException('HTML element is missing HEAD element as first child.');
80
            }
81
82 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
83 1
                return false;
84
            }
85
86 1
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
87 1
                $head = new Head($this->configuration, 'head');
88 1
                $this->prependChild($head);
89 1
                if ($logger !== null) {
90 1
                    $logger->debug('Missing HEAD element added.');
91 1
                }
92 1
            }
93 1
        }
94
95
        // Handle missing BODY element child.
96 12 View Code Duplication
        if ($bodyCount == 0) {
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...
97 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
98
                throw new ValidationException('HTML element is missing BODY element.');
99
            }
100
101 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
102 1
                return false;
103
            }
104
105 1
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
106 1
                $body = new Body($this->configuration, 'body');
107 1
                $this->appendChild($body);
108 1
                if ($logger !== null) {
109 1
                    $logger->debug('Missing BODY element added.');
110 1
                }
111 1
            }
112 1
        }
113
114
        // Handle multiple HEAD elements.
115 11 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...
116 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
117
                throw new ValidationException('HTML element can only have 1 HEAD element child.');
118
            }
119
120 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
121 1
                return false;
122
            }
123
124 1
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
125
                // Remove extraneous HEAD elements.
126 1
                $keepHead = true;
127 1
                foreach ($this->children as $key => $child) {
128 1
                    if ($child->getType() == 'element' && $child->getName() == 'head') {
129 1
                        if ($keepHead) {
130 1
                            $keepHead = false;
131 1
                        } else {
132 1
                            unset($this->children[$key]);
133 1
                            if ($logger !== null) {
134 1
                                $logger->debug('Removed extraneous HEAD element.');
135 1
                            }
136
                        }
137 1
                    }
138 1
                }
139 1
            }
140 1
        }
141
142
        // Handle multiple BODY elements.
143 10 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...
144 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
145
                throw new ValidationException('HTML element can only have 1 BODY element child.');
146
            }
147
148 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
149 1
                return false;
150
            }
151
152 1
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
153
                // Remove extraneous BODY elements.
154 1
                $keepBody = true;
155 1
                foreach ($this->children as $key => $child) {
156 1
                    if ($child->getType() == 'element' && $child->getName() == 'body') {
157 1
                        if ($keepBody) {
158 1
                            $keepBody = false;
159 1
                        } else {
160 1
                            unset($this->children[$key]);
161 1
                            if ($logger !== null) {
162 1
                                $logger->debug('Removed extraneous BODY element.');
163 1
                            }
164
                        }
165 1
                    }
166 1
                }
167 1
            }
168 1
        }
169
170
        // Handle BODY before HEAD.
171 9
        if (!$headIsFirst && $bodyCount > 0) {
172 3
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW) {
173
                throw new ValidationException('HTML element requires the HEAD element to preceed the BODY element.');
174
            }
175
176 3
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_REMOVE) {
177 1
                return false;
178
            }
179
180 2
            if ($this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_FIX) {
181 2
                foreach ($this->children as $key => $child) {
182 2
                    if ($child->getType() == 'element' && $child->getName() == 'body') {
183 2
                        unset($this->children[$key]);
184 2
                        $this->appendChild($child);
185 2
                        if ($logger !== null) {
186 2
                            $logger->debug('Moved BODY element to end of HTML children.');
187 2
                        }
188
189 2
                        break;
190
                    }
191 2
                }
192 2
            }
193 2
        }
194
195 8
        return true;
196
    }
197
}
198