Completed
Push — develop ( 523088...d9cee5 )
by Baptiste
01:42
created

Arguments::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose;
5
6
use Innmind\Compose\{
7
    Definition\Argument,
8
    Definition\Name,
9
    Exception\MissingArgument,
10
    Exception\ArgumentNotProvided
11
};
12
use Innmind\Immutable\{
13
    Sequence,
14
    MapInterface,
15
    Map
16
};
17
18
final class Arguments
19
{
20
    private $arguments;
21
    private $values;
22
23 10
    public function __construct(Argument ...$arguments)
24
    {
25 10
        $this->arguments = Sequence::of(...$arguments)->reduce(
26 10
            new Map('string', Argument::class),
27 10
            static function(Map $arguments, Argument $argument): Map {
28 8
                return $arguments->put(
29 8
                    (string) $argument->name(),
30 8
                    $argument
31
                );
32 10
            }
33
        );
34 10
        $this->values = new Map('string', 'mixed');
35 10
    }
36
37
    /**
38
     * @param MapInterface<string, mixed> $values
39
     *
40
     * @throws MissingArgument
41
     */
42 9
    public function bind(MapInterface $values): self
43
    {
44
        if (
45 9
            (string) $values->keyType() !== 'string' ||
46 9
            (string) $values->valueType() !== 'mixed'
47
        ) {
48 1
            throw new \TypeError('Argument 1 must be of type MapInterface<string, mixed>');
49
        }
50
51
        $this
52 8
            ->arguments
53 8
            ->filter(static function(string $name, Argument $argument): bool {
54 8
                return !$argument->optional() && !$argument->hasDefault();
55 8
            })
56 8
            ->foreach(static function(string $name) use ($values): void {
57 6
                if (!$values->contains($name)) {
1 ignored issue
show
Bug introduced by
$name of type string is incompatible with the type Innmind\Immutable\T expected by parameter $key of Innmind\Immutable\MapInterface::contains(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                if (!$values->contains(/** @scrutinizer ignore-type */ $name)) {
Loading history...
58 1
                    throw new MissingArgument($name);
59
                }
60 8
            });
61
        $this
62 7
            ->arguments
63 7
            ->filter(static function(string $name) use ($values): bool {
64 7
                return $values->contains($name);
1 ignored issue
show
Bug introduced by
$name of type string is incompatible with the type Innmind\Immutable\T expected by parameter $key of Innmind\Immutable\MapInterface::contains(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                return $values->contains(/** @scrutinizer ignore-type */ $name);
Loading history...
65 7
            })
66 7
            ->foreach(static function(string $name, Argument $argument) use ($values): void {
67 5
                $argument->validate($values->get($name));
1 ignored issue
show
Bug introduced by
$name of type string is incompatible with the type Innmind\Immutable\T expected by parameter $key of Innmind\Immutable\MapInterface::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                $argument->validate($values->get(/** @scrutinizer ignore-type */ $name));
Loading history...
68 7
            });
69
70 6
        $self = clone $this;
71 6
        $self->values = $values;
72
73 6
        return $self;
74
    }
75
76
    /**
77
     * @throws ArgumentNotProvided
78
     *
79
     * @return mixed
80
     */
81 5
    public function get(Name $name)
82
    {
83 5
        if (!$this->values->contains((string) $name)) {
84 3
            throw new ArgumentNotProvided($this->arguments->get((string) $name));
85
        }
86
87 3
        return $this->values->get((string) $name);
88
    }
89
}
90