Script   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 70
loc 70
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A withAttribute() 10 10 2
A withChild() 4 4 1
A withAsync() 4 4 1
A withIntegrity() 4 4 1
A withSrc() 4 4 1
A withType() 4 4 1
A withText() 4 4 1
A withDefer() 4 4 1
A withCrossOrigin() 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\Set\ElementSet;
9
use RoyallTheFourth\HtmlDocument\Tag\Standard;
10
11
/**
12
 * Class Script
13
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
14
 */
15 View Code Duplication
final class Script extends AbstractElement implements ParentElementInterface
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...
16
{
17
    public function __construct(AttributeSet $attributes = null, ElementSet $children = null)
18
    {
19
        $this->attributes = $attributes ?? new AttributeSet();
20
        $this->children = $children ?? new ElementSet();
21
        $this->tag = new Standard('script', $attributes, $children);
22
    }
23
24
    public function withAttribute(string $name, string $value = null): Script
25
    {
26
        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...
27
            $attribute = new StandardAttribute($name, $value);
28
        } else {
29
            $attribute = new BooleanAttribute($name);
30
        }
31
32
        return new Script($this->attributes->add($attribute), $this->children);
33
    }
34
35
    public function withChild(ElementInterface $element): Script
36
    {
37
        return new Script($this->attributes, $this->children->add($element));
38
    }
39
40
    public function withAsync(): Script
41
    {
42
        return $this->withAttribute('async');
43
    }
44
45
    /**
46
     * @param string $meta
47
     * @return Script
48
     * @see https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
49
     */
50
    public function withIntegrity(string $meta): Script
51
    {
52
        return $this->withAttribute('integrity', $meta);
53
    }
54
55
    public function withSrc(string $url): Script
56
    {
57
        return $this->withAttribute('src', $url);
58
    }
59
60
    public function withType(string $mime): Script
61
    {
62
        return $this->withAttribute('type', $mime);
63
    }
64
65
    public function withText(string $code): Script
66
    {
67
        return $this->withAttribute('text', $code);
68
    }
69
70
    public function withDefer(): Script
71
    {
72
        return $this->withAttribute('defer');
73
    }
74
75
    /**
76
     * @param string $policy
77
     * @return Script
78
     * @see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
79
     */
80
    public function withCrossOrigin(string $policy): Script
81
    {
82
        return $this->withAttribute('crossorigin', $policy);
83
    }
84
}
85