1 | <?php |
||
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 | 17 | */ |
|
56 | public function getMethod(): ReflectionMethod |
||
57 | { |
||
58 | if ($this->object instanceof WeavedInterface) { |
||
59 | $class = (new ReflectionObject($this->object))->getParentClass(); |
||
60 | 6 | assert($class instanceof ReflectionClass); |
|
61 | assert(class_exists($class->name)); |
||
62 | 6 | $method = new ReflectionMethod($class->name, $this->method); |
|
63 | 3 | $method->setObject($this->object, $method); |
|
64 | 3 | ||
65 | return $method; |
||
66 | } |
||
67 | 3 | ||
68 | 3 | return new ReflectionMethod($this->object, $this->method); |
|
69 | } |
||
70 | 3 | ||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | 3 | */ |
|
74 | public function getArguments(): ArrayObject |
||
75 | { |
||
76 | return $this->arguments; |
||
77 | } |
||
78 | |||
79 | 3 | /** |
|
80 | * {@inheritdoc} |
||
81 | 3 | */ |
|
82 | public function getNamedArguments(): ArrayObject |
||
83 | 3 | { |
|
84 | $args = $this->getArguments(); |
||
85 | $params = $this->getMethod()->getParameters(); |
||
86 | $namedParams = []; |
||
87 | foreach ($params as $param) { |
||
88 | $pos = $param->getPosition(); |
||
89 | 1 | if (isset($args[$pos])) { |
|
90 | /** @psalm-suppress MixedAssignment */ |
||
91 | 1 | $namedParams[$param->getName()] = $args[$pos]; |
|
92 | 1 | } |
|
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() |
||
118 | } |
||
119 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: