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

Option   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 74
Duplicated Lines 18.92 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 59.09%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 14 1
C removeInvalidChildren() 0 41 11
B removeInvalidSelf() 14 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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