MapProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di\MultiBinding;
6
7
use Koriym\ParamReader\ParamReaderInterface;
8
use Ray\Di\Di\Set;
9
use Ray\Di\Exception\SetNotFound;
10
use Ray\Di\InjectionPointInterface;
11
use Ray\Di\InjectorInterface;
12
use Ray\Di\ProviderInterface;
13
14
/**
15
 * @implements ProviderInterface<Map>
16
 */
17
final class MapProvider implements ProviderInterface
18
{
19
    /** @var MultiBindings */
20
    private $multiBindings;
21
22
    /** @var InjectionPointInterface */
23
    private $ip;
24
25
    /** @var InjectorInterface */
26
    private $injector;
27
28
    /** @var ParamReaderInterface<object>  */
29
    private $reader;
30
31
    /**
32
     * @param ParamReaderInterface<object> $reader
33
     */
34
    public function __construct(
35
        InjectionPointInterface $ip,
36
        MultiBindings $multiBindings,
37
        InjectorInterface $injector,
38
        ParamReaderInterface $reader
39
    ) {
40
        $this->multiBindings = $multiBindings;
41
        $this->ip = $ip;
42
        $this->injector = $injector;
43
        $this->reader = $reader;
44
    }
45
46
    /**
47
     * @return Map<mixed>
48
     */
49
    public function get(): Map
50
    {
51
        /** @var ?Set<object> $set */
52
        $set = $this->reader->getParametrAnnotation($this->ip->getParameter(), Set::class);
53
        if ($set === null) {
54
            throw new SetNotFound((string) $this->ip->getParameter());
55
        }
56
57
        /** @var array<string, LazyTo<object>> $lazies */
58
        $lazies = $this->multiBindings[$set->interface];
59
60
        return new Map($lazies, $this->injector);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Ray\Di\MultiB...azies, $this->injector) returns the type Ray\Di\MultiBinding\Map which is incompatible with the return type mandated by Ray\Di\ProviderInterface::get() of Ray\Di\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
61
    }
62
}
63