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

ProviderSetProvider::get()

Size

Total Lines 28
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 28
c 0
b 0
f 0
nc 2
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ProviderSetProvider.php$0 ➔ get() 0 3 1
A ProviderSetProvider.php$0 ➔ __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Koriym\ParamReader\ParamReaderInterface;
8
use Ray\Di\Di\Set;
9
use Ray\Di\Exception\SetNotFound;
10
11
final class ProviderSetProvider implements ProviderInterface
12
{
13
    /** @var InjectionPointInterface */
14
    private $ip;
15
16
    /** @var InjectorInterface */
17
    private $injector;
18
19
    /** @var ParamReaderInterface  */
20
    private $reader;
21
22
    public function __construct(
23
        InjectionPointInterface $ip,
24
        InjectorInterface $injector,
25
        ParamReaderInterface $reader
26
    ) {
27
        $this->ip = $ip;
28
        $this->injector = $injector;
29
        $this->reader = $reader;
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function get()
36
    {
37
        /** @var ?Set $set */
38
        $set = $this->reader->getParametrAnnotation($this->ip->getParameter(), Set::class);
39
        if ($set === null) {
40
            throw new SetNotFound((string) $this->ip->getParameter());
41
        }
42
43
        return new class ($this->injector, $set) implements ProviderInterface
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ClassNode($this->injector, $set) returns the type anonymous//src/di/ProviderSetProvider.php$0 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...
44
        {
45
            /** @var InjectorInterface  */
46
            private $injector;
47
48
            /** @var Set  */
49
            private $set;
50
51
            public function __construct(InjectorInterface $injector, Set $set)
52
            {
53
                $this->injector = $injector;
54
                $this->set = $set;
55
            }
56
57
            /**
58
             * @return mixed
59
             */
60
            public function get()
61
            {
62
                return $this->injector->getInstance($this->set->interface, $this->set->name);
63
            }
64
        };
65
    }
66
}
67