1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Interceptors\Context; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @template-covariant TController of object|null |
9
|
|
|
* @implements TargetInterface<TController> |
10
|
|
|
*/ |
11
|
|
|
final class Target implements TargetInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param list<string> $path |
|
|
|
|
15
|
|
|
* @param \ReflectionFunctionAbstract|null $reflection |
16
|
|
|
* @param TController|null $object |
|
|
|
|
17
|
|
|
*/ |
18
|
239 |
|
private function __construct( |
19
|
|
|
private array $path, |
20
|
|
|
private ?\ReflectionFunctionAbstract $reflection = null, |
21
|
|
|
private readonly ?object $object = null, |
22
|
|
|
private string $delimiter = '.', |
23
|
|
|
) { |
24
|
239 |
|
} |
25
|
|
|
|
26
|
17 |
|
public function __toString(): string |
27
|
|
|
{ |
28
|
17 |
|
return \implode($this->delimiter, $this->path); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a target from a method reflection. |
33
|
|
|
* |
34
|
|
|
* @template T of object |
35
|
|
|
* |
36
|
|
|
* @param \ReflectionMethod $reflection |
37
|
|
|
* @param class-string<T>|T $classOrObject The original class name or object. |
|
|
|
|
38
|
|
|
* It's required because the reflection may be referring to a parent class method. |
39
|
|
|
* THe path will contain the original class name and the method name. |
40
|
|
|
* |
41
|
|
|
* @psalmif |
42
|
|
|
* |
43
|
|
|
* @return self<T> |
44
|
|
|
*/ |
45
|
158 |
|
public static function fromReflectionMethod( |
46
|
|
|
\ReflectionFunctionAbstract $reflection, |
47
|
|
|
string|object $classOrObject, |
48
|
|
|
): self { |
49
|
|
|
/** @var self<T> $result */ |
50
|
158 |
|
$result = \is_object($classOrObject) |
51
|
102 |
|
? new self( |
52
|
102 |
|
path: [$classOrObject::class, $reflection->getName()], |
53
|
102 |
|
reflection: $reflection, |
54
|
102 |
|
object: $classOrObject, |
55
|
102 |
|
delimiter: $reflection->isStatic() ? '::' : '->', |
|
|
|
|
56
|
102 |
|
) |
57
|
56 |
|
: new self( |
58
|
56 |
|
path: [$classOrObject, $reflection->getName()], |
59
|
56 |
|
reflection: $reflection, |
60
|
56 |
|
delimiter: $reflection->isStatic() ? '::' : '->', |
61
|
56 |
|
); |
62
|
158 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a target from a function reflection. |
67
|
|
|
* |
68
|
|
|
* @param list<string> $path |
69
|
|
|
* |
70
|
|
|
* @return self<null> |
71
|
|
|
*/ |
72
|
5 |
|
public static function fromReflectionFunction(\ReflectionFunction $reflection, array $path = []): self |
73
|
|
|
{ |
74
|
|
|
/** @var self<null> $result */ |
75
|
5 |
|
$result = new self(path: $path, reflection: $reflection); |
76
|
5 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create a target from a closure. |
81
|
|
|
* |
82
|
|
|
* @param list<string> $path |
83
|
|
|
* |
84
|
|
|
* @return self<null> |
85
|
|
|
*/ |
86
|
2 |
|
public static function fromClosure(\Closure $closure, array $path = []): self |
87
|
|
|
{ |
88
|
2 |
|
return self::fromReflectionFunction(new \ReflectionFunction($closure), $path); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create a target from a path string without reflection. |
93
|
|
|
* |
94
|
|
|
* @param non-empty-string $delimiter |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @return self<null> |
97
|
|
|
*/ |
98
|
7 |
|
public static function fromPathString(string $path, string $delimiter = '.'): self |
99
|
|
|
{ |
100
|
7 |
|
return self::fromPathArray(\explode($delimiter, $path), $delimiter); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a target from a path array without reflection. |
105
|
|
|
* |
106
|
|
|
* @param list<string> $path |
107
|
|
|
* @return self<null> |
108
|
|
|
*/ |
109
|
78 |
|
public static function fromPathArray(array $path, string $delimiter = '.'): self |
110
|
|
|
{ |
111
|
|
|
/** @var self<null> $result */ |
112
|
78 |
|
$result = new self(path: $path, delimiter: $delimiter); |
113
|
78 |
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create a target from a controller and action pair. |
118
|
|
|
* If the action is a method of the controller, the reflection will be set. |
119
|
|
|
* |
120
|
|
|
* @template T of object |
121
|
|
|
* |
122
|
|
|
* @param non-empty-string|class-string<T>|T $controller |
|
|
|
|
123
|
|
|
* @param non-empty-string $action |
124
|
|
|
* |
125
|
|
|
* @return ($controller is class-string|T ? self<T> : self<null>) |
|
|
|
|
126
|
|
|
*/ |
127
|
186 |
|
public static function fromPair(string|object $controller, string $action): self |
128
|
|
|
{ |
129
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
130
|
186 |
|
if (\is_object($controller) || \method_exists($controller, $action)) { |
131
|
|
|
/** @var T|class-string<T> $controller */ |
132
|
156 |
|
return self::fromReflectionMethod(new \ReflectionMethod($controller, $action), $controller); |
133
|
|
|
} |
134
|
|
|
|
135
|
32 |
|
return self::fromPathArray([$controller, $action]); |
136
|
|
|
} |
137
|
|
|
|
138
|
223 |
|
public function getPath(): array |
139
|
|
|
{ |
140
|
223 |
|
return $this->path; |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
public function withPath(array $path, ?string $delimiter = null): static |
144
|
|
|
{ |
145
|
2 |
|
$clone = clone $this; |
146
|
2 |
|
$clone->path = $path; |
|
|
|
|
147
|
2 |
|
$clone->delimiter = $delimiter ?? $clone->delimiter; |
148
|
2 |
|
return $clone; |
149
|
|
|
} |
150
|
|
|
|
151
|
50 |
|
public function getReflection(): ?\ReflectionFunctionAbstract |
152
|
|
|
{ |
153
|
50 |
|
return $this->reflection; |
154
|
|
|
} |
155
|
|
|
|
156
|
42 |
|
public function getObject(): ?object |
157
|
|
|
{ |
158
|
42 |
|
return $this->object; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths