Completed
Push — master ( 1d4343...278efa )
by Kevin
04:08
created

Option::removeInvalidChildren()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
ccs 26
cts 26
cp 1
rs 5.2653
cc 11
eloc 22
nc 9
nop 1
crap 11

How to fix   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\Tokens\Attribute;
6
use Groundskeeper\Tokens\Element;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\NonParticipating;
9
use Groundskeeper\Tokens\Text;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * "option" element
14
 *
15
 * https://html.spec.whatwg.org/multipage/forms.html#the-option-element
16
 */
17
class Option extends OpenElement
18
{
19 5
    protected function getAllowedAttributes()
20
    {
21
        $optionAllowedAttributes = array(
22 5
            '/^disabled$/i' => Attribute::BOOL,
23 5
            '/^label$/i' => Attribute::CS_STRING,
24 5
            '/^selected$/i' => Attribute::BOOL,
25
            '/^value$/i' => Attribute::CS_STRING
26 5
        );
27
28 5
        return array_merge(
29 5
            $optionAllowedAttributes,
30 5
            parent::getAllowedAttributes()
31 5
        );
32
    }
33
34 4
    protected function removeInvalidChildren(LoggerInterface $logger)
35
    {
36 4
        if ($this->hasAttribute('label')) {
37 2
            if ($this->hasAttribute('value')) {
38
                // If both the "label" and "value" attributes are present,
39
                // then no content is allowed.
40 2
                foreach ($this->children as $child) {
41 2
                    if ($child instanceof NonParticipating) {
42 2
                        continue;
43
                    }
44
45 1
                    $logger->debug('Removing ' . $child . '. No content allowed inside an "option" element that contains both "label" and "value" attribute.');
46 1
                    $this->removeChild($child);
47 2
                }
48 2
            } else {
49
                // If the "label" and not "value" attribute is present,
50
                // then onyl text is allowed.
51 2
                foreach ($this->children as $child) {
52 2
                    if ($child instanceof NonParticipating ||
53 2
                        $child instanceof Text) {
54 1
                        continue;
55
                    }
56
57 1
                    $logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that contains only a "label" attribute.');
58 1
                    $this->removeChild($child);
59 2
                }
60
            }
61 2
        } else {
62
            // If no "label" attribute is present,
63
            // then only text is allowed.
64 4
            foreach ($this->children as $child) {
65 4
                if ($child instanceof NonParticipating ||
66 4
                    $child instanceof Text) {
67 4
                    continue;
68
                }
69
70 1
                $logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that does not contain a "label" attribute.');
71 1
                $this->removeChild($child);
72 4
            }
73
        }
74 4
    }
75
76 5
    protected function removeInvalidSelf(LoggerInterface $logger)
77
    {
78 5
        $parent = $this->getParent();
79 5
        if ($parent !== null &&
80 5
            !$parent instanceof Select &&
81 5
            !$parent instanceof Datalist &&
82 5
            !$parent instanceof Optgroup) {
83 1
            $logger->debug('Removing ' . $this . '. Only "select", "datalist", and "optgroup" elements allowed as parents of "option" element.');
84
85 1
            return true;
86
        }
87
88 4
        return false;
89
    }
90
}
91