Completed
Push — master ( 9fbe45...817727 )
by Kevin
03:24
created

Picture::removeInvalidChildren()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 40
Code Lines 22

Duplication

Lines 6
Ratio 15 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 40
ccs 0
cts 31
cp 0
rs 5.3846
cc 8
eloc 22
nc 8
nop 1
crap 72
1
<?php
2
3
namespace Groundskeeper\Tokens\Elements;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Tokens\ElementTypes\EmbeddedContent;
7
use Groundskeeper\Tokens\ElementTypes\FlowContent;
8
use Groundskeeper\Tokens\ElementTypes\OpenElement;
9
use Groundskeeper\Tokens\ElementTypes\PhrasingContent;
10
use Groundskeeper\Tokens\ElementTypes\ScriptSupporting;
11
use Groundskeeper\Tokens\Token;
12
use Psr\Log\LoggerInterface;
13
14
/**
15
 * "picture" element
16
 *
17
 * https://html.spec.whatwg.org/multipage/semantics.html#the-picture-element
18
 */
19
class Picture extends OpenElement implements FlowContent, PhrasingContent, EmbeddedContent
20
{
21
    protected function removeInvalidChildren(LoggerInterface $logger)
22
    {
23
        $encounteredImgElement = false;
24
        foreach ($this->children as $child) {
25
            if ($child->getType() == Token::COMMENT) {
26
                continue;
27
            }
28
29
            if ($child->getType() !== Token::ELEMENT) {
30
                $logger->debug('Removing ' . $child); /// @todo better message
31
                $this->removeChild($child);
32
33
                continue;
34
            }
35
36
            if ($child instanceof ScriptSupporting) {
37
                continue;
38
            }
39
40
            if ($child->getName() == 'img') {
41
                if ($encounteredImgElement) {
42
                    $logger->debug('Removing ' . $child . '. Only one "img" element allowed as child of "picture" element.');
43
                    $this->removeChild($child);
44
45
                    continue;
46
                }
47
48
                $encounteredImgElement = true;
49
50
                continue;
51
            }
52
53 View Code Duplication
            if ($child->getName() != 'source') {
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...
54
                $logger->debug('Removing ' . $child . '. Only one "img" element and zero to many "source" elements allowed as children of the "picture" element.');
55
                $this->removeChild($child);
56
57
                continue;
58
            }
59
        }
60
    }
61
}
62