Completed
Push — assisted ( 122841...48bf6b )
by Akihito
04:46
created

AssistedInterceptor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.97%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 10
c 5
b 0
f 1
lcom 1
cbo 4
dl 0
loc 71
ccs 32
cts 33
cp 0.9697
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 15 2
A getName() 0 14 3
A injectAssistedParameters() 0 14 4
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
        if ($assisted instanceof Assisted) {
39 2
            $arguments = $this->injectAssistedParameters($method, $assisted, $parameters, $arguments, $cntArgs);
40 2
        }
41 2
        $invocation->getArguments()->exchangeArray($arguments);
42
43 2
        return $invocation->proceed();
44
    }
45
46 2
    private function getName(ReflectionMethod $method, \ReflectionParameter $parameter)
47
    {
48 2
        $named = $method->getAnnotation('Ray\Di\Di\Named');
49 2
        if (! $named) {
50 1
            return Name::ANY;
51
        }
52 1
        parse_str($named->value, $names);
53 1
        $paramName = $parameter->getName();
54 1
        if (isset($names[$paramName])) {
55 1
            return $names[$paramName];
56
        }
57
58
        return Name::ANY;
59
    }
60
61
    /**
62
     * @param \ReflectionMethod      $method
63
     * @param Assisted               $assisted
64
     * @param \ReflectionParameter[] $parameters
65
     * @param array                  $arguments
66
     * @param int                    $cntArgs
67
     *
68
     * @return array
69
     */
70 2
    public function injectAssistedParameters(\ReflectionMethod $method, Assisted $assisted, array $parameters, array $arguments, $cntArgs)
0 ignored issues
show
Unused Code introduced by
The parameter $cntArgs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method parameter $cntArgs is never used
Loading history...
71
    {
72 2
        foreach ($parameters as $pos => $parameter) {
73 2
            if (! in_array($parameter->getName(), $assisted->values)) {
74 2
                continue;
75
            }
76 2
            $hint = $parameter->getClass();
77 2
            $interface = $hint ? $hint->getName() : '';
78 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...
79 2
            $arguments[] = $this->injector->getInstance($interface, $name);
80 2
        }
81
82 2
        return $arguments;
83
    }
84
}
85