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

Merge::__toString()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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