Completed
Push — master ( fef96a...9f8b44 )
by Kevin
03:02
created

Dt::doClean()   C

Complexity

Conditions 13
Paths 8

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 40
ccs 0
cts 32
cp 0
rs 5.1234
cc 13
eloc 23
nc 8
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\Configuration;
6
use Groundskeeper\Tokens\ElementTypes\HeadingContent;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\ElementTypes\SectioningContent;
9
use Groundskeeper\Tokens\Token;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * "dt" element
14
 *
15
 * https://html.spec.whatwg.org/multipage/semantics.html#the-dt-element
16
 */
17
class Dt extends OpenElement
18
{
19
    protected function doClean(LoggerInterface $logger)
20
    {
21
        // Must be child of "dl" element.
22
        $parent = $this->getParent();
23
        if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT &&
24
            $parent !== null &&
25
            $parent->getName() != 'dl') {
26
            $logger->debug('Element "dt" must be a child of a "dl" element.');
27
28
            return false;
29
        }
30
31
        // No "header", "footer", sectioning content, or heading content descendants.
32
        foreach ($this->children as $child) {
33
            if ($child->getType() == Token::COMMENT) {
34
                continue;
35
            }
36
37
            if ($child->getType() != Token::ELEMENT) {
38
                if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
39
                    $logger->debug('Removing ' . $child . '. Element "dt" cannot contain "header", "footer", section content, or heading content elements.');
40
                    $this->removeChild($child);
41
                }
42
43
                continue;
44
            }
45
46
            if ($this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
47
                if ($child->getName() == 'header' ||
48
                    $child->getName() == 'footer' ||
49
                    $child instanceof SectioningContent ||
50
                    $child instanceof HeadingContent) {
51
                    $logger->debug('Removing ' . $child . '. No "header", "footer", and sectioning content, or heading content elements allowed as children of "dt" element.');
52
                    $this->removeChild($child);
53
                }
54
            }
55
        }
56
57
        return true;
58
    }
59
}
60