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

Link::isAllowedInBody()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 8.8571
cc 6
eloc 9
nc 3
nop 0
crap 42
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