Passed
Push — injecting_provider ( 2a7465 )
by Akihito
02:19
created

InjectingProviderProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 37
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ get() 0 3 1
A __construct() 0 4 1
A hp$0 ➔ __construct() 0 4 1
get() 0 23 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Di\Set;
8
9
use function assert;
10
11
final class InjectingProviderProvider implements ProviderInterface
12
{
13
    private InjectionPointInterface $ip;
14
    private InjectorInterface $injector;
15
16
    public function __construct(InjectionPointInterface $ip, InjectorInterface $injector)
17
    {
18
        $this->ip = $ip;
19
        $this->injector = $injector;
20
    }
21
22
    /**
23
     * @return mixed
24
     */
25
    public function get()
26
    {
27
        $set = $this->ip->getParameter()->getAttributes(Set::class)[0];
28
        $instance = $set->newInstance();
29
        assert($instance instanceof Set);
30
31
        return new class ($this->injector, $instance) implements ProviderInterface
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ClassNode($this->injector, $instance) returns the type anonymous//src/di/InjectingProviderProvider.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...
32
        {
33
            private InjectorInterface $injector;
34
            private Set $set;
35
36
            public function __construct(InjectorInterface $injector, Set $set)
37
            {
38
                $this->injector = $injector;
39
                $this->set = $set;
40
            }
41
42
            /**
43
             * @return mixed
44
             */
45
            public function get()
46
            {
47
                return $this->injector->getInstance($this->set->interface, $this->set->name);
48
            }
49
        };
50
    }
51
}
52