Completed
Push — develop ( 16e25e...7d2249 )
by Baptiste
01:52
created

Merge::fromString()   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
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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,
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Compose\Definiti...vice\Constructor\Stream. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
    Exception\InvalidArgumentException
16
};
17
18
final class Merge implements Constructor
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 9
    public static function fromString(Str $value): Constructor
24
    {
25 9
        if ((string) $value !== 'merge') {
26 5
            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