Passed
Branch master (c2c9e6)
by Enjoys
02:24
created

Attributes   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 37
ccs 16
cts 17
cp 0.9412
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B __toString() 0 22 8
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 11
    public function __construct(?array $attributes)
20
    {
21 11
        $this->attributes = $attributes;
22
    }
23
24 11
    public function __toString(): string
25
    {
26 11
        if ($this->attributes === null) {
27 4
            return '';
28
        }
29 9
        $result = [];
30 9
        foreach ($this->attributes as $key => $value) {
31 9
            if (is_int($key)) {
32 1
                $key = $value;
33 1
                $value = null;
34
            }
35 9
            if ($key === null || $key === '') {
36
                continue;
37
            }
38 9
            if ($value === null) {
39 1
                $result[] = sprintf("%s", $key);
40 1
                continue;
41
            }
42 8
            $result[] = sprintf("%s='%s'", $key, $value);
43
        }
44
45 9
        return (empty($result)) ? '' : ' ' . implode(" ", $result);
46
    }
47
}