Completed
Push — master ( d41cf1...36fce9 )
by Javi
23:31
created

CallableResolver::resolve()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace Philae\Utils;
4
5
use League\Container\Container;
6
use League\Container\ContainerAwareInterface;
7
use League\Container\ContainerAwareTrait;
8
use Middlewares\Utils\CallableResolver\CallableResolverInterface;
9
use Middlewares\Utils\CallableResolver\ReflectionResolver;
10
use Middlewares\Utils\CallableResolver\Resolver;
11
use Psr\Container\ContainerInterface;
12
13
/**
14
 * Callable resolver with support for: __invoke magic method, container resolution and reflection resolution.
15
 *
16
 * @method Container|ContainerInterface getContainer()
17
 */
18
class CallableResolver extends Resolver implements CallableResolverInterface, ContainerAwareInterface
19
{
20
    use ContainerAwareTrait;
21
22
    /**
23
     * @var ReflectionResolver
24
     */
25
    private $delegateResolver;
26
27
    /**
28
     * @param CallableResolverInterface|null $delegateResolver
29
     */
30
    public function __construct(CallableResolverInterface $delegateResolver = null)
31
    {
32
        $this->delegateResolver = $delegateResolver ?: new ReflectionResolver();
0 ignored issues
show
Documentation Bug introduced by
$delegateResolver ?: new...er\ReflectionResolver() is of type object<Middlewares\Utils...lableResolverInterface>, but the property $delegateResolver was declared to be of type object<Middlewares\Utils...ver\ReflectionResolver>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
    }
34
35
    public function resolve($callable, array $args = [])
36
    {
37
        $callable = $this->delegateResolver->resolve($this->normalizeCallable($callable, $args), $args);
38
39
        if (!$this->getContainer()) {
40
            return $callable;
41
        }
42
43
        // Inflect callable
44
        if (is_array($callable) && isset($callable[0]) && ($callable[0] instanceof ContainerAwareInterface)) {
45
            $callable[0]->setContainer($this->getContainer());
0 ignored issues
show
Compatibility introduced by
$this->getContainer() of type object<Psr\Container\ContainerInterface> is not a sub-type of object<League\Container\ContainerInterface>. It seems like you assume a child interface of the interface Psr\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46
        } elseif ($callable instanceof ContainerAwareInterface) {
47
            $callable->setContainer($this->getContainer());
0 ignored issues
show
Compatibility introduced by
$this->getContainer() of type object<Psr\Container\ContainerInterface> is not a sub-type of object<League\Container\ContainerInterface>. It seems like you assume a child interface of the interface Psr\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
        }
49
50
        return $callable;
51
    }
52
53
    /**
54
     * @param mixed $callable
55
     * @param array $args
56
     * @return array|string|mixed
57
     */
58
    protected function normalizeCallable($callable, array $args = [])
59
    {
60
        // invokable object
61
        if (is_object($callable) && method_exists($callable, '__invoke')) {
62
            return $callable;
63
        }
64
65
        // class::method string
66
        if (is_string($callable)) {
67
            // Split ::
68
            $callable = $this->resolveString($callable);
69
        }
70
71
        // function string
72
        if (is_string($callable) && function_exists($callable)) {
73
            return $callable;
74
        }
75
76
        // class string (invokable class)
77
        if (is_string($callable)
78
            && class_exists($callable)
79
            && function_exists($callable . '::__invoke')
80
        ) {
81
            $instance = $this->findInContainer($callable, $args);
82
            if ($instance) {
83
                $callable = $instance;
84
            }
85
86
            return [$callable, '__invoke'];
87
        }
88
89
        // callable array with class and method as strings
90
        if (is_array($callable) && is_string($callable[0]) && class_exists($callable[0])) {
91
            $instance = $this->findInContainer($callable[0], $args);
92
            if ($instance) {
93
                $callable[0] = $instance;
94
            }
95
96
            return $callable;
97
        }
98
99
        return $callable;
100
    }
101
102
    /**
103
     * @param string $className
104
     * @param array $args
105
     * @return bool|mixed
106
     */
107
    protected function findInContainer($className, array $args = [])
108
    {
109
        if ($this->getContainer() && $this->getContainer()->has($className)) {
110
            return $this->getContainer()->get($className, $args);
0 ignored issues
show
Unused Code introduced by
The call to ContainerInterface::get() has too many arguments starting with $args.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
111
        }
112
113
        return false;
114
    }
115
}
116