MetadataBag::get()   A
last analyzed

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Thunder micro CLI framework.
7
 * (c) Jérémy Marodon <[email protected]>
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RxThunder\Core\Model;
13
14
final class MetadataBag
15
{
16
    /** @psalm-var array<string, scalar>  */
17
    private array $parameters = [];
18
19
    /**
20
     * @psalm-param array<string, scalar> $parameters
21
     */
22 4
    public function __construct(array $parameters)
23
    {
24 4
        foreach ($parameters as $key => $value) {
25 3
            $this->add((string) $key, $value);
26
        }
27 3
    }
28
29
    /**
30
     * @psalm-param scalar $value
31
     */
32 3
    public function add(string $key, $value): self
33
    {
34 3
        if (!is_scalar($value)) {
35 1
            throw new \InvalidArgumentException(sprintf(
36 1
                'MetadataBag expect scalar value, %s given',
37 1
                gettype($value)
38
            ));
39
        }
40
41 2
        $this->parameters[$key] = $value;
42
43 2
        return $this;
44
    }
45
46
    /**
47
     * @psalm-return ?scalar
48
     */
49 2
    public function get(string $key)
50
    {
51 2
        return $this->parameters[$key] ?? null;
52
    }
53
}
54