1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
10
|
|
|
use ReflectionFunctionAbstract as ContextFunction; |
11
|
|
|
use ReflectionIntersectionType; |
|
|
|
|
12
|
|
|
use ReflectionNamedType; |
13
|
|
|
use ReflectionParameter; |
14
|
|
|
use ReflectionUnionType; |
15
|
|
|
use Spiral\Core\Attribute\Proxy as ProxyAttribute; |
16
|
|
|
use Spiral\Core\Container\Autowire; |
17
|
|
|
use Spiral\Core\Exception\Resolver\ArgumentResolvingException; |
18
|
|
|
use Spiral\Core\Exception\Resolver\InvalidArgumentException; |
19
|
|
|
use Spiral\Core\Exception\Resolver\MissingRequiredArgumentException; |
20
|
|
|
use Spiral\Core\Exception\Resolver\PositionalArgumentException; |
21
|
|
|
use Spiral\Core\Exception\Resolver\ResolvingException; |
22
|
|
|
use Spiral\Core\Exception\Resolver\UnknownParameterException; |
23
|
|
|
use Spiral\Core\Exception\Resolver\UnsupportedTypeException; |
24
|
|
|
use Spiral\Core\FactoryInterface; |
25
|
|
|
use Spiral\Core\Internal\Common\DestructorTrait; |
26
|
|
|
use Spiral\Core\Internal\Common\Registry; |
27
|
|
|
use Spiral\Core\Internal\Resolver\ResolvingState; |
28
|
|
|
use Spiral\Core\ResolverInterface; |
29
|
|
|
use Throwable; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @internal |
33
|
|
|
*/ |
34
|
|
|
final class Resolver implements ResolverInterface |
35
|
|
|
{ |
36
|
|
|
use DestructorTrait; |
37
|
|
|
|
38
|
|
|
private FactoryInterface $factory; |
39
|
|
|
private ContainerInterface $container; |
40
|
|
|
|
41
|
1308 |
|
public function __construct(Registry $constructor) |
42
|
|
|
{ |
43
|
1308 |
|
$constructor->set('resolver', $this); |
44
|
|
|
|
45
|
1308 |
|
$this->factory = $constructor->get('factory', FactoryInterface::class); |
46
|
1308 |
|
$this->container = $constructor->get('container', ContainerInterface::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
1012 |
|
public function resolveArguments( |
50
|
|
|
ContextFunction $reflection, |
51
|
|
|
array $parameters = [], |
52
|
|
|
bool $validate = true, |
53
|
|
|
): array { |
54
|
1012 |
|
$state = new ResolvingState($reflection, $parameters); |
55
|
|
|
|
56
|
1012 |
|
foreach ($reflection->getParameters() as $parameter) { |
57
|
988 |
|
$this->resolveParameter($parameter, $state, $validate) |
58
|
988 |
|
or |
59
|
988 |
|
throw new ArgumentResolvingException($reflection, $parameter->getName()); |
60
|
|
|
} |
61
|
|
|
|
62
|
974 |
|
return $state->getResolvedValues(); |
63
|
|
|
} |
64
|
|
|
|
65
|
37 |
|
public function validateArguments(ContextFunction $reflection, array $arguments = []): void |
66
|
|
|
{ |
67
|
37 |
|
$positional = true; |
68
|
37 |
|
$variadic = false; |
69
|
37 |
|
$parameters = $reflection->getParameters(); |
70
|
37 |
|
if (\count($parameters) === 0) { |
71
|
2 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
35 |
|
$parameter = null; |
75
|
35 |
|
while (\count($parameters) > 0 || \count($arguments) > 0) { |
76
|
|
|
// get related argument value |
77
|
35 |
|
$key = \key($arguments); |
78
|
|
|
|
79
|
|
|
// For a variadic parameter it's no sense - named or positional argument will be sent |
80
|
|
|
// But you can't send positional argument after named in any case |
81
|
35 |
|
if (\is_int($key) && !$positional) { |
82
|
2 |
|
throw new PositionalArgumentException($reflection, $key); |
83
|
|
|
} |
84
|
|
|
|
85
|
35 |
|
$positional = $positional && \is_int($key); |
86
|
|
|
|
87
|
35 |
|
if (!$variadic) { |
88
|
35 |
|
$parameter = \array_shift($parameters); |
89
|
35 |
|
$variadic = $parameter?->isVariadic() ?? false; |
90
|
|
|
} |
91
|
|
|
|
92
|
35 |
|
if ($parameter === null) { |
93
|
1 |
|
throw new UnknownParameterException($reflection, $key); |
94
|
|
|
} |
95
|
35 |
|
$name = $parameter->getName(); |
96
|
|
|
|
97
|
35 |
|
if (($positional || $variadic) && $key !== null) { |
98
|
|
|
/** @psalm-suppress ReferenceReusedFromConfusingScope */ |
99
|
32 |
|
$value = \array_shift($arguments); |
100
|
9 |
|
} elseif ($key === null || !\array_key_exists($name, $arguments)) { |
101
|
4 |
|
if ($parameter->isOptional()) { |
102
|
3 |
|
continue; |
103
|
|
|
} |
104
|
1 |
|
throw new MissingRequiredArgumentException($reflection, $name); |
105
|
|
|
} else { |
106
|
6 |
|
$value = &$arguments[$name]; |
107
|
6 |
|
unset($arguments[$name]); |
108
|
|
|
} |
109
|
|
|
|
110
|
34 |
|
if (!$this->validateValueToParameter($parameter, $value)) { |
111
|
6 |
|
throw new InvalidArgumentException($reflection, $name); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
967 |
|
private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
117
|
|
|
{ |
118
|
967 |
|
if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
119
|
100 |
|
return true; |
120
|
|
|
} |
121
|
963 |
|
$type = $parameter->getType(); |
122
|
|
|
|
123
|
963 |
|
[$or, $types] = match (true) { |
124
|
963 |
|
$type instanceof ReflectionNamedType => [true, [$type]], |
125
|
963 |
|
$type instanceof ReflectionUnionType => [true, $type->getTypes()], |
|
|
|
|
126
|
963 |
|
$type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
127
|
963 |
|
}; |
128
|
|
|
|
129
|
963 |
|
foreach ($types as $t) { |
130
|
963 |
|
\assert($t instanceof ReflectionNamedType); |
131
|
963 |
|
if (!$this->validateValueNamedType($t, $value)) { |
132
|
|
|
// If it is TypeIntersection |
133
|
20 |
|
if ($or) { |
134
|
19 |
|
continue; |
135
|
|
|
} |
136
|
1 |
|
return false; |
137
|
|
|
} |
138
|
|
|
// If it is not type intersection then we can skip that value after first successful check |
139
|
951 |
|
if ($or) { |
140
|
947 |
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
20 |
|
return !$or; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Validate the value have the same type that in the $type. |
148
|
|
|
* This method doesn't resolve cases with nullable type and {@see null} value. |
149
|
|
|
*/ |
150
|
963 |
|
private function validateValueNamedType(ReflectionNamedType $type, mixed $value): bool |
151
|
|
|
{ |
152
|
963 |
|
$name = $type->getName(); |
153
|
|
|
|
154
|
963 |
|
if ($type->isBuiltin()) { |
155
|
512 |
|
return match ($name) { |
156
|
512 |
|
'mixed' => true, |
157
|
512 |
|
'string' => \is_string($value), |
158
|
512 |
|
'int' => \is_int($value), |
159
|
512 |
|
'bool' => \is_bool($value), |
160
|
512 |
|
'array' => \is_array($value), |
161
|
512 |
|
'callable' => \is_callable($value), |
162
|
512 |
|
'iterable' => \is_iterable($value), |
163
|
512 |
|
'float' => \is_float($value), |
164
|
512 |
|
'object' => \is_object($value), |
165
|
512 |
|
'true' => $value === true, |
166
|
512 |
|
'false' => $value === false, |
167
|
512 |
|
default => false, |
168
|
512 |
|
}; |
169
|
|
|
} |
170
|
|
|
|
171
|
906 |
|
return $value instanceof $name; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns {@see true} if argument was resolved. |
176
|
|
|
* |
177
|
|
|
* @throws ResolvingException |
178
|
|
|
* @throws NotFoundExceptionInterface|ContainerExceptionInterface |
179
|
|
|
*/ |
180
|
988 |
|
private function resolveParameter(ReflectionParameter $param, ResolvingState $state, bool $validate): bool |
181
|
|
|
{ |
182
|
988 |
|
$isVariadic = $param->isVariadic(); |
183
|
988 |
|
$hasType = $param->hasType(); |
184
|
|
|
|
185
|
|
|
// Try to resolve parameter by name |
186
|
988 |
|
$res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
187
|
987 |
|
if ($res !== [] || $isVariadic) { |
188
|
|
|
// validate |
189
|
518 |
|
if ($isVariadic) { |
190
|
15 |
|
foreach ($res as $k => &$v) { |
191
|
13 |
|
$this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
192
|
|
|
} |
193
|
|
|
} else { |
194
|
505 |
|
$this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
195
|
|
|
} |
196
|
|
|
|
197
|
512 |
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
940 |
|
$error = null; |
201
|
940 |
|
while ($hasType) { |
202
|
|
|
/** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $refType */ |
203
|
937 |
|
$refType = $param->getType(); |
204
|
|
|
|
205
|
937 |
|
if ($refType::class === ReflectionNamedType::class) { |
206
|
934 |
|
if ($refType->isBuiltin()) { |
207
|
454 |
|
break; |
208
|
|
|
} |
209
|
|
|
|
210
|
915 |
|
if (\interface_exists($refType->getName()) && !empty($param->getAttributes(ProxyAttribute::class))) { |
|
|
|
|
211
|
13 |
|
$proxy = Proxy::create(new \ReflectionClass($refType->getName()), $param); |
212
|
13 |
|
$this->processArgument($state, $proxy); |
213
|
13 |
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
try { |
217
|
906 |
|
if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
|
|
|
218
|
885 |
|
return true; |
219
|
|
|
} |
220
|
658 |
|
} catch (Throwable $e) { |
221
|
658 |
|
$error = $e; |
222
|
|
|
} |
223
|
658 |
|
break; |
224
|
|
|
} |
225
|
|
|
|
226
|
3 |
|
if ($refType::class === ReflectionUnionType::class) { |
227
|
2 |
|
foreach ($refType->getTypes() as $namedType) { |
|
|
|
|
228
|
|
|
try { |
229
|
2 |
|
if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
230
|
2 |
|
return true; |
231
|
|
|
} |
232
|
|
|
} catch (Throwable $e) { |
233
|
|
|
$error = $e; |
234
|
|
|
} |
235
|
|
|
} |
236
|
1 |
|
break; |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
240
|
|
|
} |
241
|
|
|
|
242
|
732 |
|
if ($param->isDefaultValueAvailable()) { |
243
|
699 |
|
$argument = $param->getDefaultValue(); |
244
|
699 |
|
$this->processArgument($state, $argument); |
245
|
699 |
|
return true; |
246
|
|
|
} |
247
|
|
|
|
248
|
35 |
|
if ($hasType && $param->allowsNull()) { |
249
|
6 |
|
$argument = null; |
250
|
6 |
|
$this->processArgument($state, $argument); |
251
|
6 |
|
return true; |
252
|
|
|
} |
253
|
|
|
|
254
|
29 |
|
if ($error === null) { |
255
|
10 |
|
return false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Throw NotFoundExceptionInterface |
259
|
22 |
|
throw $error; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Resolve argument by class name and context. Returns {@see true} if argument resolved. |
264
|
|
|
* |
265
|
|
|
* @throws ContainerExceptionInterface |
266
|
|
|
* @throws NotFoundExceptionInterface |
267
|
|
|
*/ |
268
|
907 |
|
private function resolveObject( |
269
|
|
|
ResolvingState $state, |
270
|
|
|
ReflectionNamedType $type, |
271
|
|
|
ReflectionParameter $parameter, |
272
|
|
|
bool $validateWith = false, |
273
|
|
|
): bool { |
274
|
|
|
/** @psalm-suppress TooManyArguments */ |
275
|
907 |
|
$argument = $this->container->get($type->getName(), $parameter); |
|
|
|
|
276
|
888 |
|
$this->processArgument($state, $argument, $validateWith ? $parameter : null); |
277
|
886 |
|
return true; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Arguments processing. {@see Autowire} object will be resolved. |
282
|
|
|
* |
283
|
|
|
* @param mixed $value Resolved value. |
284
|
|
|
* @param ReflectionParameter|null $validateWith Should be passed when the value should be validated. |
285
|
|
|
* Must be set for when value is user's argument. |
286
|
|
|
* @param int|string|null $key Only {@see string} values will be preserved. |
287
|
|
|
*/ |
288
|
965 |
|
private function processArgument( |
289
|
|
|
ResolvingState $state, |
290
|
|
|
mixed &$value, |
291
|
|
|
ReflectionParameter $validateWith = null, |
292
|
|
|
int|string $key = null |
293
|
|
|
): void { |
294
|
|
|
// Resolve Autowire objects |
295
|
965 |
|
if ($value instanceof Autowire) { |
296
|
2 |
|
$value = $value->resolve($this->factory); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
// Validation |
300
|
965 |
|
if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
301
|
12 |
|
throw new InvalidArgumentException( |
302
|
12 |
|
$validateWith->getDeclaringFunction(), |
303
|
12 |
|
$validateWith->getName() |
304
|
12 |
|
); |
305
|
|
|
} |
306
|
|
|
|
307
|
957 |
|
$state->addResolvedValue($value, \is_string($key) ? $key : null); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
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