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

AssistedInterceptor::injectAssistedParameters()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 9
nc 4
nop 5
crap 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