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

Head::removeInvalidChildren()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 39
Code Lines 22

Duplication

Lines 39
Ratio 100 %

Code Coverage

Tests 25
CRAP Score 8

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 39
loc 39
ccs 25
cts 25
cp 1
rs 5.3846
cc 8
eloc 22
nc 8
nop 1
crap 8
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