Completed
Push — master ( 3f79e5...1ec99b )
by Kevin
02:42
created

Head::doClean()   C

Complexity

Conditions 17
Paths 75

Size

Total Lines 56
Code Lines 32

Duplication

Lines 11
Ratio 19.64 %

Code Coverage

Tests 42
CRAP Score 17

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 11
loc 56
ccs 42
cts 42
cp 1
rs 6.5889
cc 17
eloc 32
nc 75
nop 1
crap 17

How to fix   Long Method    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\OpenElement;
7
use Groundskeeper\Tokens\ElementTypes\MetadataContent;
8
use Groundskeeper\Tokens\Token;
9
use Psr\Log\LoggerInterface;
10
11
class Head extends OpenElement
12
{
13 16
    protected function doClean(LoggerInterface $logger = null)
14
    {
15
        // HEAD must contain only metadata content elements.
16
        // HEAD must contain exactly one TITLE element.
17
        // HEAD must contain either 0 or 1 BASE element.
18 16
        $titleCount = 0;
19 16
        $baseCount = 0;
20 16
        foreach ($this->children as $child) {
21 16
            if (!$child instanceof MetadataContent &&
22 16
                $child->getType() !== 'comment' &&
23 16
                $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
24 1
                $this->removeChild($child);
25 1
                if ($logger !== null) {
26 1
                    $logger->debug('Removing ' . $child . '. Only children of metadata content allowed.');
27 1
                }
28 1
            }
29
30 16
            if ($child->getType() != Token::ELEMENT) {
31 1
                continue;
32
            }
33
34 16
            if ($child->getName() == 'title') {
35 16
                $titleCount++;
36 16
                if ($titleCount > 1 &&
37 16
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
38 1
                    if ($logger !== null) {
39 1
                        $logger->debug('Removing ' . $child . '. Only one "title" element allowed.');
40 1
                    }
41
42 1
                    $this->removeChild($child);
43 1
                }
44 16 View Code Duplication
            } elseif ($child->getName() == 'base') {
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...
45 1
                $baseCount++;
46 1
                if ($baseCount > 1 &&
47 1
                    $this->configuration->get('clean-strategy') != Configuration::CLEAN_STRATEGY_LENIENT) {
48 1
                    if ($logger !== null) {
49 1
                        $logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.');
50 1
                    }
51
52 1
                    $this->removeChild($child);
53 1
                }
54 1
            }
55 16
        }
56
57
        // Missing title.
58 16
        if ($titleCount == 0) {
59 2
            if ($logger !== null) {
60 2
                $logger->debug('Adding "title" element. One "title" element required.');
61 2
            }
62
63 2
            $title = new Title($this->configuration, 'title');
64 2
            $this->prependChild($title);
65 2
        }
66
67 16
        return true;
68
    }
69
}
70