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

MetadataBag   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 38
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 2
A get() 0 3 1
A __construct() 0 4 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