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

Head   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 59
Duplicated Lines 18.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 17
c 4
b 1
f 2
lcom 1
cbo 4
dl 11
loc 59
ccs 42
cts 42
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C doClean() 11 56 17

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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