ReflectiveMethodInvocation::getMethod()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
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. Consider defining an alias.

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
use function array_shift;
12
use function assert;
13
use function call_user_func_array;
14
use function class_exists;
15
use function is_callable;
16
17
final class ReflectiveMethodInvocation implements MethodInvocation
18
{
19
    /** @var object */
20
    private $object;
21
22
    /** @var ArrayObject<int, mixed> */
23
    private $arguments;
24
25
    /** @var string */
26
    private $method;
27
28
    /** @var MethodInterceptor[] */
29
    private $interceptors;
30
31
    /** @var callable */
32
    private $callable;
33
34
    /**
35
     * @param array<MethodInterceptor> $interceptors
36
     * @param array<int, mixed>        $arguments
37
     */
38
    public function __construct(
39
        object $object,
40
        string $method,
41
        array $arguments,
42
        array $interceptors = []
43
    ) {
44 17
        $this->object = $object;
45
        $this->method = $method;
46
        $callable = [$object, $method];
47
        assert(is_callable($callable));
48
        $this->callable = $callable;
49
        $this->arguments = new ArrayObject($arguments);
50 17
        $this->interceptors = $interceptors;
51 17
    }
52 17
53 17
    /**
54 17
     * {@inheritDoc}
55
     */
56 17
    public function getMethod(): ReflectionMethod
57 17
    {
58 17
        if ($this->object instanceof WeavedInterface) {
59
            $class = (new ReflectionObject($this->object))->getParentClass();
60
            assert($class instanceof ReflectionClass);
61
            assert(class_exists($class->name));
62
            $method = new ReflectionMethod($class->name, $this->method);
63 6
            $method->setObject($this->object);
64
65 6
            return $method;
66 3
        }
67 3
68
        return new ReflectionMethod($this->object, $this->method);
69
    }
70 3
71 3
    /**
72
     * {@inheritDoc}
73 3
     */
74
    public function getArguments(): ArrayObject
75
    {
76 3
        return $this->arguments;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 3
    public function getNamedArguments(): ArrayObject
83
    {
84 3
        $args = $this->getArguments();
85
        $params = $this->getMethod()->getParameters();
86 3
        $namedParams = [];
87
        foreach ($params as $param) {
88
            $pos = $param->getPosition();
89
            $name = $param->getName();
90
            /** @psalm-suppress MixedAssignment */
91
            $namedParams[$name] = $args[$pos];
92 1
        }
93
94 1
        return new ArrayObject($namedParams); // @phpstan-ignore-line
95 1
    }
96 1
97 1
    /**
98 1
     * {@inheritDoc}
99
     */
100
    public function proceed()
101 1
    {
102
        $interceptor = array_shift($this->interceptors);
103
        if ($interceptor instanceof MethodInterceptor) {
104
            return $interceptor->invoke($this);
105
        }
106
107 11
        return call_user_func_array($this->callable, (array) $this->arguments);
108
    }
109 11
110 9
    /**
111
     * {@inheritDoc}
112 9
     */
113 9
    public function getThis()
114 8
    {
115
        return $this->object;
116 1
    }
117
}
118