Completed
Push — 2.x ( f7c839...266f66 )
by Akihito
01:45
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
c 0
b 0
f 0
dl 0
loc 11
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 array|\ArrayObject
20
     */
21
    private $arguments;
22
23
    /**
24
     * @var string
25
     */
26
    private $method;
27
28
    /**
29
     * @var MethodInterceptor[]
30
     */
31
    private $interceptors;
32
33
    /**
34
     * @param object              $object
35
     * @param string              $method
36
     * @param array               $arguments
37
     * @param MethodInterceptor[] $interceptors
38
     */
39 16
    public function __construct(
40
        $object,
41
        string $method,
42
        array $arguments,
43
        array $interceptors = []
44
    ) {
45 16
        $this->object = $object;
46 16
        $this->method = $method;
47 16
        $this->arguments = $arguments;
48 16
        $this->interceptors = $interceptors;
49 16
    }
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);
59 3
            $method->setObject($this->object, $method);
60
61 3
            return $method;
62
        }
63
64 3
        return new ReflectionMethod($this->object, $this->method);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function getArguments() : \ArrayObject
71
    {
72 3
        $this->arguments = new \ArrayObject($this->arguments);
73
74 3
        return $this->arguments;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function getNamedArguments() : \ArrayObject
81
    {
82 1
        $args = $this->getArguments();
83 1
        $paramas = $this->getMethod()->getParameters();
84 1
        $namedParams = new \ArrayObject;
85 1
        foreach ($paramas as $param) {
86 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...
87
        }
88
89 1
        return $namedParams;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 10
    public function proceed()
96
    {
97 10
        if ($this->interceptors === []) {
98 9
            return call_user_func_array([$this->object, $this->method], (array) $this->arguments);
99
        }
100 8
        $interceptor = array_shift($this->interceptors);
101
102 8
        return $interceptor->invoke($this);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1
    public function getThis()
109
    {
110 1
        return $this->object;
111
    }
112
}
113