ContainerAwareResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prozorov\Repositories\Resolvers;
6
7
use Prozorov\Repositories\Contracts\{RepositoryInterface, ResolverInterface};
8
use Prozorov\Repositories\Exceptions\CouldNotResolve;
9
use Psr\Container\{ContainerInterface, ContainerExceptionInterface};
10
11
class ContainerAwareResolver implements ResolverInterface
12
{
13
    /**
14
     * @var ContainerInterface $container
15
     */
16
    protected $container;
17
18 3
    public function __construct(ContainerInterface $container)
19
    {
20 3
        $this->container = $container;
21 3
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26 3
    public function resolve(string $className): RepositoryInterface
27
    {
28
        try {
29 3
            $resolved = $this->container->get($className);
30
31 2
            if (! ($resolved instanceof RepositoryInterface)) {
32 1
                $class = get_class($resolved);
33
34 1
                $message = $class . ' does not implements RepositoryInterface. Refer to documentation';
35 1
                $this->fail($className, $message);
36
            }
37
38 1
            return $resolved;
39 2
        } catch (ContainerExceptionInterface $exception) {
40 1
            $this->fail($className, $exception->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $this->fail($className, $exception->/** @scrutinizer ignore-call */ getMessage());
Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Prozorov\Repositories\Co...cts\RepositoryInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
41
        }
42
    }
43
44
    /**
45
     * fail.
46
     *
47
     * @access    protected
48
     * @param string $className
49
     * @param string $message
50
     * @return    void
51
     */
52 2
    protected function fail(string $className, string $message = ''): void
53
    {
54 2
        $exception = new CouldNotResolve($message);
55 2
        $exception->setRepositoryCode($className);
56
57 2
        throw $exception;
58
    }
59
}
60