Merge   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 51
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A compile() 0 3 1
A fromString() 0 7 2
A __invoke() 0 22 3
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
    Compilation\Service\Constructor as CompiledConstructor,
10
    Compilation\Service\Constructor\Merge as CompiledMerge,
11
    Compilation\Service\Argument as CompiledArgument,
12
    Lazy,
13
};
14
use Innmind\Immutable\{
15
    Str,
16
    SetInterface,
17
    MapInterface,
18
    Stream,
19
    Sequence,
20
    Exception\InvalidArgumentException
21
};
22
23
final class Merge implements Constructor
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 16
    public static function fromString(Str $value): Constructor
29
    {
30 16
        if ((string) $value !== 'merge') {
31 10
            throw new ValueNotSupported((string) $value);
32
        }
33
34 6
        return new self;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function __invoke(...$arguments): object
41
    {
42 4
        $arguments = Sequence::of(...$arguments)->map(static function($argument) {
43 4
            if ($argument instanceof Lazy) {
44 1
                return $argument->load();
45
            }
46
47 3
            return $argument;
48 4
        });
49
50
        try {
51 4
            $arguments = Stream::of(SetInterface::class, ...$arguments);
52 2
        } catch (InvalidArgumentException $e) {
53 2
            $arguments = Stream::of(MapInterface::class, ...$arguments);
54
        }
55
56
        return $arguments
57 3
            ->drop(1)
58 3
            ->reduce(
59 3
                $arguments->first(),
60 3
                static function($structure, $element) {
61 3
                    return $structure->merge($element);
62 3
                }
63
            );
64
    }
65
66 1
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
67
    {
68 1
        return new CompiledMerge(...$arguments);
69
    }
70
71 1
    public function __toString(): string
72
    {
73 1
        return 'merge';
74
    }
75
}
76