Track   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 44
loc 44
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A withAttribute() 10 10 2
A withDefault() 4 4 1
A withKind() 4 4 1
A withLabel() 4 4 1
A withSrc() 4 4 1
A withSrcLang() 4 4 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 RoyallTheFourth\HtmlDocument\Element;
4
5
use RoyallTheFourth\HtmlDocument\Attribute\BooleanAttribute;
6
use RoyallTheFourth\HtmlDocument\Attribute\StandardAttribute;
7
use RoyallTheFourth\HtmlDocument\Set\AttributeSet;
8
use RoyallTheFourth\HtmlDocument\Tag\EmptyTag;
9
10
/**
11
 * Class Track
12
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/track
13
 */
14 View Code Duplication
final class Track extends AbstractElement
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    public function __construct(AttributeSet $attributes = null)
17
    {
18
        $this->attributes = $attributes ?? new AttributeSet();
19
        $this->tag = new EmptyTag('track', $attributes);
20
    }
21
22
    public function withAttribute(string $name, string $value = null): Track
23
    {
24
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
            $attribute = new StandardAttribute($name, $value);
26
        } else {
27
            $attribute = new BooleanAttribute($name);
28
        }
29
30
        return new Track($this->attributes->add($attribute));
31
    }
32
33
    public function withDefault(): Track
34
    {
35
        return $this->withAttribute('default');
36
    }
37
38
    public function withKind(string $kind = 'subtitles'): Track
39
    {
40
        return $this->withAttribute('kind', $kind);
41
    }
42
43
    public function withLabel(string $label): Track
44
    {
45
        return $this->withAttribute('label', $label);
46
    }
47
48
    public function withSrc(string $url): Track
49
    {
50
        return $this->withAttribute('src', $url);
51
    }
52
53
    public function withSrcLang(string $lang): Track
54
    {
55
        return $this->withAttribute('srclang', $lang);
56
    }
57
}
58