Completed
Push — assisted ( d6f990...122841 )
by Akihito
02:58
created

AssistedInterceptor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.77%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
c 4
b 0
f 1
lcom 1
cbo 4
dl 0
loc 69
ccs 30
cts 31
cp 0.9677
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 13 1
A getName() 0 14 3
B injectAssistedParameters() 0 14 6
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Ray\Aop\MethodInterceptor;
10
use Ray\Aop\MethodInvocation;
11
use Ray\Aop\ReflectionMethod;
12
use Ray\Di\Di\Assisted;
13
14
final class AssistedInterceptor implements MethodInterceptor
15
{
16
    /**
17
     * @var InjectorInterface
18
     */
19
    private $injector;
20
21 2
    public function __construct(InjectorInterface $injector)
22
    {
23 2
        $this->injector = $injector;
24 2
    }
25
26
    /**
27
     * Intercepts any method and injects instances of the missing arguments
28
     * when they are type hinted
29
     */
30 2
    public function invoke(MethodInvocation $invocation)
31
    {
32 2
        $method = $invocation->getMethod();
33 2
        $assisted = $method->getAnnotation('Ray\Di\Di\Assisted');
34
        /* @var \Ray\Di\Di\Assisted $assisted */
35 2
        $parameters = $method->getParameters();
36 2
        $arguments = $invocation->getArguments()->getArrayCopy();
37 2
        $cntArgs = count($arguments);
38 2
        $arguments = $this->injectAssistedParameters($method, $assisted, $parameters, $arguments, $cntArgs);
39 2
        $invocation->getArguments()->exchangeArray($arguments);
40
41 2
        return $invocation->proceed();
42
    }
43
44 2
    private function getName(ReflectionMethod $method, \ReflectionParameter $parameter)
45
    {
46 2
        $named = $method->getAnnotation('Ray\Di\Di\Named');
47 2
        if (! $named) {
48 1
            return Name::ANY;
49
        }
50 1
        parse_str($named->value, $names);
51 1
        $paramName = $parameter->getName();
52 1
        if (isset($names[$paramName])) {
53 1
            return $names[$paramName];
54
        }
55
56
        return Name::ANY;
57
    }
58
59
    /**
60
     * @param \ReflectionMethod      $method
61
     * @param Assisted               $assisted
62
     * @param \ReflectionParameter[] $parameters
63
     * @param array                  $arguments
64
     * @param int                    $cntArgs
65
     *
66
     * @return array
67
     */
68 2
    public function injectAssistedParameters(\ReflectionMethod $method, Assisted $assisted, array $parameters, array $arguments, $cntArgs)
69
    {
70 2
        foreach ($parameters as $pos => $parameter) {
71 2
            if ($pos < $cntArgs || ! $assisted || ! in_array($parameter->getName(), $assisted->values)) {
72 2
                continue;
73
            }
74 2
            $hint = $parameter->getClass();
75 2
            $interface = $hint ? $hint->getName() : '';
76 2
            $name = $this->getName($method, $parameter);
0 ignored issues
show
Compatibility introduced by
$method of type object<ReflectionMethod> is not a sub-type of object<Ray\Aop\ReflectionMethod>. It seems like you assume a child class of the class ReflectionMethod 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...
77 2
            $arguments[] = $this->injector->getInstance($interface, $name);
78 2
        }
79
80 2
        return $arguments;
81
    }
82
}
83