anonymous//src/di/ProviderSetProvider.php$0   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 23
rs 10
c 0
b 0
f 0
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 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...
52
        {
53
            /** @var InjectorInterface  */
54
            private $injector;
55
56
            /** @var Set<object>  */
57
            private $set;
58
59
            /**
60
             * @param Set<object> $set
61
             */
62
            public function __construct(InjectorInterface $injector, Set $set)
63
            {
64
                $this->injector = $injector;
65
                $this->set = $set;
66
            }
67
68
            /**
69
             * @return mixed
70
             */
71
            public function get()
72
            {
73
                return $this->injector->getInstance($this->set->interface, $this->set->name);
74
            }
75
        };
76
    }
77
}
78