Completed
Push — master ( 9fbe45...817727 )
by Kevin
03:24
created

A::removeInvalidSelf()   D

Complexity

Conditions 9
Paths 3

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9

Importance

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