Passed
Push — develop ( 7d2249...4ccadf )
by Baptiste
02:09
created

Merge   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 7 2
A __invoke() 0 14 2
A __toString() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Constructor;
5
6
use Innmind\Compose\{
7
    Definition\Service\Constructor,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Immutable\{
11
    Str,
12
    SetInterface,
13
    MapInterface,
14
    Stream,
15
    Exception\InvalidArgumentException
16
};
17
18
final class Merge implements Constructor
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 10
    public static function fromString(Str $value): Constructor
24
    {
25 10
        if ((string) $value !== 'merge') {
26 6
            throw new ValueNotSupported((string) $value);
27
        }
28
29 4
        return new self;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    public function __invoke(...$arguments): object
36
    {
37
        try {
38 3
            $arguments = Stream::of(SetInterface::class, ...$arguments);
39 2
        } catch (InvalidArgumentException $e) {
40 2
            $arguments = Stream::of(MapInterface::class, ...$arguments);
41
        }
42
43
        return $arguments
44 2
            ->drop(1)
45 2
            ->reduce(
46 2
                $arguments->first(),
47 2
                static function($structure, $element) {
48 2
                    return $structure->merge($element);
49 2
                }
50
            );
51
    }
52
53 1
    public function __toString(): string
54
    {
55 1
        return 'merge';
56
    }
57
}
58