Completed
Branch 2.x (8b4efa)
by Akihito
06:27
created

ReflectiveMethodInvocation::getNamedArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Aop package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Aop;
10
11
final class ReflectiveMethodInvocation implements MethodInvocation
12
{
13
    /**
14
     * @var object
15
     */
16
    private $object;
17
18
    /**
19
     * @var Arguments
20
     */
21
    private $arguments;
22
23
    /**
24
     * @var \ReflectionMethod
25
     */
26
    private $method;
27
28
    /**
29
     * @var MethodInterceptor[]
30
     */
31
    private $interceptors;
32
33
    /**
34
     * @param object              $object
35
     * @param \ReflectionMethod   $method
36
     * @param Arguments           $arguments
37
     * @param MethodInterceptor[] $interceptors
38
     */
39 15
    public function __construct(
40
        $object,
41
        \ReflectionMethod $method,
42
        Arguments $arguments,
43
        array $interceptors = []
44
    ) {
45 15
        $this->object = $object;
46 15
        $this->method = $method;
47 15
        $this->arguments = $arguments;
48 15
        $this->interceptors = $interceptors;
49 15
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 6
    public function getMethod() : \ReflectionMethod
55
    {
56 6
        if ($this->object instanceof WeavedInterface) {
57 3
            $class = (new \ReflectionObject($this->object))->getParentClass();
58 3
            $method = new ReflectionMethod($class->name, $this->method->name);
59 3
            $method->setObject($this->object, $this->method);
60
61 3
            return $method;
62
        }
63
64 3
        return $this->method;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function getArguments() : \ArrayObject
71
    {
72 2
        return $this->arguments;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function getNamedArguments() : \ArrayObject
79
    {
80 1
        $args = $this->getArguments();
81 1
        $paramas = $this->getMethod()->getParameters();
82 1
        $namedParams = new \ArrayObject;
83 1
        foreach ($paramas as $param) {
84 1
            $namedParams[$param->getName()] = $args[$param->getPosition()];
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
85
        }
86
87 1
        return $namedParams;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 9
    public function proceed()
94
    {
95 9
        if ($this->interceptors === []) {
96 8
            return $this->method->invokeArgs($this->object, $this->arguments->getArrayCopy());
97
        }
98 7
        $interceptor = array_shift($this->interceptors);
99
        /* @var $interceptor MethodInterceptor */
100
101 7
        return $interceptor->invoke($this);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function getThis()
108
    {
109 1
        return $this->object;
110
    }
111
}
112