Completed
Push — php7.1 ( 255d3a...f9ac5c )
by Akihito
01:56
created

ReflectiveMethodInvocation::getCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
final class ReflectiveMethodInvocation implements MethodInvocation
8
{
9
    /**
10
     * @var object
11
     */
12
    private $object;
13
14
    /**
15
     * @var array|\ArrayObject
16
     */
17
    private $arguments;
18
19
    /**
20
     * @var string
21
     */
22
    private $method;
23
24
    /**
25
     * @var MethodInterceptor[]
26
     */
27
    private $interceptors;
28
29
    /**
30
     * @var callable
31
     */
32
    private $callable;
33
34
    /**
35
     * @param object              $object
36
     * @param MethodInterceptor[] $interceptors
37
     */
38
    public function __construct(
39
        $object,
40
        string $method,
41
        array $arguments,
42
        array $interceptors = []
43
    ) {
44
        $this->object = $object;
45
        $this->method = $method;
46
        $this->callable = $this->getCallable($object, $method);
47
        $this->arguments = $arguments;
48
        $this->interceptors = $interceptors;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @throws \ReflectionException
55
     */
56
    public function getMethod() : ReflectionMethod
57
    {
58
        if ($this->object instanceof WeavedInterface) {
59
            $class = (new \ReflectionObject($this->object))->getParentClass();
60
            if (! $class instanceof \ReflectionClass) {
61
                throw new \LogicException; // @codeCoverageIgnore
62
            }
63
            $method = new ReflectionMethod($class->name, $this->method);
64
            $method->setObject($this->object, $method);
65
66
            return $method;
67
        }
68
69
        return new ReflectionMethod($this->object, $this->method);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getArguments() : \ArrayObject
76
    {
77
        $this->arguments = new \ArrayObject($this->arguments);
78
79
        return $this->arguments;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @throws \ReflectionException
86
     */
87
    public function getNamedArguments() : \ArrayObject
88
    {
89
        $args = $this->getArguments();
90
        $paramas = $this->getMethod()->getParameters();
91
        $namedParams = new \ArrayObject;
92
        foreach ($paramas as $param) {
93
            $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...
94
        }
95
96
        return $namedParams;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function proceed()
103
    {
104
        if ($this->interceptors === []) {
105
            return call_user_func_array($this->callable, (array) $this->arguments);
106
        }
107
        $interceptor = array_shift($this->interceptors);
108
        if ($interceptor instanceof MethodInterceptor) {
109
            return $interceptor->invoke($this);
110
        }
111
112
        throw new \LogicException;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getThis()
119
    {
120
        return $this->object;
121
    }
122
123
    private function getCallable($object, string $method) : callable
124
    {
125
        return [$object, $method];
126
    }
127
}
128