Completed
Pull Request — 2.x (#91)
by Akihito
02:24
created

ReflectiveMethodInvocation::getMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0123
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
     * @var callable
35
     */
36
    private $callable;
37
38
    /**
39
     * @param object              $object
40
     * @param string              $method
41
     * @param array               $arguments
42
     * @param MethodInterceptor[] $interceptors
43
     */
44 17
    public function __construct(
45
        $object,
46
        string $method,
47
        array $arguments,
48
        array $interceptors = []
49
    ) {
50 17
        $this->object = $object;
51 17
        $this->method = $method;
52 17
        $callable = [$object, $method];
53 17
        if (is_callable($callable)) {
54 17
            $this->callable = $callable;
55
        }
56 17
        $this->arguments = $arguments;
57 17
        $this->interceptors = $interceptors;
58 17
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 6
    public function getMethod() : ReflectionMethod
64
    {
65 6
        if ($this->object instanceof WeavedInterface) {
66 3
            $class = (new \ReflectionObject($this->object))->getParentClass();
67 3
            if (! $class instanceof \ReflectionClass) {
68
                throw new \LogicException;
69
            }
70 3
            $method = new ReflectionMethod($class->name, $this->method);
71 3
            $method->setObject($this->object, $method);
72
73 3
            return $method;
74
        }
75
76 3
        return new ReflectionMethod($this->object, $this->method);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 3
    public function getArguments() : \ArrayObject
83
    {
84 3
        $this->arguments = new \ArrayObject($this->arguments);
85
86 3
        return $this->arguments;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function getNamedArguments() : \ArrayObject
93
    {
94 1
        $args = $this->getArguments();
95 1
        $paramas = $this->getMethod()->getParameters();
96 1
        $namedParams = new \ArrayObject;
97 1
        foreach ($paramas as $param) {
98 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...
99
        }
100
101 1
        return $namedParams;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 11
    public function proceed()
108
    {
109 11
        if ($this->interceptors === []) {
110 9
            return call_user_func_array($this->callable, (array) $this->arguments);
111
        }
112 9
        $interceptor = array_shift($this->interceptors);
113 9
        if ($interceptor instanceof MethodInterceptor) {
114 8
            return $interceptor->invoke($this);
115
        }
116 1
        throw new \LogicException;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function getThis()
123
    {
124 1
        return $this->object;
125
    }
126
}
127