Passed
Push — error-message ( b7292a...ff40fa )
by Akihito
04:28 queued 02:25
created

MapProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 2
b 0
f 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A get() 0 12 2
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
final class MapProvider implements ProviderInterface
15
{
16
    /** @var MultiBindings */
17
    private $multiBindings;
18
19
    /** @var InjectionPointInterface */
20
    private $ip;
21
22
    /** @var InjectorInterface */
23
    private $injector;
24
25
    /** @var ParamReaderInterface  */
26
    private $reader;
27
28
    public function __construct(
29
        InjectionPointInterface $ip,
30
        MultiBindings $multiBindings,
31
        InjectorInterface $injector,
32
        ParamReaderInterface $reader
33
    ) {
34
        $this->multiBindings = $multiBindings;
35
        $this->ip = $ip;
36
        $this->injector = $injector;
37
        $this->reader = $reader;
38
    }
39
40
    public function get(): Map
41
    {
42
        /** @var ?Set $set */
43
        $set = $this->reader->getParametrAnnotation($this->ip->getParameter(), Set::class);
44
        if ($set === null) {
45
            throw new SetNotFound((string) $this->ip->getParameter());
46
        }
47
48
        /** @var array<string, LazyTo<object>> $lazies */
49
        $lazies = $this->multiBindings[$set->interface];
50
51
        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...
52
    }
53
}
54