MapProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di\MultiBinding;
6
7
use Ray\Di\Di\Set;
8
use Ray\Di\Exception\SetNotFound;
9
use Ray\Di\InjectionPointInterface;
10
use Ray\Di\InjectorInterface;
11
use Ray\Di\ProviderInterface;
12
13
/**
14
 * @implements ProviderInterface<Map>
15
 */
16
final class MapProvider implements ProviderInterface
17
{
18
    public function __construct(private readonly InjectionPointInterface $ip, private MultiBindings $multiBindings, private readonly InjectorInterface $injector)
19
    {
20
    }
21
22
    /**
23
     * @return Map<mixed>
24
     */
25
    public function get(): Map
26
    {
27
        $param = $this->ip->getParameter();
28
        $setAttribute = $param->getAttributes(Set::class);
29
        if (! isset($setAttribute[0])) {
30
            throw new SetNotFound((string) $param);
31
        }
32
33
        /** @var Set<object> $set */
34
        $set = $setAttribute[0]->newInstance();
35
36
        /** @var array<string, LazyTo<object>> $lazies */
37
        $lazies = $this->multiBindings[$set->interface];
38
39
        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...
40
    }
41
}
42