Completed
Pull Request — 2.x (#98)
by Akihito
03:19 queued 01:55
created

ReflectiveMethodInvocation::getCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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