GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Attributes::setAttributes()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Spatie\HtmlElement;
4
5
class Attributes
6
{
7
    /** @var array */
8
    protected $attributes = [];
9
10
    /** @var array */
11
    protected $classes = [];
12
13
    public function __construct(array $attributes = [])
14
    {
15
        $this->setAttributes($attributes);
16
    }
17
18
    /**
19
     * @param array $attributes
20
     *
21
     * @return $this
22
     */
23
    public function setAttributes(array $attributes)
24
    {
25
        foreach ($attributes as $attribute => $value) {
26
27
            if ($attribute === 'class') {
28
                $this->addClass($value);
29
                continue;
30
            }
31
32
            if (is_int($attribute)) {
33
                $attribute = $value;
34
                $value = '';
35
            }
36
37
            $this->setAttribute($attribute, $value);
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param string $attribute
45
     * @param string $value
46
     *
47
     * @return $this
48
     */
49
    public function setAttribute(string $attribute, string $value = '')
50
    {
51
        if ($attribute === 'class') {
52
            $this->addClass($value);
53
54
            return $this;
55
        }
56
57
        $this->attributes[$attribute] = $value;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param string|array $class
64
     *
65
     * @return $this
66
     */
67
    public function addClass($class)
68
    {
69
        if (!is_array($class)) {
70
            $class = [$class];
71
        }
72
73
        $this->classes = array_unique(
74
            array_merge($this->classes, $class)
75
        );
76
77
        return $this;
78
    }
79
80
    public function isEmpty() : bool
81
    {
82
        return empty($this->attributes) && empty($this->classes);
83
    }
84
85
    public function toArray() : array
86
    {
87
        if (empty($this->classes)) {
88
            return $this->attributes;
89
        }
90
91
        return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]);
92
    }
93
94
    public function toString() : string
95
    {
96
        if ($this->isEmpty()) {
97
            return '';
98
        }
99
100
        $attributeStrings = [];
101
102
        foreach ($this->toArray() as $attribute => $value) {
103
            if (is_null($value) || $value === '') {
104
                $attributeStrings[] = $attribute;
105
                continue;
106
            }
107
            $value = htmlspecialchars($value, ENT_COMPAT);
108
109
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
110
        }
111
112
        return implode(' ', $attributeStrings);
113
    }
114
115
    public function __toString() : string
116
    {
117
        return $this->toString();
118
    }
119
}
120