Completed
Push — master ( 1ec99b...a17ead )
by Kevin
02:44
created

Link   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 20
c 5
b 1
f 2
lcom 1
cbo 3
dl 7
loc 93
ccs 0
cts 64
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 0 17 1
C doClean() 7 49 13
B isAllowedInBody() 0 15 6

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\Element;
6
use Groundskeeper\Tokens\ElementTypes\ClosedElement;
7
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
8
use Psr\Log\LoggerInterface;
9
10
class Link extends ClosedElement implements MetadataContent
11
{
12
    protected function getAllowedAttributes()
13
    {
14
        $linkAllowedAttributes = array(
15
            '/^href$/i' => Element::ATTR_URI,
16
            '/^crossorigin$/i' => Element::ATTR_CS_STRING,
17
            '/^rel$/i' => Element::ATTR_CI_SSENUM . '("alternate","author","help","icon","license","next","pingback","prefetch","prev","search","stylesheet")',
18
            '/^media$/i' => Element::ATTR_CI_STRING,
19
            '/^hreflang$/i' => Element::ATTR_CS_STRING,
20
            '/^type$/i' => Element::ATTR_CI_STRING,
21
            '/^sizes$/i' => Element::ATTR_CI_STRING
22
        );
23
24
        return array_merge(
25
            $linkAllowedAttributes,
26
            parent::getAllowedAttributes()
27
        );
28
    }
29
30
    protected function doClean(LoggerInterface $logger = null)
31
    {
32
        // Must have "href" attribute.
33 View Code Duplication
        if (!$this->hasAttribute('href')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
            if ($logger !== null) {
35
                $logger->debug('Element "link" requires "href" attribute.');
36
            }
37
38
            return false;
39
        }
40
41
        // Must have either "rel" or "itemprop" attribute, but not both.
42
        $attrCount = 0;
43
        foreach ($this->attributes as $key => $value) {
44
            if ($key == 'rel' || $key == 'itemprop') {
45
                $attrCount++;
46
            }
47
48
            if ($attrCount > 1) {
49
                // If both, then we don't know which one should be kept,
50
                // so we recommend to delete the entire element.
51
                if ($logger !== null) {
52
                    $logger->debug('Element "link" requires either "rel" or "itemprop" attribute, but not both.');
53
                }
54
55
                return false;
56
            }
57
        }
58
59
        if ($attrCount == 0) {
60
            if ($logger !== null) {
61
                $logger->debug('Element "link" requires either "rel" or "itemprop" attribute.');
62
            }
63
64
            return false;
65
        }
66
67
        // If inside "body" element, then we check if allowed.
68
        $body = new Body($this->configuration, 'body');
69
        if ($this->hasAncestor($body) && !$this->isAllowedInBody()) {
70
            if ($logger !== null) {
71
                $logger->debug('Element "link" does not have the correct attributes to be allowed inside the "body" element.');
72
            }
73
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Is this element allowed inside a body element?
82
     *
83
     * https://html.spec.whatwg.org/multipage/semantics.html#allowed-in-the-body
84
     *
85
     * @return boolean True if allowed.
86
     */
87
    public function isAllowedInBody()
88
    {
89
        if ($this->hasAttribute('itemprop')) {
90
            return true;
91
        }
92
93
        if ($this->hasAttribute('rel') &&
94
            ($this->attributes['rel'] == 'pingback' ||
95
             $this->attributes['rel'] == 'prefetch' ||
96
             $this->attributes['rel'] == 'stylesheet')) {
97
            return true;
98
        }
99
100
        return false;
101
    }
102
}
103