Completed
Push — develop ( b9b8dc...f49312 )
by Baptiste
02:08
created

Service::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition;
5
6
use Innmind\Compose\{
7
    Definition\Service\Constructor,
8
    Definition\Service\Argument,
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Compose\Definition\Argument. 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...
9
    Definitions,
10
    Exception\ServiceCannotDecorateMultipleServices
11
};
12
use Innmind\Immutable\{
13
    StreamInterface,
14
    Stream
15
};
16
17
final class Service
18
{
19
    private $name;
20
    private $construct;
21
    private $arguments;
22
    private $exposeName;
23
24 25
    public function __construct(
25
        Name $name,
26
        Constructor $constructor,
27
        Argument ...$arguments
28
    ) {
29 25
        $this->name = $name;
30 25
        $this->construct = $constructor;
31 25
        $this->arguments = Stream::of(Argument::class, ...$arguments);
32
33 25
        $decorates = $this->arguments->filter(static function(Argument $argument): bool {
34 11
            return $argument instanceof Argument\Decorate;
35 25
        });
36
37 25
        if ($decorates->size() > 1) {
38 1
            throw new ServiceCannotDecorateMultipleServices((string) $name);
39
        }
40 24
    }
41
42 23
    public function exposeAs(Name $name): self
43
    {
44 23
        $self = clone $this;
45 23
        $self->exposeName = $name;
46
47 23
        return $self;
48
    }
49
50 23
    public function exposed(): bool
51
    {
52 23
        return $this->exposeName instanceof Name;
53
    }
54
55 23
    public function exposedAs(): Name
56
    {
57 23
        return $this->exposeName;
58
    }
59
60 4
    public function isExposedAs(Name $name): bool
61
    {
62 4
        return (string) $this->exposeName === (string) $name;
63
    }
64
65 24
    public function name(): Name
66
    {
67 24
        return $this->name;
68
    }
69
70 1
    public function constructor(): Constructor
71
    {
72 1
        return $this->construct;
73
    }
74
75
    /**
76
     * @return StreamInterface<Argument>
77
     */
78 1
    public function arguments(): StreamInterface
79
    {
80 1
        return $this->arguments;
81
    }
82
83 9
    public function build(Definitions $definitions): object
84
    {
85 9
        return ($this->construct)(...$this->arguments->reduce(
86 9
            Stream::of('mixed'),
87 9
            static function(Stream $arguments, Argument $argument) use ($definitions): Stream {
88 4
                return $argument->resolve($arguments, $definitions);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $argument->resolv...rguments, $definitions) returns the type Innmind\Immutable\StreamInterface which includes types incompatible with the type-hinted return Innmind\Immutable\Stream.
Loading history...
89 9
            }
90
        ));
91
    }
92
}
93