Completed
Pull Request — 2.x (#139)
by Akihito
02:52
created

AssistedInterceptor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.43%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 52
ccs 27
cts 28
cp 0.9643
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 14 3
A invoke() 0 21 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 Doctrine\Common\Annotations\Reader;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
13
final class AssistedInterceptor implements MethodInterceptor
14
{
15
    private $injector;
16
    private $reader;
17
18 2
    public function __construct(InjectorInterface $injector, Reader $reader)
19
    {
20 2
        $this->injector = $injector;
21 2
        $this->reader = $reader;
22 2
    }
23
24
    /**
25
     * Intercepts any method and injects instances of the missing arguments
26
     * when they are type hinted
27
     */
28 2
    public function invoke(MethodInvocation $invocation)
29
    {
30 2
        $method = $invocation->getMethod();
31 2
        $parameters = $method->getParameters();
32 2
        $arguments = $invocation->getArguments()->getArrayCopy();
33 2
        $cntArgs = count($arguments);
34 2
        $assisted = [];
0 ignored issues
show
Unused Code introduced by
$assisted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
36 2
        foreach ($parameters as $pos => $parameter) {
37 2
            if ($pos < $cntArgs) {
38 2
                continue;
39
            }
40 2
            $hint = $parameter->getClass();
41 2
            $interface = $hint ? $hint->getName() : '';
42 2
            $name = $this->getName($method, $parameter);
43 2
            $arguments[] = $this->injector->getInstance($interface, $name);
44
        }
45 2
        $invocation->getArguments()->exchangeArray($arguments);
46
47 2
        return $invocation->proceed();
48
    }
49
50 2
    private function getName(\ReflectionMethod $method, \ReflectionParameter $parameter)
51
    {
52 2
        $named = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\Named');
53 2
        if (! $named) {
54 1
            return Name::ANY;
55
        }
56 1
        parse_str($named->value, $names);
57 1
        $paramName = $parameter->getName();
58 1
        if (isset($names[$paramName])) {
59 1
            return $names[$paramName];
60
        }
61
62
        return Name::ANY;
63
    }
64
}