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

Attributes::__toString()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.0189

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
b 0
f 0
nc 15
nop 0
dl 0
loc 22
ccs 14
cts 15
cp 0.9333
crap 8.0189
rs 8.4444
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
}