Completed
Push — master ( 9f8b44...e0ac7a )
by Kevin
03:57
created

Li::doClean()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 18.42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
ccs 5
cts 13
cp 0.3846
rs 6.7272
cc 7
eloc 12
nc 5
nop 1
crap 18.42
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Tokens\ElementTypes\OpenElement;
6
use Groundskeeper\Tokens\ElementTypes\ScriptSupporting;
7
use Groundskeeper\Tokens\Token;
8
use Psr\Log\LoggerInterface;
9
10
/**
11
 * "li" element
12
 *
13
 * https://html.spec.whatwg.org/multipage/semantics.html#the-li-element
14
 */
15
class Li extends OpenElement
16
{
17
    protected function getAllowedAttributes()
18
    {
19
        // "value" attribute for "li" which are children of "ol" elements.
20
        $parent = $this->getParent();
21
        if ($parent == null) {
22
            return parent::getAllowedAttributes();
23
        }
24
25
        if ($parent->getType() === Token::ELEMENT &&
26
            $parent->getName() == 'ol') {
27
            $liAllowedAttributes = array(
28
                '/^value$/i' => Element::ATTR_INT
29
            );
30
31
            return array_merge(
32
                $liAllowedAttributes,
33
                parent::getAllowedAttributes()
34
            );
35
        }
36
37
        return parent::getAllowedAttributes();
38
    }
39
40 1
    protected function doClean(LoggerInterface $logger)
41
    {
42
        // Only allowed:
43
        // Inside ol elements.
44
        // Inside ul elements.
45
        // Inside menu elements whose type attribute is in the toolbar state.
46 1
        $parent = $this->getParent();
47 1
        if ($parent === null) {
48
            return true;
49
        }
50
51 1
        if ($parent->getName() == 'ol' || $parent->getName() == 'ul') {
52 1
            return true;
53
        }
54
55
        if ($parent->getName() == 'menu' && $parent->hasAttribute('type')) {
56
            $typeAttributeValue = $parent->getAttribute('type');
0 ignored issues
show
Bug introduced by
The method getAttribute() does not seem to exist on object<Groundskeeper\Tokens\Token>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            if ($typeAttributeValue == 'toolbar') {
58
                return true;
59
            }
60
        }
61
62
        $logger->debug('Element "li" only allowed inside "ol", "ul" and "menu[type=toolbar]" elements.');
63
64
        return false;
65
    }
66
}
67