Passed
Pull Request — master (#18)
by Enjoys
08:13
created

Attributes   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 40
ccs 18
cts 19
cp 0.9474
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B __toString() 0 25 9
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\AssetsCollector;
7
8
9
final class Attributes
10
{
11
    /**
12
     * @var array<array-key, string|null>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, string|null>|null at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, string|null>|null.
Loading history...
13
     */
14
    private ?array $attributes;
15
16
    /**
17
     * @param array<array-key, string|null>|null $attributes
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, string|null>|null at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, string|null>|null.
Loading history...
18
     */
19 15
    public function __construct(?array $attributes)
20
    {
21 15
        $this->attributes = $attributes;
22
    }
23
24 15
    public function __toString(): string
25
    {
26 15
        if ($this->attributes === null) {
27 4
            return '';
28
        }
29 13
        $result = [];
30 13
        foreach ($this->attributes as $key => $value) {
31 13
            if (is_int($key)) {
32 1
                $key = $value;
33 1
                $value = null;
34
            }
35 13
            if ($key === null || $key === '') {
36
                continue;
37
            }
38 13
            if ($value === false) {
39 2
                continue;
40
            }
41 13
            if ($value === null) {
42 1
                $result[] = sprintf("%s", $key);
43 1
                continue;
44
            }
45 12
            $result[] = sprintf("%s='%s'", $key, $value);
46
        }
47
48 13
        return (empty($result)) ? '' : ' ' . implode(" ", $result);
49
    }
50
}
51