Completed
Push — master ( 91ab9a...64950e )
by Kevin
04:15
created

Option::removeInvalidChildren()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 51.1484

Importance

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

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 2
    protected function getAllowedAttributes()
20
    {
21
        $optionAllowedAttributes = array(
22 2
            '/^disabled$/i' => Attribute::BOOL,
23 2
            '/^label$/i' => Attribute::CS_STRING,
24 2
            '/^selected$/i' => Attribute::BOOL,
25
            '/^value$/i' => Attribute::CS_STRING
26 2
        );
27
28 2
        return array_merge(
29 2
            $optionAllowedAttributes,
30 2
            parent::getAllowedAttributes()
31 2
        );
32
    }
33
34 1
    protected function removeInvalidChildren(LoggerInterface $logger)
35
    {
36 1
        if ($this->hasAttribute('label')) {
37
            if ($this->hasAttribute('value')) {
38
                // If both the "label" and "value" attributes are present,
39
                // then no content is allowed.
40
                foreach ($this->children as $child) {
41
                    if ($child instanceof NonParticipating) {
42
                        continue;
43
                    }
44
45
                    $logger->debug('Removing ' . $child . '. No content allowed inside an "option" element that contains both "label" and "value" attribute.');
46
                    $this->removeChild($child);
47
                }
48
            } else {
49
                // If the "label" and not "value" attribute is present,
50
                // then onyl text is allowed.
51
                foreach ($this->children as $child) {
52
                    if ($child instanceof NonParticipating ||
53
                        $child instanceof Text) {
54
                        continue;
55
                    }
56
57
                    $logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that contains only a "label" attribute.');
58
                    $this->removeChild($child);
59
                }
60
            }
61
        } else {
62
            // If no "label" attribute is present,
63
            // then only text is allowed.
64 1
            foreach ($this->children as $child) {
65 1
                if ($child instanceof NonParticipating ||
66 1
                    $child instanceof Text) {
67 1
                    continue;
68
                }
69
70
                $logger->debug('Removing ' . $child . '. Only text allowed inside an "option" element that does not contain a "label" attribute.');
71
                $this->removeChild($child);
72 1
            }
73
        }
74 1
    }
75
76 2 View Code Duplication
    protected function removeInvalidSelf(LoggerInterface $logger)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78 2
        $parent = $this->getParent();
79 2
        if ($parent !== null &&
80 2
            !$parent instanceof Select &&
81 2
            !$parent instanceof Datalist &&
82 2
            !$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 1
        return false;
89
    }
90
}
91