Completed
Push — develop ( c0cf48...9f68ce )
by Baptiste
03:11
created

Dependency   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A __construct() 0 8 1
A bind() 0 18 1
A build() 0 9 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition;
5
6
use Innmind\Compose\{
7
    Definition\Dependency\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...
8
    Services,
9
    Exception\ReferenceNotFound
10
};
11
use Innmind\Immutable\{
12
    Set,
13
    Map
14
};
15
16
final class Dependency
17
{
18
    private $name;
19
    private $services;
20
    private $arguments;
21
22 10
    public function __construct(
23
        Name $name,
24
        Services $services,
25
        Argument ...$arguments
26
    ) {
27 10
        $this->name = $name;
28 10
        $this->services = $services;
29 10
        $this->arguments = Set::of(Argument::class, ...$arguments);
30 10
    }
31
32 6
    public function name(): Name
33
    {
34 6
        return $this->name;
35
    }
36
37 2
    public function bind(Services $services): self
38
    {
39
        $arguments = $this
40 2
            ->arguments
41 2
            ->reduce(
42 2
                new Map('string', 'mixed'),
43 2
                static function(Map $arguments, Argument $argument) use ($services): Map {
44 2
                    return $arguments->put(
45 2
                        (string) $argument->name(),
46 2
                        $argument->resolve($services)
47
                    );
48 2
                }
49
            );
50
51 2
        $self = clone $this;
52 2
        $self->services = $self->services->inject($arguments);
53
54 2
        return $self;
55
    }
56
57 7
    public function build(Name $name): object
58
    {
59 7
        $service = $this->services->get($name);
60
61 7
        if (!$service->exposed() || !$service->isExposedAs($name)) {
62 3
            throw new ReferenceNotFound((string) $name);
63
        }
64
65 4
        return $this->services->build($name);
66
    }
67
}
68