Issues (94)

src/di/ProviderSetProvider.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Di\Set;
8
use Ray\Di\Exception\SetNotFound;
9
10
/**
11
 * @implements ProviderInterface<mixed>
12
 * @template T of object
13
 */
14
final class ProviderSetProvider implements ProviderInterface
15
{
16
    public function __construct(private InjectionPointInterface $ip, private InjectorInterface $injector)
17
    {
18
    }
19
20
    /** @phpstan-return ProviderProvider<object> */
21
    public function get(): ProviderProvider
22
    {
23
        $param = $this->ip->getParameter();
24
        $setAttribute = $param->getAttributes(Set::class);
25
        if (! isset($setAttribute[0])) {
26
            throw new SetNotFound((string) $this->ip->getParameter());
27
        }
28
29
        $set = $setAttribute[0]->newInstance();
30
31
        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...
32
    }
33
}
34