Passed
Push — master ( 92b282...84dd72 )
by n
02:28
created

ResolverTrait::resolveByContainer()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 3
crap 5
1
<?php
2
3
namespace N1215\Jugoya\Resolver;
4
5
use Interop\Http\ServerMiddleware\MiddlewareInterface;
6
use Psr\Container\ContainerInterface;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Psr\Container\ContainerExceptionInterface;
9
10
trait ResolverTrait
11
{
12
13
    /**
14
     * @param string|callable|MiddlewareInterface $ref
15
     * @param ContainerInterface $container
16
     * @param string $expectedClass
17
     * @return mixed
18
     * @throws UnresolvedException
19
     */
20 11
    public function resolveByContainer($ref, ContainerInterface $container, $expectedClass)
21
    {
22
        try {
23 11
            $entry = $container->get($ref);
0 ignored issues
show
Documentation introduced by
$ref is of type callable|object<Interop\...re\MiddlewareInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24 11
        } catch (NotFoundExceptionInterface $e) {
25 2
            throw new UnresolvedException('Could not found an entry from the container.', 0, $e);
26 2
        } catch (ContainerExceptionInterface $e) {
27 2
            throw new UnresolvedException('Something wrong with the container.', 0, $e);
28
        }
29
30 7
        if (!$entry instanceof $expectedClass) {
31 2
            $type = is_object($entry) ? get_class($entry) : gettype($entry);
32 2
            throw new UnresolvedException("Expected container returns an instance of {$expectedClass}, {$type} given.");
33
        }
34
35 5
        return $entry;
36
    }
37
}
38