Completed
Push — 2.x ( f6f39e...3155fc )
by Akihito
18s queued 15s
created

ProviderSetProvider.php$0 ➔ get()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 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
/**
12
 * @implements ProviderInterface<mixed>
13
 * @template T of object
14
 */
15
final class ProviderSetProvider implements ProviderInterface
16
{
17
    /** @var InjectionPointInterface */
18
    private $ip;
19
20
    /** @var InjectorInterface */
21
    private $injector;
22
23
    /** @var ParamReaderInterface<T>  */
24
    private $reader;
25
26
    /**
27
     * @param ParamReaderInterface<T> $reader
28
     */
29
    public function __construct(
30
        InjectionPointInterface $ip,
31
        InjectorInterface $injector,
32
        ParamReaderInterface $reader
33
    ) {
34
        $this->ip = $ip;
35
        $this->injector = $injector;
36
        $this->reader = $reader;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function get()
43
    {
44
        $param = $this->ip->getParameter();
45
        /** @var ?Set<object> $set */
46
        $set = $this->reader->getParametrAnnotation($param, Set::class); // @phpstan-ignore-line
47
        if ($set === null) {
48
            throw new SetNotFound((string) $this->ip->getParameter());
49
        }
50
51
        return new ProviderProvider($this->injector, $set);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Ray\Di\Provid...($this->injector, $set) returns the type Ray\Di\ProviderProvider 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