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

Video   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 29.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 13
c 2
b 1
f 1
lcom 1
cbo 3
dl 21
loc 72
ccs 0
cts 57
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedAttributes() 21 21 1
D removeInvalidChildren() 0 36 10
A isInteractiveContent() 0 4 1
A isTransparentElement() 0 5 1

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\Element;
7
use Groundskeeper\Tokens\ElementTypes\EmbeddedContent;
8
use Groundskeeper\Tokens\ElementTypes\FlowContent;
9
use Groundskeeper\Tokens\ElementTypes\InteractiveContent;
10
use Groundskeeper\Tokens\ElementTypes\OpenElement;
11
use Groundskeeper\Tokens\ElementTypes\PhrasingContent;
12
use Groundskeeper\Tokens\ElementTypes\TransparentElement;
13
use Groundskeeper\Tokens\Token;
14
use Psr\Log\LoggerInterface;
15
16
/**
17
 * "video" element
18
 *
19
 * https://html.spec.whatwg.org/multipage/semantics.html#the-video-element
20
 */
21
class Video extends OpenElement implements FlowContent, PhrasingContent, EmbeddedContent, InteractiveContent, TransparentElement
22
{
23 View Code Duplication
    protected function getAllowedAttributes()
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...
24
    {
25
        $videoAllowedAttributes = array(
26
            '/^src$/i' => Element::ATTR_URI,
27
            '/^crossorigin$/i' => Element::ATTR_CS_STRING,
28
            '/^poster$/i' => Element::ATTR_URI,
29
            '/^preload$/i' => Element::ATTR_CI_ENUM . '("","none","metadata","auto"|"")',
30
            '/^autoplay$/i' => Element::ATTR_BOOL,
31
            '/^mediagroup$/i' => Element::ATTR_CS_STRING,
32
            '/^loop$/i' => Element::ATTR_BOOL,
33
            '/^muted$/i' => Element::ATTR_BOOL,
34
            '/^controls$/i' => Element::ATTR_BOOL,
35
            '/^width$/i' => Element::ATTR_INT,
36
            '/^height$/i' => Element::ATTR_INT
37
        );
38
39
        return array_merge(
40
            $videoAllowedAttributes,
41
            parent::getAllowedAttributes()
42
        );
43
    }
44
45
    protected function removeInvalidChildren(LoggerInterface $logger)
46
    {
47
        $hasSrc = $this->hasAttribute('src');
48
        foreach ($this->children as $child) {
49
            if ($child->getType() == Token::COMMENT) {
50
                continue;
51
            }
52
53
            if ($child->getType() == Token::TEXT) {
54
                continue;
55
            }
56
57
            if ($child->getType() !== Token::ELEMENT) {
58
                $logger->debug('Removing ' . $child . '. Only elements allowed as children of "video" element.');
59
                $this->removeChild($child);
60
61
                continue;
62
            }
63
64
            if (!$hasSrc && $child->getName() == 'source') {
65
                continue;
66
            }
67
68
            if ($child->getName() == 'track') {
69
                continue;
70
            }
71
72
            if ($child instanceof TransparentElement &&
73
                $child->isTransparentElement()) {
74
                continue;
75
            }
76
77
            $logger->debug('Removing ' . $child . '. Only "source", "track", and transparent elements allowed as children of "video" element.');
78
            $this->removeChild($child);
79
        }
80
    }
81
82
    public function isInteractiveContent()
83
    {
84
        return $this->hasAttribute('controls');
85
    }
86
87
    public function isTransparentElement()
88
    {
89
        /// @todo Implement this.  Complicated checks involved.
90
        return true;
91
    }
92
}
93