Completed
Push — master ( 817727...d8e3ee )
by Kevin
03:58
created

Head   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 55.71 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 3 Features 5
Metric Value
wmc 14
c 9
b 3
f 5
lcom 1
cbo 3
dl 39
loc 70
ccs 40
cts 40
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fixSelf() 0 14 3
C removeInvalidChildren() 39 39 8
A removeInvalidSelf() 0 12 3

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\MetadataContent;
7
use Groundskeeper\Tokens\ElementTypes\OpenElement;
8
use Groundskeeper\Tokens\NonParticipating;
9
use Groundskeeper\Tokens\Token;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * "head" element
14
 *
15
 * https://html.spec.whatwg.org/multipage/semantics.html#the-head-element
16
 */
17
class Head extends OpenElement
18
{
19 32
    protected function fixSelf(LoggerInterface $logger)
20
    {
21
        // Look for "title" element
22 32
        foreach ($this->children as $child) {
23 32
            if ($child instanceof Title) {
24 32
                return;
25
            }
26 6
        }
27
28
        // Missing title.
29 4
        $logger->debug('Adding "title" element. One "title" element required.');
30 4
        $title = new Title($this->configuration, 'title');
31 4
        $this->prependChild($title);
32 4
    }
33
34 30 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...
35
    {
36
        // "head" element must contain only metadata content elements.
37
        // "head" element must contain exactly one "title" element.
38
        // "head" element must contain either 0 or 1 "base" element.
39 30
        $titleCount = 0;
40 30
        $baseCount = 0;
41 30
        foreach ($this->children as $child) {
42 30
            if ($child instanceof NonParticipating) {
43 1
                continue;
44
            }
45
46 30
            if (!$child instanceof MetadataContent) {
47 1
                $logger->debug('Removing ' . $child . '. Only children of metadata content allowed.');
48 1
                $this->removeChild($child);
49
50 1
                continue;
51
            }
52
53 30
            if ($child instanceof Title) {
54 30
                ++$titleCount;
55 30
                if ($titleCount > 1) {
56 1
                    $logger->debug('Removing ' . $child . '. Only one "title" element allowed.');
57 1
                    $this->removeChild($child);
58
59 1
                    continue;
60
                }
61 30
            } elseif ($child instanceof Base) {
62 3
                ++$baseCount;
63 3
                if ($baseCount > 1) {
64 1
                    $logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.');
65
66 1
                    $this->removeChild($child);
67
68 1
                    continue;
69
                }
70 3
            }
71 30
        }
72 30
    }
73
74 31
    protected function removeInvalidSelf(LoggerInterface $logger)
75
    {
76
        // "head" element must be a child of "html" element.
77 31
        $parent = $this->getParent();
78 31
        if ($parent !== null && !$parent instanceof Html) {
79 1
            $logger->debug('Removing ' . $this . '. Must be a child of "html" element.');
80
81 1
            return true;
82
        }
83
84 30
        return false;
85
    }
86
}
87