Completed
Push — develop ( 15ec3c...8b62a8 )
by Baptiste
02:19
created

Merge::compile()   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 1
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
    Compilation\Service\Constructor as CompiledConstructor,
10
    Compilation\Service\Constructor\Merge as CompiledMerge,
11
    Compilation\Service\Argument as CompiledArgument
12
};
13
use Innmind\Immutable\{
14
    Str,
15
    SetInterface,
16
    MapInterface,
17
    Stream,
18
    Exception\InvalidArgumentException
19
};
20
21
final class Merge implements Constructor
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 13
    public static function fromString(Str $value): Constructor
27
    {
28 13
        if ((string) $value !== 'merge') {
29 8
            throw new ValueNotSupported((string) $value);
30
        }
31
32 5
        return new self;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function __invoke(...$arguments): object
39
    {
40
        try {
41 3
            $arguments = Stream::of(SetInterface::class, ...$arguments);
42 2
        } catch (InvalidArgumentException $e) {
43 2
            $arguments = Stream::of(MapInterface::class, ...$arguments);
44
        }
45
46
        return $arguments
47 2
            ->drop(1)
48 2
            ->reduce(
49 2
                $arguments->first(),
50 2
                static function($structure, $element) {
51 2
                    return $structure->merge($element);
52 2
                }
53
            );
54
    }
55
56 1
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
57
    {
58 1
        return new CompiledMerge(...$arguments);
2 ignored issues
show
Bug introduced by
The call to Innmind\Compose\Compilat...or\Merge::__construct() has too few arguments starting with argument2. ( Ignorable by Annotation )

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

58
        return /** @scrutinizer ignore-call */ new CompiledMerge(...$arguments);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$arguments is expanded, but the parameter $argument1 of Innmind\Compose\Compilat...or\Merge::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

58
        return new CompiledMerge(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
59
    }
60
61 1
    public function __toString(): string
62
    {
63 1
        return 'merge';
64
    }
65
}
66