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

Attributes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
}