@@ -6,5 +6,5 @@ |
||
6 | 6 | |
7 | 7 | class EnumService |
8 | 8 | { |
9 | - public function __construct(public EnumObject $enum) {} |
|
9 | + public function __construct(public EnumObject $enum){} |
|
10 | 10 | } |
@@ -6,5 +6,7 @@ |
||
6 | 6 | |
7 | 7 | class EnumService |
8 | 8 | { |
9 | - public function __construct(public EnumObject $enum) {} |
|
9 | + public function __construct(public EnumObject $enum) |
|
10 | + { |
|
11 | +} |
|
10 | 12 | } |
@@ -8,5 +8,5 @@ |
||
8 | 8 | { |
9 | 9 | public function __construct( |
10 | 10 | private object $object = new \stdClass(), |
11 | - ) {} |
|
11 | + ){} |
|
12 | 12 | } |
@@ -8,5 +8,6 @@ |
||
8 | 8 | { |
9 | 9 | public function __construct( |
10 | 10 | private object $object = new \stdClass(), |
11 | - ) {} |
|
11 | + ) { |
|
12 | +} |
|
12 | 13 | } |
@@ -4,9 +4,9 @@ |
||
4 | 4 | |
5 | 5 | namespace Spiral\Framework; |
6 | 6 | |
7 | -require_once __DIR__ . '/../vendor/autoload.php'; |
|
7 | +require_once __DIR__.'/../vendor/autoload.php'; |
|
8 | 8 | |
9 | -if (!\enum_exists(Spiral::class)) { |
|
9 | +if (!\enum_exists(Spiral::class)){ |
|
10 | 10 | enum Spiral: string |
11 | 11 | { |
12 | 12 | case HttpRequest = 'http-request'; |
@@ -6,7 +6,8 @@ |
||
6 | 6 | |
7 | 7 | require_once __DIR__ . '/../vendor/autoload.php'; |
8 | 8 | |
9 | -if (!\enum_exists(Spiral::class)) { |
|
9 | +if (!\enum_exists(Spiral::class)) |
|
10 | +{ |
|
10 | 11 | enum Spiral: string |
11 | 12 | { |
12 | 13 | case HttpRequest = 'http-request'; |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | { |
67 | 67 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
68 | 68 | |
69 | - $result = $this->container->invoke(Storage::class . '::createBucket', ['name' => 'bar']); |
|
69 | + $result = $this->container->invoke(Storage::class.'::createBucket', ['name' => 'bar']); |
|
70 | 70 | |
71 | 71 | self::assertSame($bucket, $result['bucket']); |
72 | 72 | self::assertInstanceOf(SampleClass::class, $result['class']); |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | $this->expectException(ArgumentResolvingException::class); |
80 | 80 | $this->expectExceptionMessage('Unable to resolve required argument `name` when resolving'); |
81 | 81 | |
82 | - $this->container->invoke(Storage::class . '::createBucket', ['name' => 'bar']); |
|
82 | + $this->container->invoke(Storage::class.'::createBucket', ['name' => 'bar']); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | public function testCallValidClosure(): void |
@@ -20,7 +20,7 @@ |
||
20 | 20 | } |
21 | 21 | |
22 | 22 | #[DataProvider('scopeNameDataProvider')] |
23 | - public function testScope(string|\BackedEnum $name, string $expected): void |
|
23 | + public function testScope(string | \BackedEnum $name, string $expected): void |
|
24 | 24 | { |
25 | 25 | $scope = new Scope($name); |
26 | 26 |
@@ -65,19 +65,19 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @throws \Throwable |
67 | 67 | */ |
68 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
68 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
69 | 69 | { |
70 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
70 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
71 | 71 | return $this->state->singletons[$alias]; |
72 | 72 | } |
73 | 73 | |
74 | 74 | $binding = $this->state->bindings[$alias] ?? null; |
75 | 75 | |
76 | - if ($binding === null) { |
|
76 | + if ($binding === null){ |
|
77 | 77 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
78 | 78 | } |
79 | 79 | |
80 | - try { |
|
80 | + try{ |
|
81 | 81 | $this->tracer->push( |
82 | 82 | false, |
83 | 83 | action: 'resolve from binding', |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
108 | 108 | default => $binding, |
109 | 109 | }; |
110 | - } finally { |
|
110 | + }finally{ |
|
111 | 111 | $this->state->bindings[$alias] ??= $binding; |
112 | 112 | $this->tracer->pop(true); |
113 | 113 | $this->tracer->pop(false); |
@@ -121,18 +121,18 @@ discard block |
||
121 | 121 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
122 | 122 | { |
123 | 123 | $context = $ctx->context; |
124 | - try { |
|
124 | + try{ |
|
125 | 125 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
126 | - } catch (\ReflectionException $e) { |
|
126 | + }catch (\ReflectionException $e){ |
|
127 | 127 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
128 | 128 | } |
129 | 129 | |
130 | 130 | $injector = $binding->injector; |
131 | 131 | |
132 | - try { |
|
132 | + try{ |
|
133 | 133 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
134 | 134 | |
135 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
135 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
136 | 136 | throw new InjectionException( |
137 | 137 | \sprintf( |
138 | 138 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | static $cache = []; |
147 | 147 | $extended = $cache[$injectorInstance::class] ??= ( |
148 | 148 | static fn(\ReflectionType $type): bool => |
149 | - $type::class === \ReflectionUnionType::class || (string) $type === 'mixed' |
|
149 | + $type::class === \ReflectionUnionType::class || (string)$type === 'mixed' |
|
150 | 150 | )( |
151 | 151 | ($refMethod = new \ReflectionMethod($injectorInstance, 'createInjection')) |
152 | 152 | ->getParameters()[1]->getType() |
@@ -156,10 +156,10 @@ discard block |
||
156 | 156 | $instance = $injectorInstance->createInjection($reflection, match (true) { |
157 | 157 | $asIs => $context, |
158 | 158 | $context instanceof \ReflectionParameter => $context->getName(), |
159 | - default => (string) $context, |
|
159 | + default => (string)$context, |
|
160 | 160 | }); |
161 | 161 | |
162 | - if (!$reflection->isInstance($instance)) { |
|
162 | + if (!$reflection->isInstance($instance)){ |
|
163 | 163 | throw new InjectionException( |
164 | 164 | \sprintf( |
165 | 165 | "Invalid injection response for '%s'.", |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | } |
170 | 170 | |
171 | 171 | return $instance; |
172 | - } finally { |
|
172 | + }finally{ |
|
173 | 173 | $this->state->bindings[$ctx->class] ??= $binding; |
174 | 174 | } |
175 | 175 | } |
@@ -177,27 +177,27 @@ discard block |
||
177 | 177 | private function resolveAlias( |
178 | 178 | Config\Alias $binding, |
179 | 179 | string $alias, |
180 | - \Stringable|string|null $context, |
|
180 | + \Stringable | string | null $context, |
|
181 | 181 | array $arguments, |
182 | 182 | ): mixed { |
183 | 183 | $result = $binding->alias === $alias |
184 | 184 | ? $this->autowire( |
185 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
185 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
186 | 186 | $arguments, |
187 | 187 | ) |
188 | 188 | //Binding is pointing to something else |
189 | 189 | : $this->make($binding->alias, $arguments, $context); |
190 | 190 | |
191 | - if ($binding->singleton && $arguments === []) { |
|
191 | + if ($binding->singleton && $arguments === []){ |
|
192 | 192 | $this->state->singletons[$alias] = $result; |
193 | 193 | } |
194 | 194 | |
195 | 195 | return $result; |
196 | 196 | } |
197 | 197 | |
198 | - private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
|
198 | + private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable | string | null $context): mixed |
|
199 | 199 | { |
200 | - if ($context instanceof RetryContext) { |
|
200 | + if ($context instanceof RetryContext){ |
|
201 | 201 | return $binding->fallbackFactory === null |
202 | 202 | ? throw new RecursiveProxyException( |
203 | 203 | $alias, |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | |
209 | 209 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
210 | 210 | |
211 | - if ($binding->singleton) { |
|
211 | + if ($binding->singleton){ |
|
212 | 212 | $this->state->singletons[$alias] = $result; |
213 | 213 | } |
214 | 214 | |
@@ -218,19 +218,19 @@ discard block |
||
218 | 218 | private function resolveShared( |
219 | 219 | Config\Shared $binding, |
220 | 220 | string $alias, |
221 | - \Stringable|string|null $context, |
|
221 | + \Stringable | string | null $context, |
|
222 | 222 | array $arguments, |
223 | 223 | ): object { |
224 | 224 | $avoidCache = $arguments !== []; |
225 | 225 | |
226 | - if ($avoidCache) { |
|
226 | + if ($avoidCache){ |
|
227 | 227 | return $this->createInstance( |
228 | 228 | new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
229 | 229 | $arguments, |
230 | 230 | ); |
231 | 231 | } |
232 | 232 | |
233 | - if ($binding->singleton) { |
|
233 | + if ($binding->singleton){ |
|
234 | 234 | $this->state->singletons[$alias] = $binding->value; |
235 | 235 | } |
236 | 236 | |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | private function resolveAutowire( |
241 | 241 | Config\Autowire $binding, |
242 | 242 | string $alias, |
243 | - \Stringable|string|null $context, |
|
243 | + \Stringable | string | null $context, |
|
244 | 244 | array $arguments, |
245 | 245 | ): mixed { |
246 | 246 | $target = $binding->autowire->alias; |
@@ -254,17 +254,17 @@ discard block |
||
254 | 254 | } |
255 | 255 | |
256 | 256 | private function resolveFactory( |
257 | - Config\Factory|Config\DeferredFactory $binding, |
|
257 | + Config\Factory | Config\DeferredFactory $binding, |
|
258 | 258 | string $alias, |
259 | - \Stringable|string|null $context, |
|
259 | + \Stringable | string | null $context, |
|
260 | 260 | array $arguments, |
261 | 261 | ): mixed { |
262 | 262 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
263 | - try { |
|
263 | + try{ |
|
264 | 264 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
265 | 265 | ? ($binding->factory)() |
266 | 266 | : $this->invoker->invoke($binding->factory, $arguments); |
267 | - } catch (NotCallableException $e) { |
|
267 | + }catch (NotCallableException $e){ |
|
268 | 268 | throw new ContainerException( |
269 | 269 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
270 | 270 | $e->getCode(), |
@@ -278,24 +278,24 @@ discard block |
||
278 | 278 | private function resolveWeakReference( |
279 | 279 | Config\WeakReference $binding, |
280 | 280 | string $alias, |
281 | - \Stringable|string|null $context, |
|
281 | + \Stringable | string | null $context, |
|
282 | 282 | array $arguments, |
283 | 283 | ): ?object { |
284 | 284 | $avoidCache = $arguments !== []; |
285 | 285 | |
286 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
287 | - try { |
|
286 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
287 | + try{ |
|
288 | 288 | $this->tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
289 | 289 | |
290 | 290 | $object = $this->createInstance( |
291 | 291 | new Ctx(alias: $alias, class: $alias, context: $context), |
292 | 292 | $arguments, |
293 | 293 | ); |
294 | - if ($avoidCache) { |
|
294 | + if ($avoidCache){ |
|
295 | 295 | return $object; |
296 | 296 | } |
297 | 297 | $binding->reference = \WeakReference::create($object); |
298 | - } catch (\Throwable) { |
|
298 | + }catch (\Throwable){ |
|
299 | 299 | throw new ContainerException( |
300 | 300 | $this->tracer->combineTraceMessage( |
301 | 301 | \sprintf( |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | ), |
306 | 306 | ), |
307 | 307 | ); |
308 | - } finally { |
|
308 | + }finally{ |
|
309 | 309 | $this->tracer->pop(); |
310 | 310 | } |
311 | 311 | } |
@@ -316,23 +316,23 @@ discard block |
||
316 | 316 | private function resolveWithoutBinding( |
317 | 317 | string $alias, |
318 | 318 | array $parameters = [], |
319 | - \Stringable|string|null $context = null, |
|
319 | + \Stringable | string | null $context = null, |
|
320 | 320 | ): mixed { |
321 | 321 | $parent = $this->scope->getParentFactory(); |
322 | 322 | |
323 | - if ($parent !== null) { |
|
324 | - try { |
|
323 | + if ($parent !== null){ |
|
324 | + try{ |
|
325 | 325 | $this->tracer->push(false, ...[ |
326 | 326 | 'current scope' => $this->scope->getScopeName(), |
327 | 327 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
328 | 328 | ]); |
329 | 329 | /** @psalm-suppress TooManyArguments */ |
330 | 330 | return $parent->make($alias, $parameters, $context); |
331 | - } catch (BadScopeException $e) { |
|
332 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
331 | + }catch (BadScopeException $e){ |
|
332 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
333 | 333 | throw $e; |
334 | 334 | } |
335 | - } catch (ContainerExceptionInterface $e) { |
|
335 | + }catch (ContainerExceptionInterface $e){ |
|
336 | 336 | $className = match (true) { |
337 | 337 | $e instanceof NotFoundException => NotFoundException::class, |
338 | 338 | $e instanceof RecursiveProxyException => throw $e, |
@@ -342,19 +342,19 @@ discard block |
||
342 | 342 | 'Can\'t resolve `%s`.', |
343 | 343 | $alias, |
344 | 344 | )), previous: $e); |
345 | - } finally { |
|
345 | + }finally{ |
|
346 | 346 | $this->tracer->pop(false); |
347 | 347 | } |
348 | 348 | } |
349 | 349 | |
350 | 350 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
351 | - try { |
|
351 | + try{ |
|
352 | 352 | //No direct instructions how to construct class, make is automatically |
353 | 353 | return $this->autowire( |
354 | 354 | new Ctx(alias: $alias, class: $alias, context: $context), |
355 | 355 | $parameters, |
356 | 356 | ); |
357 | - } finally { |
|
357 | + }finally{ |
|
358 | 358 | $this->tracer->pop(false); |
359 | 359 | } |
360 | 360 | } |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | && |
376 | 376 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
377 | 377 | )) |
378 | - ) { |
|
378 | + ){ |
|
379 | 379 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
380 | 380 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
381 | 381 | $this->tracer->getRootAlias(), |
@@ -401,13 +401,13 @@ discard block |
||
401 | 401 | Ctx $ctx, |
402 | 402 | array $arguments, |
403 | 403 | ): object { |
404 | - if ($this->options->checkScope) { |
|
404 | + if ($this->options->checkScope){ |
|
405 | 405 | // Check scope name |
406 | 406 | $ctx->reflection = new \ReflectionClass($instance); |
407 | 407 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
408 | - if ($scopeName !== null) { |
|
408 | + if ($scopeName !== null){ |
|
409 | 409 | $scope = $this->scope; |
410 | - while ($scope->getScopeName() !== $scopeName) { |
|
410 | + while ($scope->getScopeName() !== $scopeName){ |
|
411 | 411 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
412 | 412 | } |
413 | 413 | } |
@@ -436,26 +436,26 @@ discard block |
||
436 | 436 | array $arguments, |
437 | 437 | ): object { |
438 | 438 | $class = $ctx->class; |
439 | - try { |
|
439 | + try{ |
|
440 | 440 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
441 | - } catch (\ReflectionException $e) { |
|
441 | + }catch (\ReflectionException $e){ |
|
442 | 442 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
443 | 443 | } |
444 | 444 | |
445 | 445 | // Check scope name |
446 | - if ($this->options->checkScope) { |
|
446 | + if ($this->options->checkScope){ |
|
447 | 447 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
448 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
448 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
449 | 449 | throw new BadScopeException($scope, $class); |
450 | 450 | } |
451 | 451 | } |
452 | 452 | |
453 | 453 | // We have to construct class using external injector when we know the exact context |
454 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
454 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
455 | 455 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
456 | 456 | } |
457 | 457 | |
458 | - if (!$reflection->isInstantiable()) { |
|
458 | + if (!$reflection->isInstantiable()){ |
|
459 | 459 | $itIs = match (true) { |
460 | 460 | $reflection->isEnum() => 'Enum', |
461 | 461 | $reflection->isAbstract() => 'Abstract class', |
@@ -468,12 +468,12 @@ discard block |
||
468 | 468 | |
469 | 469 | $constructor = $reflection->getConstructor(); |
470 | 470 | |
471 | - if ($constructor !== null) { |
|
472 | - try { |
|
471 | + if ($constructor !== null){ |
|
472 | + try{ |
|
473 | 473 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
474 | 474 | $this->tracer->push(true); |
475 | 475 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
476 | - } catch (ValidationException $e) { |
|
476 | + }catch (ValidationException $e){ |
|
477 | 477 | throw new ContainerException( |
478 | 478 | $this->tracer->combineTraceMessage( |
479 | 479 | \sprintf( |
@@ -483,22 +483,22 @@ discard block |
||
483 | 483 | ), |
484 | 484 | ), |
485 | 485 | ); |
486 | - } finally { |
|
486 | + }finally{ |
|
487 | 487 | $this->tracer->pop(true); |
488 | 488 | $this->tracer->pop(false); |
489 | 489 | } |
490 | - try { |
|
490 | + try{ |
|
491 | 491 | // Using constructor with resolved arguments |
492 | 492 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
493 | 493 | $this->tracer->push(true); |
494 | 494 | $instance = new $class(...$args); |
495 | - } catch (\TypeError $e) { |
|
495 | + }catch (\TypeError $e){ |
|
496 | 496 | throw new WrongTypeException($constructor, $e); |
497 | - } finally { |
|
497 | + }finally{ |
|
498 | 498 | $this->tracer->pop(true); |
499 | 499 | $this->tracer->pop(false); |
500 | 500 | } |
501 | - } else { |
|
501 | + }else{ |
|
502 | 502 | // No constructor specified |
503 | 503 | $instance = $reflection->newInstance(); |
504 | 504 | } |
@@ -516,13 +516,13 @@ discard block |
||
516 | 516 | $instance = $this->runInflector($instance); |
517 | 517 | |
518 | 518 | //Declarative singletons |
519 | - if ($this->isSingleton($ctx)) { |
|
519 | + if ($this->isSingleton($ctx)){ |
|
520 | 520 | $this->state->singletons[$ctx->alias] = $instance; |
521 | 521 | } |
522 | 522 | |
523 | 523 | // Register finalizer |
524 | 524 | $finalizer = $this->getFinalizer($ctx, $instance); |
525 | - if ($finalizer !== null) { |
|
525 | + if ($finalizer !== null){ |
|
526 | 526 | $this->state->finalizers[] = $finalizer; |
527 | 527 | } |
528 | 528 | |
@@ -534,12 +534,12 @@ discard block |
||
534 | 534 | */ |
535 | 535 | private function isSingleton(Ctx $ctx): bool |
536 | 536 | { |
537 | - if ($ctx->singleton === true) { |
|
537 | + if ($ctx->singleton === true){ |
|
538 | 538 | return true; |
539 | 539 | } |
540 | 540 | |
541 | 541 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
542 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
542 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
543 | 543 | return true; |
544 | 544 | } |
545 | 545 | |
@@ -553,7 +553,7 @@ discard block |
||
553 | 553 | * @var Attribute\Finalize|null $attribute |
554 | 554 | */ |
555 | 555 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
556 | - if ($attribute === null) { |
|
556 | + if ($attribute === null){ |
|
557 | 557 | return null; |
558 | 558 | } |
559 | 559 | |
@@ -567,10 +567,10 @@ discard block |
||
567 | 567 | { |
568 | 568 | $scope = $this->scope; |
569 | 569 | |
570 | - while ($scope !== null) { |
|
571 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
572 | - if ($instance instanceof $class) { |
|
573 | - foreach ($inflectors as $inflector) { |
|
570 | + while ($scope !== null){ |
|
571 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
572 | + if ($instance instanceof $class){ |
|
573 | + foreach ($inflectors as $inflector){ |
|
574 | 574 | $instance = $inflector->getParametersCount() > 1 |
575 | 575 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
576 | 576 | : ($inflector->inflector)($instance); |
@@ -586,9 +586,9 @@ discard block |
||
586 | 586 | |
587 | 587 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
588 | 588 | { |
589 | - try { |
|
589 | + try{ |
|
590 | 590 | $this->resolver->validateArguments($reflection, $arguments); |
591 | - } catch (\Throwable) { |
|
591 | + }catch (\Throwable){ |
|
592 | 592 | return false; |
593 | 593 | } |
594 | 594 |
@@ -67,17 +67,20 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
69 | 69 | { |
70 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
70 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
71 | + { |
|
71 | 72 | return $this->state->singletons[$alias]; |
72 | 73 | } |
73 | 74 | |
74 | 75 | $binding = $this->state->bindings[$alias] ?? null; |
75 | 76 | |
76 | - if ($binding === null) { |
|
77 | + if ($binding === null) |
|
78 | + { |
|
77 | 79 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
78 | 80 | } |
79 | 81 | |
80 | - try { |
|
82 | + try |
|
83 | + { |
|
81 | 84 | $this->tracer->push( |
82 | 85 | false, |
83 | 86 | action: 'resolve from binding', |
@@ -107,7 +110,9 @@ discard block |
||
107 | 110 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
108 | 111 | default => $binding, |
109 | 112 | }; |
110 | - } finally { |
|
113 | + } |
|
114 | + finally |
|
115 | + { |
|
111 | 116 | $this->state->bindings[$alias] ??= $binding; |
112 | 117 | $this->tracer->pop(true); |
113 | 118 | $this->tracer->pop(false); |
@@ -121,18 +126,23 @@ discard block |
||
121 | 126 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
122 | 127 | { |
123 | 128 | $context = $ctx->context; |
124 | - try { |
|
129 | + try |
|
130 | + { |
|
125 | 131 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
126 | - } catch (\ReflectionException $e) { |
|
132 | + } |
|
133 | + catch (\ReflectionException $e) |
|
134 | + { |
|
127 | 135 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
128 | 136 | } |
129 | 137 | |
130 | 138 | $injector = $binding->injector; |
131 | 139 | |
132 | - try { |
|
140 | + try |
|
141 | + { |
|
133 | 142 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
134 | 143 | |
135 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
144 | + if (!$injectorInstance instanceof InjectorInterface) |
|
145 | + { |
|
136 | 146 | throw new InjectionException( |
137 | 147 | \sprintf( |
138 | 148 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -159,7 +169,8 @@ discard block |
||
159 | 169 | default => (string) $context, |
160 | 170 | }); |
161 | 171 | |
162 | - if (!$reflection->isInstance($instance)) { |
|
172 | + if (!$reflection->isInstance($instance)) |
|
173 | + { |
|
163 | 174 | throw new InjectionException( |
164 | 175 | \sprintf( |
165 | 176 | "Invalid injection response for '%s'.", |
@@ -169,7 +180,9 @@ discard block |
||
169 | 180 | } |
170 | 181 | |
171 | 182 | return $instance; |
172 | - } finally { |
|
183 | + } |
|
184 | + finally |
|
185 | + { |
|
173 | 186 | $this->state->bindings[$ctx->class] ??= $binding; |
174 | 187 | } |
175 | 188 | } |
@@ -188,7 +201,8 @@ discard block |
||
188 | 201 | //Binding is pointing to something else |
189 | 202 | : $this->make($binding->alias, $arguments, $context); |
190 | 203 | |
191 | - if ($binding->singleton && $arguments === []) { |
|
204 | + if ($binding->singleton && $arguments === []) |
|
205 | + { |
|
192 | 206 | $this->state->singletons[$alias] = $result; |
193 | 207 | } |
194 | 208 | |
@@ -197,7 +211,8 @@ discard block |
||
197 | 211 | |
198 | 212 | private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
199 | 213 | { |
200 | - if ($context instanceof RetryContext) { |
|
214 | + if ($context instanceof RetryContext) |
|
215 | + { |
|
201 | 216 | return $binding->fallbackFactory === null |
202 | 217 | ? throw new RecursiveProxyException( |
203 | 218 | $alias, |
@@ -208,7 +223,8 @@ discard block |
||
208 | 223 | |
209 | 224 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
210 | 225 | |
211 | - if ($binding->singleton) { |
|
226 | + if ($binding->singleton) |
|
227 | + { |
|
212 | 228 | $this->state->singletons[$alias] = $result; |
213 | 229 | } |
214 | 230 | |
@@ -223,14 +239,16 @@ discard block |
||
223 | 239 | ): object { |
224 | 240 | $avoidCache = $arguments !== []; |
225 | 241 | |
226 | - if ($avoidCache) { |
|
242 | + if ($avoidCache) |
|
243 | + { |
|
227 | 244 | return $this->createInstance( |
228 | 245 | new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
229 | 246 | $arguments, |
230 | 247 | ); |
231 | 248 | } |
232 | 249 | |
233 | - if ($binding->singleton) { |
|
250 | + if ($binding->singleton) |
|
251 | + { |
|
234 | 252 | $this->state->singletons[$alias] = $binding->value; |
235 | 253 | } |
236 | 254 | |
@@ -260,11 +278,14 @@ discard block |
||
260 | 278 | array $arguments, |
261 | 279 | ): mixed { |
262 | 280 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
263 | - try { |
|
281 | + try |
|
282 | + { |
|
264 | 283 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
265 | 284 | ? ($binding->factory)() |
266 | 285 | : $this->invoker->invoke($binding->factory, $arguments); |
267 | - } catch (NotCallableException $e) { |
|
286 | + } |
|
287 | + catch (NotCallableException $e) |
|
288 | + { |
|
268 | 289 | throw new ContainerException( |
269 | 290 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
270 | 291 | $e->getCode(), |
@@ -283,19 +304,24 @@ discard block |
||
283 | 304 | ): ?object { |
284 | 305 | $avoidCache = $arguments !== []; |
285 | 306 | |
286 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
287 | - try { |
|
307 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
308 | + { |
|
309 | + try |
|
310 | + { |
|
288 | 311 | $this->tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
289 | 312 | |
290 | 313 | $object = $this->createInstance( |
291 | 314 | new Ctx(alias: $alias, class: $alias, context: $context), |
292 | 315 | $arguments, |
293 | 316 | ); |
294 | - if ($avoidCache) { |
|
317 | + if ($avoidCache) |
|
318 | + { |
|
295 | 319 | return $object; |
296 | 320 | } |
297 | 321 | $binding->reference = \WeakReference::create($object); |
298 | - } catch (\Throwable) { |
|
322 | + } |
|
323 | + catch (\Throwable) |
|
324 | + { |
|
299 | 325 | throw new ContainerException( |
300 | 326 | $this->tracer->combineTraceMessage( |
301 | 327 | \sprintf( |
@@ -305,7 +331,9 @@ discard block |
||
305 | 331 | ), |
306 | 332 | ), |
307 | 333 | ); |
308 | - } finally { |
|
334 | + } |
|
335 | + finally |
|
336 | + { |
|
309 | 337 | $this->tracer->pop(); |
310 | 338 | } |
311 | 339 | } |
@@ -320,19 +348,26 @@ discard block |
||
320 | 348 | ): mixed { |
321 | 349 | $parent = $this->scope->getParentFactory(); |
322 | 350 | |
323 | - if ($parent !== null) { |
|
324 | - try { |
|
351 | + if ($parent !== null) |
|
352 | + { |
|
353 | + try |
|
354 | + { |
|
325 | 355 | $this->tracer->push(false, ...[ |
326 | 356 | 'current scope' => $this->scope->getScopeName(), |
327 | 357 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
328 | 358 | ]); |
329 | 359 | /** @psalm-suppress TooManyArguments */ |
330 | 360 | return $parent->make($alias, $parameters, $context); |
331 | - } catch (BadScopeException $e) { |
|
332 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
361 | + } |
|
362 | + catch (BadScopeException $e) |
|
363 | + { |
|
364 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
365 | + { |
|
333 | 366 | throw $e; |
334 | 367 | } |
335 | - } catch (ContainerExceptionInterface $e) { |
|
368 | + } |
|
369 | + catch (ContainerExceptionInterface $e) |
|
370 | + { |
|
336 | 371 | $className = match (true) { |
337 | 372 | $e instanceof NotFoundException => NotFoundException::class, |
338 | 373 | $e instanceof RecursiveProxyException => throw $e, |
@@ -342,19 +377,24 @@ discard block |
||
342 | 377 | 'Can\'t resolve `%s`.', |
343 | 378 | $alias, |
344 | 379 | )), previous: $e); |
345 | - } finally { |
|
380 | + } |
|
381 | + finally |
|
382 | + { |
|
346 | 383 | $this->tracer->pop(false); |
347 | 384 | } |
348 | 385 | } |
349 | 386 | |
350 | 387 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
351 | - try { |
|
388 | + try |
|
389 | + { |
|
352 | 390 | //No direct instructions how to construct class, make is automatically |
353 | 391 | return $this->autowire( |
354 | 392 | new Ctx(alias: $alias, class: $alias, context: $context), |
355 | 393 | $parameters, |
356 | 394 | ); |
357 | - } finally { |
|
395 | + } |
|
396 | + finally |
|
397 | + { |
|
358 | 398 | $this->tracer->pop(false); |
359 | 399 | } |
360 | 400 | } |
@@ -401,13 +441,16 @@ discard block |
||
401 | 441 | Ctx $ctx, |
402 | 442 | array $arguments, |
403 | 443 | ): object { |
404 | - if ($this->options->checkScope) { |
|
444 | + if ($this->options->checkScope) |
|
445 | + { |
|
405 | 446 | // Check scope name |
406 | 447 | $ctx->reflection = new \ReflectionClass($instance); |
407 | 448 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
408 | - if ($scopeName !== null) { |
|
449 | + if ($scopeName !== null) |
|
450 | + { |
|
409 | 451 | $scope = $this->scope; |
410 | - while ($scope->getScopeName() !== $scopeName) { |
|
452 | + while ($scope->getScopeName() !== $scopeName) |
|
453 | + { |
|
411 | 454 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
412 | 455 | } |
413 | 456 | } |
@@ -436,26 +479,33 @@ discard block |
||
436 | 479 | array $arguments, |
437 | 480 | ): object { |
438 | 481 | $class = $ctx->class; |
439 | - try { |
|
482 | + try |
|
483 | + { |
|
440 | 484 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
441 | - } catch (\ReflectionException $e) { |
|
485 | + } |
|
486 | + catch (\ReflectionException $e) |
|
487 | + { |
|
442 | 488 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
443 | 489 | } |
444 | 490 | |
445 | 491 | // Check scope name |
446 | - if ($this->options->checkScope) { |
|
492 | + if ($this->options->checkScope) |
|
493 | + { |
|
447 | 494 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
448 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
495 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
496 | + { |
|
449 | 497 | throw new BadScopeException($scope, $class); |
450 | 498 | } |
451 | 499 | } |
452 | 500 | |
453 | 501 | // We have to construct class using external injector when we know the exact context |
454 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
502 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
503 | + { |
|
455 | 504 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
456 | 505 | } |
457 | 506 | |
458 | - if (!$reflection->isInstantiable()) { |
|
507 | + if (!$reflection->isInstantiable()) |
|
508 | + { |
|
459 | 509 | $itIs = match (true) { |
460 | 510 | $reflection->isEnum() => 'Enum', |
461 | 511 | $reflection->isAbstract() => 'Abstract class', |
@@ -468,12 +518,16 @@ discard block |
||
468 | 518 | |
469 | 519 | $constructor = $reflection->getConstructor(); |
470 | 520 | |
471 | - if ($constructor !== null) { |
|
472 | - try { |
|
521 | + if ($constructor !== null) |
|
522 | + { |
|
523 | + try |
|
524 | + { |
|
473 | 525 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
474 | 526 | $this->tracer->push(true); |
475 | 527 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
476 | - } catch (ValidationException $e) { |
|
528 | + } |
|
529 | + catch (ValidationException $e) |
|
530 | + { |
|
477 | 531 | throw new ContainerException( |
478 | 532 | $this->tracer->combineTraceMessage( |
479 | 533 | \sprintf( |
@@ -483,22 +537,31 @@ discard block |
||
483 | 537 | ), |
484 | 538 | ), |
485 | 539 | ); |
486 | - } finally { |
|
540 | + } |
|
541 | + finally |
|
542 | + { |
|
487 | 543 | $this->tracer->pop(true); |
488 | 544 | $this->tracer->pop(false); |
489 | 545 | } |
490 | - try { |
|
546 | + try |
|
547 | + { |
|
491 | 548 | // Using constructor with resolved arguments |
492 | 549 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
493 | 550 | $this->tracer->push(true); |
494 | 551 | $instance = new $class(...$args); |
495 | - } catch (\TypeError $e) { |
|
552 | + } |
|
553 | + catch (\TypeError $e) |
|
554 | + { |
|
496 | 555 | throw new WrongTypeException($constructor, $e); |
497 | - } finally { |
|
556 | + } |
|
557 | + finally |
|
558 | + { |
|
498 | 559 | $this->tracer->pop(true); |
499 | 560 | $this->tracer->pop(false); |
500 | 561 | } |
501 | - } else { |
|
562 | + } |
|
563 | + else |
|
564 | + { |
|
502 | 565 | // No constructor specified |
503 | 566 | $instance = $reflection->newInstance(); |
504 | 567 | } |
@@ -516,13 +579,15 @@ discard block |
||
516 | 579 | $instance = $this->runInflector($instance); |
517 | 580 | |
518 | 581 | //Declarative singletons |
519 | - if ($this->isSingleton($ctx)) { |
|
582 | + if ($this->isSingleton($ctx)) |
|
583 | + { |
|
520 | 584 | $this->state->singletons[$ctx->alias] = $instance; |
521 | 585 | } |
522 | 586 | |
523 | 587 | // Register finalizer |
524 | 588 | $finalizer = $this->getFinalizer($ctx, $instance); |
525 | - if ($finalizer !== null) { |
|
589 | + if ($finalizer !== null) |
|
590 | + { |
|
526 | 591 | $this->state->finalizers[] = $finalizer; |
527 | 592 | } |
528 | 593 | |
@@ -534,12 +599,14 @@ discard block |
||
534 | 599 | */ |
535 | 600 | private function isSingleton(Ctx $ctx): bool |
536 | 601 | { |
537 | - if ($ctx->singleton === true) { |
|
602 | + if ($ctx->singleton === true) |
|
603 | + { |
|
538 | 604 | return true; |
539 | 605 | } |
540 | 606 | |
541 | 607 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
542 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
608 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
609 | + { |
|
543 | 610 | return true; |
544 | 611 | } |
545 | 612 | |
@@ -553,7 +620,8 @@ discard block |
||
553 | 620 | * @var Attribute\Finalize|null $attribute |
554 | 621 | */ |
555 | 622 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
556 | - if ($attribute === null) { |
|
623 | + if ($attribute === null) |
|
624 | + { |
|
557 | 625 | return null; |
558 | 626 | } |
559 | 627 | |
@@ -567,10 +635,14 @@ discard block |
||
567 | 635 | { |
568 | 636 | $scope = $this->scope; |
569 | 637 | |
570 | - while ($scope !== null) { |
|
571 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
572 | - if ($instance instanceof $class) { |
|
573 | - foreach ($inflectors as $inflector) { |
|
638 | + while ($scope !== null) |
|
639 | + { |
|
640 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
641 | + { |
|
642 | + if ($instance instanceof $class) |
|
643 | + { |
|
644 | + foreach ($inflectors as $inflector) |
|
645 | + { |
|
574 | 646 | $instance = $inflector->getParametersCount() > 1 |
575 | 647 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
576 | 648 | : ($inflector->inflector)($instance); |
@@ -586,9 +658,12 @@ discard block |
||
586 | 658 | |
587 | 659 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
588 | 660 | { |
589 | - try { |
|
661 | + try |
|
662 | + { |
|
590 | 663 | $this->resolver->validateArguments($reflection, $arguments); |
591 | - } catch (\Throwable) { |
|
664 | + } |
|
665 | + catch (\Throwable) |
|
666 | + { |
|
592 | 667 | return false; |
593 | 668 | } |
594 | 669 |
@@ -31,7 +31,7 @@ |
||
31 | 31 | { |
32 | 32 | public function __construct( |
33 | 33 | public PublicContainer $publicContainer, |
34 | - ) {} |
|
34 | + ){} |
|
35 | 35 | |
36 | 36 | public function __get(string $name): object |
37 | 37 | { |
@@ -31,7 +31,8 @@ |
||
31 | 31 | { |
32 | 32 | public function __construct( |
33 | 33 | public PublicContainer $publicContainer, |
34 | - ) {} |
|
34 | + ) { |
|
35 | +} |
|
35 | 36 | |
36 | 37 | public function __get(string $name): object |
37 | 38 | { |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | ): array { |
49 | 49 | $state = new ResolvingState($reflection, $parameters); |
50 | 50 | |
51 | - foreach ($reflection->getParameters() as $parameter) { |
|
51 | + foreach ($reflection->getParameters() as $parameter){ |
|
52 | 52 | $this->resolveParameter($parameter, $state, $validate) |
53 | 53 | or |
54 | 54 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -62,47 +62,47 @@ discard block |
||
62 | 62 | $positional = true; |
63 | 63 | $variadic = false; |
64 | 64 | $parameters = $reflection->getParameters(); |
65 | - if (\count($parameters) === 0) { |
|
65 | + if (\count($parameters) === 0){ |
|
66 | 66 | return; |
67 | 67 | } |
68 | 68 | |
69 | 69 | $parameter = null; |
70 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
70 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
71 | 71 | // get related argument value |
72 | 72 | $key = \key($arguments); |
73 | 73 | |
74 | 74 | // For a variadic parameter it's no sense - named or positional argument will be sent |
75 | 75 | // But you can't send positional argument after named in any case |
76 | - if (\is_int($key) && !$positional) { |
|
76 | + if (\is_int($key) && !$positional){ |
|
77 | 77 | throw new PositionalArgumentException($reflection, $key); |
78 | 78 | } |
79 | 79 | |
80 | 80 | $positional = $positional && \is_int($key); |
81 | 81 | |
82 | - if (!$variadic) { |
|
82 | + if (!$variadic){ |
|
83 | 83 | $parameter = \array_shift($parameters); |
84 | 84 | $variadic = $parameter?->isVariadic() ?? false; |
85 | 85 | } |
86 | 86 | |
87 | - if ($parameter === null) { |
|
87 | + if ($parameter === null){ |
|
88 | 88 | throw new UnknownParameterException($reflection, $key); |
89 | 89 | } |
90 | 90 | $name = $parameter->getName(); |
91 | 91 | |
92 | - if (($positional || $variadic) && $key !== null) { |
|
92 | + if (($positional || $variadic) && $key !== null){ |
|
93 | 93 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
94 | 94 | $value = \array_shift($arguments); |
95 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
96 | - if ($parameter->isOptional()) { |
|
95 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
96 | + if ($parameter->isOptional()){ |
|
97 | 97 | continue; |
98 | 98 | } |
99 | 99 | throw new MissingRequiredArgumentException($reflection, $name); |
100 | - } else { |
|
100 | + }else{ |
|
101 | 101 | $value = &$arguments[$name]; |
102 | 102 | unset($arguments[$name]); |
103 | 103 | } |
104 | 104 | |
105 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
105 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
106 | 106 | throw new InvalidArgumentException($reflection, $name); |
107 | 107 | } |
108 | 108 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | |
111 | 111 | private function validateValueToParameter(\ReflectionParameter $parameter, mixed $value): bool |
112 | 112 | { |
113 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
113 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
114 | 114 | return true; |
115 | 115 | } |
116 | 116 | $type = $parameter->getType(); |
@@ -121,17 +121,17 @@ discard block |
||
121 | 121 | $type instanceof \ReflectionIntersectionType => [false, $type->getTypes()], |
122 | 122 | }; |
123 | 123 | |
124 | - foreach ($types as $t) { |
|
124 | + foreach ($types as $t){ |
|
125 | 125 | \assert($t instanceof \ReflectionNamedType); |
126 | - if (!$this->validateValueNamedType($t, $value)) { |
|
126 | + if (!$this->validateValueNamedType($t, $value)){ |
|
127 | 127 | // If it is TypeIntersection |
128 | - if ($or) { |
|
128 | + if ($or){ |
|
129 | 129 | continue; |
130 | 130 | } |
131 | 131 | return false; |
132 | 132 | } |
133 | 133 | // If it is not type intersection then we can skip that value after first successful check |
134 | - if ($or) { |
|
134 | + if ($or){ |
|
135 | 135 | return true; |
136 | 136 | } |
137 | 137 | } |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | { |
147 | 147 | $name = $type->getName(); |
148 | 148 | |
149 | - if ($type->isBuiltin()) { |
|
149 | + if ($type->isBuiltin()){ |
|
150 | 150 | return match ($name) { |
151 | 151 | 'mixed' => true, |
152 | 152 | 'string' => \is_string($value), |
@@ -179,13 +179,13 @@ discard block |
||
179 | 179 | |
180 | 180 | // Try to resolve parameter by name |
181 | 181 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
182 | - if ($res !== [] || $isVariadic) { |
|
182 | + if ($res !== [] || $isVariadic){ |
|
183 | 183 | // validate |
184 | - if ($isVariadic) { |
|
185 | - foreach ($res as $k => &$v) { |
|
186 | - $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
|
184 | + if ($isVariadic){ |
|
185 | + foreach ($res as $k => &$v){ |
|
186 | + $this->processArgument($state, $v, validateWith: $validate ? $param : null, key : $k); |
|
187 | 187 | } |
188 | - } else { |
|
188 | + }else{ |
|
189 | 189 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
190 | 190 | } |
191 | 191 | |
@@ -193,18 +193,18 @@ discard block |
||
193 | 193 | } |
194 | 194 | |
195 | 195 | $error = null; |
196 | - while ($hasType) { |
|
196 | + while ($hasType){ |
|
197 | 197 | /** @var \ReflectionIntersectionType|\ReflectionUnionType|\ReflectionNamedType $refType */ |
198 | 198 | $refType = $param->getType(); |
199 | 199 | |
200 | - if ($refType::class === \ReflectionNamedType::class) { |
|
201 | - if ($refType->isBuiltin()) { |
|
200 | + if ($refType::class === \ReflectionNamedType::class){ |
|
201 | + if ($refType->isBuiltin()){ |
|
202 | 202 | break; |
203 | 203 | } |
204 | 204 | |
205 | 205 | if (\interface_exists($refType->getName()) && !empty( |
206 | 206 | $attrs = $param->getAttributes(ProxyAttribute::class) |
207 | - )) { |
|
207 | + )){ |
|
208 | 208 | $proxy = Proxy::create( |
209 | 209 | new \ReflectionClass($refType->getName()), |
210 | 210 | $param, |
@@ -214,23 +214,23 @@ discard block |
||
214 | 214 | return true; |
215 | 215 | } |
216 | 216 | |
217 | - try { |
|
218 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
217 | + try{ |
|
218 | + if ($this->resolveObject($state, $refType, $param, $validate)){ |
|
219 | 219 | return true; |
220 | 220 | } |
221 | - } catch (\Throwable $e) { |
|
221 | + }catch (\Throwable $e){ |
|
222 | 222 | $error = $e; |
223 | 223 | } |
224 | 224 | break; |
225 | 225 | } |
226 | 226 | |
227 | - if ($refType::class === \ReflectionUnionType::class) { |
|
228 | - foreach ($refType->getTypes() as $namedType) { |
|
229 | - try { |
|
230 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
227 | + if ($refType::class === \ReflectionUnionType::class){ |
|
228 | + foreach ($refType->getTypes() as $namedType){ |
|
229 | + try{ |
|
230 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)){ |
|
231 | 231 | return true; |
232 | 232 | } |
233 | - } catch (\Throwable $e) { |
|
233 | + }catch (\Throwable $e){ |
|
234 | 234 | $error = $e; |
235 | 235 | } |
236 | 236 | } |
@@ -240,19 +240,19 @@ discard block |
||
240 | 240 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
241 | 241 | } |
242 | 242 | |
243 | - if ($param->isDefaultValueAvailable()) { |
|
243 | + if ($param->isDefaultValueAvailable()){ |
|
244 | 244 | $argument = $param->getDefaultValue(); |
245 | 245 | $this->processArgument($state, $argument); |
246 | 246 | return true; |
247 | 247 | } |
248 | 248 | |
249 | - if ($hasType && $param->allowsNull()) { |
|
249 | + if ($hasType && $param->allowsNull()){ |
|
250 | 250 | $argument = null; |
251 | 251 | $this->processArgument($state, $argument); |
252 | 252 | return true; |
253 | 253 | } |
254 | 254 | |
255 | - if ($error === null) { |
|
255 | + if ($error === null){ |
|
256 | 256 | return false; |
257 | 257 | } |
258 | 258 | |
@@ -290,15 +290,15 @@ discard block |
||
290 | 290 | ResolvingState $state, |
291 | 291 | mixed &$value, |
292 | 292 | ?\ReflectionParameter $validateWith = null, |
293 | - int|string|null $key = null, |
|
293 | + int | string | null $key = null, |
|
294 | 294 | ): void { |
295 | 295 | // Resolve Autowire objects |
296 | - if ($value instanceof Autowire) { |
|
296 | + if ($value instanceof Autowire){ |
|
297 | 297 | $value = $value->resolve($this->factory); |
298 | 298 | } |
299 | 299 | |
300 | 300 | // Validation |
301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
301 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
302 | 302 | throw new InvalidArgumentException( |
303 | 303 | $validateWith->getDeclaringFunction(), |
304 | 304 | $validateWith->getName(), |
@@ -48,7 +48,8 @@ discard block |
||
48 | 48 | ): array { |
49 | 49 | $state = new ResolvingState($reflection, $parameters); |
50 | 50 | |
51 | - foreach ($reflection->getParameters() as $parameter) { |
|
51 | + foreach ($reflection->getParameters() as $parameter) |
|
52 | + { |
|
52 | 53 | $this->resolveParameter($parameter, $state, $validate) |
53 | 54 | or |
54 | 55 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -62,47 +63,59 @@ discard block |
||
62 | 63 | $positional = true; |
63 | 64 | $variadic = false; |
64 | 65 | $parameters = $reflection->getParameters(); |
65 | - if (\count($parameters) === 0) { |
|
66 | + if (\count($parameters) === 0) |
|
67 | + { |
|
66 | 68 | return; |
67 | 69 | } |
68 | 70 | |
69 | 71 | $parameter = null; |
70 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
72 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
73 | + { |
|
71 | 74 | // get related argument value |
72 | 75 | $key = \key($arguments); |
73 | 76 | |
74 | 77 | // For a variadic parameter it's no sense - named or positional argument will be sent |
75 | 78 | // But you can't send positional argument after named in any case |
76 | - if (\is_int($key) && !$positional) { |
|
79 | + if (\is_int($key) && !$positional) |
|
80 | + { |
|
77 | 81 | throw new PositionalArgumentException($reflection, $key); |
78 | 82 | } |
79 | 83 | |
80 | 84 | $positional = $positional && \is_int($key); |
81 | 85 | |
82 | - if (!$variadic) { |
|
86 | + if (!$variadic) |
|
87 | + { |
|
83 | 88 | $parameter = \array_shift($parameters); |
84 | 89 | $variadic = $parameter?->isVariadic() ?? false; |
85 | 90 | } |
86 | 91 | |
87 | - if ($parameter === null) { |
|
92 | + if ($parameter === null) |
|
93 | + { |
|
88 | 94 | throw new UnknownParameterException($reflection, $key); |
89 | 95 | } |
90 | 96 | $name = $parameter->getName(); |
91 | 97 | |
92 | - if (($positional || $variadic) && $key !== null) { |
|
98 | + if (($positional || $variadic) && $key !== null) |
|
99 | + { |
|
93 | 100 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
94 | 101 | $value = \array_shift($arguments); |
95 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
96 | - if ($parameter->isOptional()) { |
|
102 | + } |
|
103 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
104 | + { |
|
105 | + if ($parameter->isOptional()) |
|
106 | + { |
|
97 | 107 | continue; |
98 | 108 | } |
99 | 109 | throw new MissingRequiredArgumentException($reflection, $name); |
100 | - } else { |
|
110 | + } |
|
111 | + else |
|
112 | + { |
|
101 | 113 | $value = &$arguments[$name]; |
102 | 114 | unset($arguments[$name]); |
103 | 115 | } |
104 | 116 | |
105 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
117 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
118 | + { |
|
106 | 119 | throw new InvalidArgumentException($reflection, $name); |
107 | 120 | } |
108 | 121 | } |
@@ -110,7 +123,8 @@ discard block |
||
110 | 123 | |
111 | 124 | private function validateValueToParameter(\ReflectionParameter $parameter, mixed $value): bool |
112 | 125 | { |
113 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
126 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
127 | + { |
|
114 | 128 | return true; |
115 | 129 | } |
116 | 130 | $type = $parameter->getType(); |
@@ -121,17 +135,21 @@ discard block |
||
121 | 135 | $type instanceof \ReflectionIntersectionType => [false, $type->getTypes()], |
122 | 136 | }; |
123 | 137 | |
124 | - foreach ($types as $t) { |
|
138 | + foreach ($types as $t) |
|
139 | + { |
|
125 | 140 | \assert($t instanceof \ReflectionNamedType); |
126 | - if (!$this->validateValueNamedType($t, $value)) { |
|
141 | + if (!$this->validateValueNamedType($t, $value)) |
|
142 | + { |
|
127 | 143 | // If it is TypeIntersection |
128 | - if ($or) { |
|
144 | + if ($or) |
|
145 | + { |
|
129 | 146 | continue; |
130 | 147 | } |
131 | 148 | return false; |
132 | 149 | } |
133 | 150 | // If it is not type intersection then we can skip that value after first successful check |
134 | - if ($or) { |
|
151 | + if ($or) |
|
152 | + { |
|
135 | 153 | return true; |
136 | 154 | } |
137 | 155 | } |
@@ -146,7 +164,8 @@ discard block |
||
146 | 164 | { |
147 | 165 | $name = $type->getName(); |
148 | 166 | |
149 | - if ($type->isBuiltin()) { |
|
167 | + if ($type->isBuiltin()) |
|
168 | + { |
|
150 | 169 | return match ($name) { |
151 | 170 | 'mixed' => true, |
152 | 171 | 'string' => \is_string($value), |
@@ -179,13 +198,18 @@ discard block |
||
179 | 198 | |
180 | 199 | // Try to resolve parameter by name |
181 | 200 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
182 | - if ($res !== [] || $isVariadic) { |
|
201 | + if ($res !== [] || $isVariadic) |
|
202 | + { |
|
183 | 203 | // validate |
184 | - if ($isVariadic) { |
|
185 | - foreach ($res as $k => &$v) { |
|
204 | + if ($isVariadic) |
|
205 | + { |
|
206 | + foreach ($res as $k => &$v) |
|
207 | + { |
|
186 | 208 | $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
187 | 209 | } |
188 | - } else { |
|
210 | + } |
|
211 | + else |
|
212 | + { |
|
189 | 213 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
190 | 214 | } |
191 | 215 | |
@@ -193,18 +217,22 @@ discard block |
||
193 | 217 | } |
194 | 218 | |
195 | 219 | $error = null; |
196 | - while ($hasType) { |
|
220 | + while ($hasType) |
|
221 | + { |
|
197 | 222 | /** @var \ReflectionIntersectionType|\ReflectionUnionType|\ReflectionNamedType $refType */ |
198 | 223 | $refType = $param->getType(); |
199 | 224 | |
200 | - if ($refType::class === \ReflectionNamedType::class) { |
|
201 | - if ($refType->isBuiltin()) { |
|
225 | + if ($refType::class === \ReflectionNamedType::class) |
|
226 | + { |
|
227 | + if ($refType->isBuiltin()) |
|
228 | + { |
|
202 | 229 | break; |
203 | 230 | } |
204 | 231 | |
205 | 232 | if (\interface_exists($refType->getName()) && !empty( |
206 | 233 | $attrs = $param->getAttributes(ProxyAttribute::class) |
207 | - )) { |
|
234 | + )) |
|
235 | + { |
|
208 | 236 | $proxy = Proxy::create( |
209 | 237 | new \ReflectionClass($refType->getName()), |
210 | 238 | $param, |
@@ -214,23 +242,33 @@ discard block |
||
214 | 242 | return true; |
215 | 243 | } |
216 | 244 | |
217 | - try { |
|
218 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
245 | + try |
|
246 | + { |
|
247 | + if ($this->resolveObject($state, $refType, $param, $validate)) |
|
248 | + { |
|
219 | 249 | return true; |
220 | 250 | } |
221 | - } catch (\Throwable $e) { |
|
251 | + } |
|
252 | + catch (\Throwable $e) |
|
253 | + { |
|
222 | 254 | $error = $e; |
223 | 255 | } |
224 | 256 | break; |
225 | 257 | } |
226 | 258 | |
227 | - if ($refType::class === \ReflectionUnionType::class) { |
|
228 | - foreach ($refType->getTypes() as $namedType) { |
|
229 | - try { |
|
230 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
259 | + if ($refType::class === \ReflectionUnionType::class) |
|
260 | + { |
|
261 | + foreach ($refType->getTypes() as $namedType) |
|
262 | + { |
|
263 | + try |
|
264 | + { |
|
265 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) |
|
266 | + { |
|
231 | 267 | return true; |
232 | 268 | } |
233 | - } catch (\Throwable $e) { |
|
269 | + } |
|
270 | + catch (\Throwable $e) |
|
271 | + { |
|
234 | 272 | $error = $e; |
235 | 273 | } |
236 | 274 | } |
@@ -240,19 +278,22 @@ discard block |
||
240 | 278 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
241 | 279 | } |
242 | 280 | |
243 | - if ($param->isDefaultValueAvailable()) { |
|
281 | + if ($param->isDefaultValueAvailable()) |
|
282 | + { |
|
244 | 283 | $argument = $param->getDefaultValue(); |
245 | 284 | $this->processArgument($state, $argument); |
246 | 285 | return true; |
247 | 286 | } |
248 | 287 | |
249 | - if ($hasType && $param->allowsNull()) { |
|
288 | + if ($hasType && $param->allowsNull()) |
|
289 | + { |
|
250 | 290 | $argument = null; |
251 | 291 | $this->processArgument($state, $argument); |
252 | 292 | return true; |
253 | 293 | } |
254 | 294 | |
255 | - if ($error === null) { |
|
295 | + if ($error === null) |
|
296 | + { |
|
256 | 297 | return false; |
257 | 298 | } |
258 | 299 | |
@@ -293,12 +334,14 @@ discard block |
||
293 | 334 | int|string|null $key = null, |
294 | 335 | ): void { |
295 | 336 | // Resolve Autowire objects |
296 | - if ($value instanceof Autowire) { |
|
337 | + if ($value instanceof Autowire) |
|
338 | + { |
|
297 | 339 | $value = $value->resolve($this->factory); |
298 | 340 | } |
299 | 341 | |
300 | 342 | // Validation |
301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
343 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
344 | + { |
|
302 | 345 | throw new InvalidArgumentException( |
303 | 346 | $validateWith->getDeclaringFunction(), |
304 | 347 | $validateWith->getName(), |
@@ -17,8 +17,8 @@ |
||
17 | 17 | public function __construct( |
18 | 18 | public readonly string $alias, |
19 | 19 | public string $class, |
20 | - public \Stringable|string|null $context = null, |
|
20 | + public \Stringable | string | null $context = null, |
|
21 | 21 | public ?bool $singleton = null, |
22 | 22 | public ?\ReflectionClass $reflection = null, |
23 | - ) {} |
|
23 | + ){} |
|
24 | 24 | } |
@@ -20,5 +20,6 @@ |
||
20 | 20 | public \Stringable|string|null $context = null, |
21 | 21 | public ?bool $singleton = null, |
22 | 22 | public ?\ReflectionClass $reflection = null, |
23 | - ) {} |
|
23 | + ) { |
|
24 | +} |
|
24 | 25 | } |