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

Link::doClean()   C

Complexity

Conditions 13
Paths 21

Size

Total Lines 49
Code Lines 23

Duplication

Lines 7
Ratio 14.29 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 7
loc 49
ccs 0
cts 35
cp 0
rs 5.1401
cc 13
eloc 23
nc 21
nop 1
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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