Issues (61)

src/di/MethodInvocationProvider.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\MethodInvocation;
8
use Ray\Di\Exception\MethodInvocationNotAvailable;
9
10
/**
11
 * @implements ProviderInterface<MethodInvocation>
12
 */
13
final class MethodInvocationProvider implements ProviderInterface
14
{
15
    /** @var ?MethodInvocation */
16
    private $invocation;
17
18
    public function set(MethodInvocation $invocation): void
19
    {
20
        $this->invocation = $invocation;
21
    }
22
23
    public function get(): MethodInvocation
24
    {
25
        if ($this->invocation === null) {
26
            throw new MethodInvocationNotAvailable();
27
        }
28
29
        return $this->invocation;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->invocation returns the type Ray\Aop\MethodInvocation 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...
30
    }
31
}
32