Completed
Push — master ( 559766...559d2f )
by Kevin
03:24
created

A   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 85
Duplicated Lines 10.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 21
c 5
b 2
f 2
lcom 1
cbo 2
dl 9
loc 85
ccs 55
cts 55
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 18 1
A fixSelf() 9 9 3
B removeInvalidChildren() 0 15 6
D removeInvalidSelf() 0 27 9
A isInteractiveContent() 0 4 1
A isTransparentElement() 0 4 1

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\InlineElement;
8
use Groundskeeper\Tokens\ElementTypes\InteractiveContent;
9
use Groundskeeper\Tokens\ElementTypes\OpenElement;
10
use Groundskeeper\Tokens\ElementTypes\PhrasingContent;
11
use Groundskeeper\Tokens\ElementTypes\TransparentElement;
12
use Groundskeeper\Tokens\NonParticipating;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * "a" element
17
 *
18
 * https://html.spec.whatwg.org/multipage/semantics.html#the-a-element
19
 */
20
class A extends OpenElement implements FlowContent, InteractiveContent, PhrasingContent, InlineElement, TransparentElement
21
{
22 7
    protected function getAllowedAttributes()
23
    {
24
        $aAllowedAttributes = array(
25 7
            '/^href$/i' => Attribute::URI,
26 7
            '/^target$/i' => Attribute::CS_STRING,
27 7
            '/^download$/i' => Attribute::CS_STRING,
28 7
            '/^ping$/i' => Attribute::URI,
29 7
            '/^rel$/i' => Attribute::CS_STRING,
30 7
            '/^hreflang$/i' => Attribute::CS_STRING,
31 7
            '/^type$/i' => Attribute::CS_STRING,
32
            '/^referrerpolicy$/i' => Attribute::CS_STRING
33 7
        );
34
35 7
        return array_merge(
36 7
            $aAllowedAttributes,
37 7
            parent::getAllowedAttributes()
38 7
        );
39
    }
40
41 7 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...
42
    {
43
        // If the "itemprop" attribute is specified on an "a" element, then
44
        // the "href" attribute must also be specified.
45 7
        if ($this->hasAttribute('itemprop') && !$this->hasAttribute('href')) {
46 1
            $logger->debug($this . ' with "itemprop" attribute requires the "href" attribute also.  Adding empty "href" attribute.');
47 1
            $this->addAttribute('href', '');
48 1
        }
49 7
    }
50
51 6
    protected function removeInvalidChildren(LoggerInterface $logger)
52
    {
53
        // There must be no interactive content or "a" element descendants.
54 6
        foreach ($this->children as $child) {
55 6
            if ($child instanceof NonParticipating) {
56 1
                continue;
57
            }
58
59 6
            if ($child instanceof self &&
60 6
                ($child instanceof InteractiveContent && $child->isInteractiveContent())) {
61 1
                $logger->debug('Removing ' . $child . '. Element "a" cannot contain "a" or interactive content elements.');
62 1
                $this->removeChild($child);
63 1
            }
64 6
        }
65 6
    }
66
67 6
    protected function removeInvalidSelf(LoggerInterface $logger)
68
    {
69
        // The target, download, ping, rel, hreflang, type, and
70
        // referrerpolicy attributes must be omitted if the href
71
        // attribute is not present.
72 6
        if (!$this->hasAttribute('href')) {
73 1
            if ($this->hasAttribute('target') ||
74 1
                $this->hasAttribute('download') ||
75 1
                $this->hasAttribute('ping') ||
76 1
                $this->hasAttribute('rel') ||
77 1
                $this->hasAttribute('hreflang') ||
78 1
                $this->hasAttribute('type') ||
79 1
                $this->hasAttribute('referrerpolicy')
80 1
            ) {
81 1
                $logger->debug('Removing invalid attributes. ' . $this . ' without "href" attribute cannot contain "target", "download", "ping", "rel", "hreflang", "type", or "referrerpolicy" attributes.');
82 1
                $this->removeAttribute('target');
83 1
                $this->removeAttribute('download');
84 1
                $this->removeAttribute('ping');
85 1
                $this->removeAttribute('rel');
86 1
                $this->removeAttribute('hreflang');
87 1
                $this->removeAttribute('type');
88 1
                $this->removeAttribute('referrerpolicy');
89 1
            }
90 1
        }
91
92 6
        return false;
93
    }
94
95 2
    public function isInteractiveContent()
96
    {
97 2
        return $this->hasAttribute('href');
98
    }
99
100 1
    public function isTransparentElement()
101
    {
102 1
        return true;
103
    }
104
}
105