Completed
Pull Request — master (#9)
by Jérémy
05:48 queued 02:29
created

MetadataBag::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RxThunder\Core\Model;
6
7
final class MetadataBag
8
{
9
    /** @var array<string, bool|int|float|string>  */
10
    private array $parameters = [];
11
12
    /**
13
     * @param array<mixed, mixed> $parameters
14
     */
15 5
    public function __construct(array $parameters)
16
    {
17 5
        foreach ($parameters as $key => $value) {
18 3
            $this->add((string) $key, $value);
19
        }
20 4
    }
21
22
    /**
23
     * @param bool|int|float|string $value
24
     */
25 3
    public function add(string $key, $value): self
26
    {
27 3
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
28 1
            throw new \InvalidArgumentException(sprintf(
29 1
                'MetadataBag expect scalar value, %s given',
30 1
                gettype($value)
31
            ));
32
        }
33
34 2
        $this->parameters[$key] = $value;
35
36 2
        return $this;
37
    }
38
39
    /**
40
     * @return bool|int|float|string
41
     */
42 2
    public function get(string $key)
43
    {
44 2
        return $this->parameters[$key] ?? null;
45
    }
46
}
47