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
![]() 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
}
}
![]() |
|||
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 |