Test Setup Failed
Push — master ( 5d533a...3e433e )
by Herberto
02:34
created

DependencyResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 29
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveDependencies() 0 13 2
A getDependenciesKey() 0 4 1
1
<?php
2
namespace Hgraca\MicroDI;
3
4
use Hgraca\Cache\CacheInterface;
5
use Hgraca\Cache\Exception\CacheItemNotFoundException;
6
use Hgraca\Helper\ClassHelper;
7
8
final class DependencyResolver implements DependencyResolverInterface
9
{
10
    /** @var CacheInterface */
11
    private $cache;
12
13
    public function __construct(CacheInterface $cache)
14
    {
15
        $this->cache = $cache;
16
    }
17
18
    public function resolveDependencies(string $dependentClass, string $dependentMethod): array
19
    {
20
        $dependenciesKey = $this->getDependenciesKey($dependentClass, $dependentMethod);
21
22
        try {
23
            $dependencies = $this->cache->fetch($dependenciesKey);
24
        } catch (CacheItemNotFoundException $e) {
25
            $dependencies = ClassHelper::getParameters($dependentClass, $dependentMethod);
26
            $this->cache->save($dependenciesKey, $dependencies);
0 ignored issues
show
Documentation introduced by
$dependencies is of type array, 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...
27
        }
28
29
        return $dependencies;
30
    }
31
32
    private function getDependenciesKey(string $class, string $method = '__construct'): string
33
    {
34
        return sprintf('%s::%s', str_replace('\\', '_', $class), $method);
35
    }
36
}
37