Menu   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 7
loc 42
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 12 1
A fixSelf() 7 7 2
B removeInvalidChildren() 0 18 8

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\ElementTypes\FlowContent;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\ElementTypes\ScriptSupporting;
9
use Groundskeeper\Tokens\NonParticipating;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * "menu" element
14
 *
15
 * https://html.spec.whatwg.org/multipage/semantics.html#the-menu-element
16
 *
17
 * As of May 2016, implemented in only 8% of browsers.
18
 */
19
class Menu extends OpenElement implements FlowContent
20
{
21 3
    protected function getAllowedAttributes()
22
    {
23
        $menuAllowedAttributes = array(
24 3
            '/^type$/i' => Attribute::CI_ENUM . '("","context","toolbar"|"context")',
25
            '/^label$/i' => Attribute::CS_STRING
26
        );
27
28 3
        return array_merge(
29 3
            $menuAllowedAttributes,
30 3
            parent::getAllowedAttributes()
31
        );
32
    }
33
34 3 View Code Duplication
    protected function fixSelf(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...
35
    {
36 3
        if (!$this->hasAttribute('type')) {
37 2
            $logger->debug('Modifying ' . $this . '. Adding the default "type" attribute for the "menu" element.');
38 2
            $this->addAttribute('type', 'context');
39
        }
40 3
    }
41
42 3
    protected function removeInvalidChildren(LoggerInterface $logger)
43
    {
44
        // Only "li" and ScriptSupporting elements allowed.
45 3
        foreach ($this->children as $child) {
46 3
            if ($child instanceof NonParticipating) {
47 1
                continue;
48
            }
49
50 3
            if (!$child instanceof Li &&
51 3
                !$child instanceof Menuitem &&
52 3
                !$child instanceof Hr &&
53 3
                !$child instanceof self &&
54 3
                !$child instanceof ScriptSupporting) {
55 1
                $logger->debug('Removing ' . $child . '. Only elements "li", "menuitem", "hr", "menu", and script supporting elements allowed as children of "menu" element.');
56 3
                $this->removeChild($child);
57
            }
58
        }
59 3
    }
60
}
61