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

Option   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
ccs 44
cts 44
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 14 1
B removeInvalidSelf() 0 14 5
C removeInvalidChildren() 0 41 11
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