Completed
Push — 2.x ( 711074...57aa77 )
by Akihito
17s queued 12s
created

ReflectiveMethodInvocation::getNamedArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use ArrayObject;
8
use ReflectionClass;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ray\Aop\ReflectionClass.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use ReflectionObject;
10
11
final class ReflectiveMethodInvocation implements MethodInvocation
12
{
13
    /**
14
     * @var object
15
     */
16
    private $object;
17
18
    /**
19
     * @var ArrayObject<int, mixed>
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 array<MethodInterceptor> $interceptors
40
     * @param array<int, mixed>        $arguments
41
     */
42
    public function __construct(
43
        object $object,
44 17
        string $method,
45
        array $arguments,
46
        array $interceptors = []
47
    ) {
48
        $this->object = $object;
49
        $this->method = $method;
50 17
        $callable = [$object, $method];
51 17
        assert(is_callable($callable));
52 17
        $this->callable = $callable;
53 17
        $this->arguments = new ArrayObject($arguments);
54 17
        $this->interceptors = $interceptors;
55 17
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 6
    public function getMethod() : ReflectionMethod
61
    {
62 6
        if ($this->object instanceof WeavedInterface) {
63 3
            $class = (new ReflectionObject($this->object))->getParentClass();
64 3
            assert($class instanceof ReflectionClass);
65
            $method = new ReflectionMethod($class->name, $this->method);
66
            $method->setObject($this->object, $method);
67 3
68 3
            return $method;
69
        }
70 3
71
        return new ReflectionMethod($this->object, $this->method);
72
    }
73 3
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getArguments() : ArrayObject
78
    {
79 3
        return $this->arguments;
80
    }
81 3
82
    /**
83 3
     * {@inheritdoc}
84
     */
85
    public function getNamedArguments() : ArrayObject
86
    {
87
        $args = $this->getArguments();
88
        $params = $this->getMethod()->getParameters();
89 1
        $namedParams = [];
90
        foreach ($params as $param) {
91 1
            /** @psalm-suppress MixedAssignment */
92 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...
93 1
        }
94 1
95 1
        return new ArrayObject($namedParams);
96
    }
97
98 1
    /**
99
     * {@inheritdoc}
100
     */
101
    public function proceed()
102
    {
103
        $interceptor = array_shift($this->interceptors);
104 11
        if ($interceptor instanceof MethodInterceptor) {
105
            return $interceptor->invoke($this);
106 11
        }
107 9
108
        return call_user_func_array($this->callable, (array) $this->arguments);
109 9
    }
110 9
111 8
    /**
112
     * {@inheritdoc}
113 1
     */
114
    public function getThis()
115
    {
116
        return $this->object;
117
    }
118
}
119