Test Failed
Pull Request — master (#1222)
by Aleksei
19:46 queued 08:54
created
src/Core/src/Internal/Actor.php 1 patch
Braces   +155 added lines, -66 removed lines patch added patch discarded remove patch
@@ -96,20 +96,25 @@  discard block
 block discarded – undo
96 96
         // Aliases to prevent circular dependencies
97 97
         $as = [];
98 98
         $actor = $this;
99
-        do {
99
+        do
100
+        {
100 101
             $bindings = &$actor->state->bindings;
101 102
             $singletons = &$actor->state->singletons;
102 103
             $injectors = &$actor->state->injectors;
103 104
             $binding = $bindings[$alias] ?? null;
104
-            if (\array_key_exists($alias, $singletons)) {
105
+            if (\array_key_exists($alias, $singletons))
106
+            {
105 107
                 $singleton = $singletons[$alias];
106 108
                 $injector = $injectors[$alias] ?? null;
107 109
                 return \is_object($singleton::class) ? $singleton::class : null;
108 110
             }
109 111
 
110
-            if ($binding !== null) {
111
-                if ($followAlias && $binding::class === Alias::class) {
112
-                    if ($binding->alias === $alias) {
112
+            if ($binding !== null)
113
+            {
114
+                if ($followAlias && $binding::class === Alias::class)
115
+                {
116
+                    if ($binding->alias === $alias)
117
+                    {
113 118
                         break;
114 119
                     }
115 120
 
@@ -124,7 +129,8 @@  discard block
 block discarded – undo
124 129
                 return $binding->getReturnClass();
125 130
             }
126 131
 
127
-            if (\array_key_exists($alias, $injectors)) {
132
+            if (\array_key_exists($alias, $injectors))
133
+            {
128 134
                 $injector = $injectors[$alias];
129 135
                 $binding = $bindings[$alias] ?? null;
130 136
                 return $alias;
@@ -132,7 +138,8 @@  discard block
 block discarded – undo
132 138
 
133 139
             // Go to parent scope
134 140
             $parent = $actor->scope->getParentActor();
135
-            if ($parent === null) {
141
+            if ($parent === null)
142
+            {
136 143
                 break;
137 144
             }
138 145
 
@@ -202,18 +209,23 @@  discard block
 block discarded – undo
202 209
     private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer)
203 210
     {
204 211
         $context = $ctx->context;
205
-        try {
212
+        try
213
+        {
206 214
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
207
-        } catch (\ReflectionException $e) {
215
+        }
216
+        catch (\ReflectionException $e)
217
+        {
208 218
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
209 219
         }
210 220
 
211 221
         $injector = $binding->injector;
212 222
 
213
-        try {
223
+        try
224
+        {
214 225
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
215 226
 
216
-            if (!$injectorInstance instanceof InjectorInterface) {
227
+            if (!$injectorInstance instanceof InjectorInterface)
228
+            {
217 229
                 throw new InjectionException(
218 230
                     \sprintf(
219 231
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -240,7 +252,8 @@  discard block
 block discarded – undo
240 252
                 default => (string) $context,
241 253
             });
242 254
 
243
-            if (!$reflection->isInstance($instance)) {
255
+            if (!$reflection->isInstance($instance))
256
+            {
244 257
                 throw new InjectionException(
245 258
                     \sprintf(
246 259
                         "Invalid injection response for '%s'.",
@@ -250,12 +263,16 @@  discard block
 block discarded – undo
250 263
             }
251 264
 
252 265
             return $instance;
253
-        } catch (TracedContainerException $e) {
266
+        }
267
+        catch (TracedContainerException $e)
268
+        {
254 269
             throw isset($injectorInstance) ? $e : $e::extendTracedException(\sprintf(
255 270
                 'Can\'t resolve `%s`.',
256 271
                 $tracer->getRootAlias(),
257 272
             ), $tracer->getTraces(), $e);
258
-        } finally {
273
+        }
274
+        finally
275
+        {
259 276
             $this->state->bindings[$ctx->class] ??= $binding;
260 277
         }
261 278
     }
@@ -267,18 +284,24 @@  discard block
 block discarded – undo
267 284
         array $arguments,
268 285
         Tracer $tracer,
269 286
     ): mixed {
270
-        if ($binding->alias === $alias) {
287
+        if ($binding->alias === $alias)
288
+        {
271 289
             $instance = $this->autowire(
272 290
                 new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []),
273 291
                 $arguments,
274 292
                 $this,
275 293
                 $tracer,
276 294
             );
277
-        } else {
278
-            try {
295
+        }
296
+        else
297
+        {
298
+            try
299
+            {
279 300
                 //Binding is pointing to something else
280 301
                 $instance = $this->factory->make($binding->alias, $arguments, $context);
281
-            } catch (TracedContainerException $e) {
302
+            }
303
+            catch (TracedContainerException $e)
304
+            {
282 305
                 throw $e::extendTracedException(
283 306
                     $alias === $tracer->getRootAlias()
284 307
                         ? "Can't resolve `{$alias}`."
@@ -297,7 +320,8 @@  discard block
 block discarded – undo
297 320
 
298 321
     private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed
299 322
     {
300
-        if ($context instanceof RetryContext) {
323
+        if ($context instanceof RetryContext)
324
+        {
301 325
             return $binding->fallbackFactory === null
302 326
                 ? throw new RecursiveProxyException(
303 327
                     $alias,
@@ -308,7 +332,8 @@  discard block
 block discarded – undo
308 332
 
309 333
         $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy());
310 334
 
311
-        if ($binding->singleton) {
335
+        if ($binding->singleton)
336
+        {
312 337
             $this->state->singletons[$alias] = $result;
313 338
         }
314 339
 
@@ -322,7 +347,8 @@  discard block
 block discarded – undo
322 347
         array $arguments,
323 348
         Tracer $tracer,
324 349
     ): object {
325
-        if ($arguments !== []) {
350
+        if ($arguments !== [])
351
+        {
326 352
             // Avoid singleton cache
327 353
             return $this->createInstance(
328 354
                 new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false),
@@ -332,7 +358,8 @@  discard block
 block discarded – undo
332 358
             );
333 359
         }
334 360
 
335
-        if ($binding->singleton) {
361
+        if ($binding->singleton)
362
+        {
336 363
             $this->state->singletons[$alias] = $binding->value;
337 364
         }
338 365
 
@@ -349,9 +376,12 @@  discard block
 block discarded – undo
349 376
         $target = $binding->autowire->alias;
350 377
         $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null);
351 378
 
352
-        if ($alias === $target) {
379
+        if ($alias === $target)
380
+        {
353 381
             $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer);
354
-        } else {
382
+        }
383
+        else
384
+        {
355 385
             $instance = $binding->autowire->resolve($this->factory, $arguments);
356 386
             $this->validateConstraint($instance, $ctx);
357 387
         }
@@ -367,25 +397,34 @@  discard block
 block discarded – undo
367 397
         Tracer $tracer,
368 398
     ): mixed {
369 399
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null);
370
-        try {
400
+        try
401
+        {
371 402
             $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0
372 403
                 ? ($binding->factory)()
373 404
                 : $this->invoker->invoke($binding->factory, $arguments);
374
-        } catch (NotCallableException $e) {
405
+        }
406
+        catch (NotCallableException $e)
407
+        {
375 408
             throw TracedContainerException::createWithTrace(
376 409
                 \sprintf('Invalid callable binding for `%s`.', $ctx->alias),
377 410
                 $tracer->getTraces(),
378 411
                 $e,
379 412
             );
380
-        } catch (TracedContainerException $e) {
413
+        }
414
+        catch (TracedContainerException $e)
415
+        {
381 416
             throw $e::extendTracedException(
382 417
                 \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()),
383 418
                 $tracer->getTraces(),
384 419
                 $e,
385 420
             );
386
-        } catch (ContainerExceptionInterface $e) {
421
+        }
422
+        catch (ContainerExceptionInterface $e)
423
+        {
387 424
             throw $e;
388
-        } catch (\Throwable $e) {
425
+        }
426
+        catch (\Throwable $e)
427
+        {
389 428
             throw NotFoundException::createWithTrace(
390 429
                 \sprintf("Can't resolve `%s` due to factory invocation error: %s", $tracer->getRootAlias(), $e->getMessage()),
391 430
                 $tracer->getTraces(),
@@ -393,7 +432,8 @@  discard block
 block discarded – undo
393 432
             );
394 433
         }
395 434
 
396
-        if (\is_object($instance)) {
435
+        if (\is_object($instance))
436
+        {
397 437
             $this->validateConstraint($instance, $ctx);
398 438
             return $this->registerInstance($ctx, $instance);
399 439
         }
@@ -410,8 +450,10 @@  discard block
 block discarded – undo
410 450
     ): ?object {
411 451
         $avoidCache = $arguments !== [];
412 452
 
413
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
414
-            try {
453
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
454
+        {
455
+            try
456
+            {
415 457
                 $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context);
416 458
 
417 459
                 $object = $this->createInstance(
@@ -420,17 +462,22 @@  discard block
 block discarded – undo
420 462
                     $this,
421 463
                     $tracer,
422 464
                 );
423
-                if ($avoidCache) {
465
+                if ($avoidCache)
466
+                {
424 467
                     return $object;
425 468
                 }
426 469
                 $binding->reference = \WeakReference::create($object);
427
-            } catch (\Throwable) {
470
+            }
471
+            catch (\Throwable)
472
+            {
428 473
                 throw TracedContainerException::createWithTrace(\sprintf(
429 474
                     'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.',
430 475
                     $tracer->getRootAlias(),
431 476
                     $alias,
432 477
                 ), $tracer->getTraces());
433
-            } finally {
478
+            }
479
+            finally
480
+            {
434 481
                 $tracer->pop();
435 482
             }
436 483
         }
@@ -446,13 +493,16 @@  discard block
 block discarded – undo
446 493
         object $instance,
447 494
         Ctx $ctx,
448 495
     ): void {
449
-        if ($this->options->checkScope) {
496
+        if ($this->options->checkScope)
497
+        {
450 498
             // Check scope name
451 499
             $ctx->reflection ??= new \ReflectionClass($instance);
452 500
             $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
453
-            if ($scopeName !== null) {
501
+            if ($scopeName !== null)
502
+            {
454 503
                 $scope = $this->scope;
455
-                while ($scope->getScopeName() !== $scopeName) {
504
+                while ($scope->getScopeName() !== $scopeName)
505
+                {
456 506
                     $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class);
457 507
                 }
458 508
             }
@@ -479,26 +529,34 @@  discard block
 block discarded – undo
479 529
         Tracer $tracer,
480 530
     ): object {
481 531
         $class = $ctx->class;
482
-        try {
532
+        try
533
+        {
483 534
             $ctx->reflection = $reflection = new \ReflectionClass($class);
484
-        } catch (\ReflectionException $e) {
535
+        }
536
+        catch (\ReflectionException $e)
537
+        {
485 538
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
486 539
         }
487 540
 
488 541
         // Check Scope attribute
489 542
         $actor = $fallbackActor ?? $this;
490
-        if ($this->options->checkScope) { # todo
543
+        if ($this->options->checkScope)
544
+        {
545
+# todo
491 546
             $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null);
492
-            if ($ar !== null) {
547
+            if ($ar !== null)
548
+            {
493 549
                 /** @var Attribute\Scope $attr */
494 550
                 $attr = $ar->newInstance();
495 551
                 $scope = $this->scope;
496 552
                 $actor = $this;
497 553
                 // Go through all parent scopes
498 554
                 $needed = $actor;
499
-                while ($attr->name !== $scope->getScopeName()) {
555
+                while ($attr->name !== $scope->getScopeName())
556
+                {
500 557
                     $needed = $scope->getParentActor();
501
-                    if ($needed === null) {
558
+                    if ($needed === null)
559
+                    {
502 560
                         throw new BadScopeException($attr->name, $class);
503 561
                     }
504 562
 
@@ -511,11 +569,13 @@  discard block
 block discarded – undo
511 569
         } # todo
512 570
 
513 571
         // We have to construct class using external injector when we know the exact context
514
-        if ($arguments === [] && $actor->binder->hasInjector($class)) {
572
+        if ($arguments === [] && $actor->binder->hasInjector($class))
573
+        {
515 574
             return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer);
516 575
         }
517 576
 
518
-        if (!$reflection->isInstantiable()) {
577
+        if (!$reflection->isInstantiable())
578
+        {
519 579
             $itIs = match (true) {
520 580
                 $reflection->isEnum() => 'Enum',
521 581
                 $reflection->isAbstract() => 'Abstract class',
@@ -529,8 +589,10 @@  discard block
 block discarded – undo
529 589
 
530 590
         $constructor = $reflection->getConstructor();
531 591
 
532
-        if ($constructor !== null) {
533
-            try {
592
+        if ($constructor !== null)
593
+        {
594
+            try
595
+            {
534 596
                 $newScope = $this !== $actor;
535 597
                 $debug = [
536 598
                     'action' => 'resolve arguments',
@@ -544,18 +606,24 @@  discard block
 block discarded – undo
544 606
                 $tracer->push($newScope, ...$debug);
545 607
                 $tracer->push(true);
546 608
                 $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments);
547
-            } catch (ValidationException $e) {
609
+            }
610
+            catch (ValidationException $e)
611
+            {
548 612
                 throw TracedContainerException::createWithTrace(\sprintf(
549 613
                     'Can\'t resolve `%s`. %s',
550 614
                     $tracer->getRootAlias(),
551 615
                     $e->getMessage(),
552 616
                 ), $tracer->getTraces());
553
-            } catch (TracedContainerException $e) {
617
+            }
618
+            catch (TracedContainerException $e)
619
+            {
554 620
                 throw $e::extendTracedException(\sprintf(
555 621
                     'Can\'t resolve `%s`.',
556 622
                     $tracer->getRootAlias(),
557 623
                 ), $tracer->getTraces(), $e);
558
-            } catch (\Throwable $e) {
624
+            }
625
+            catch (\Throwable $e)
626
+            {
559 627
                 throw TracedContainerException::createWithTrace(
560 628
                     \sprintf(
561 629
                         "Can't resolve `%s`.\n%s",
@@ -563,28 +631,39 @@  discard block
 block discarded – undo
563 631
                         $e->getMessage(),
564 632
                     ), $tracer->getTraces(), $e
565 633
                 );
566
-            } finally {
634
+            }
635
+            finally
636
+            {
567 637
                 $tracer->pop($newScope);
568 638
                 $tracer->pop(false);
569 639
             }
570
-            try {
640
+            try
641
+            {
571 642
                 // Using constructor with resolved arguments
572 643
                 $tracer->push(false, call: "$class::__construct", arguments: $args);
573 644
                 $tracer->push(true);
574 645
                 $instance = new $class(...$args);
575
-            } catch (\TypeError $e) {
646
+            }
647
+            catch (\TypeError $e)
648
+            {
576 649
                 throw new WrongTypeException($constructor, $e);
577
-            } catch (TracedContainerException $e) {
650
+            }
651
+            catch (TracedContainerException $e)
652
+            {
578 653
                 throw $e::extendTracedException(\sprintf(
579 654
                     'Can\'t resolve `%s`: failed constructing `%s`.',
580 655
                     $tracer->getRootAlias(),
581 656
                     $class,
582 657
                 ), $tracer->getTraces(), $e);
583
-            } finally {
658
+            }
659
+            finally
660
+            {
584 661
                 $tracer->pop(true);
585 662
                 $tracer->pop(false);
586 663
             }
587
-        } else {
664
+        }
665
+        else
666
+        {
588 667
             // No constructor specified
589 668
             $instance = $reflection->newInstance();
590 669
         }
@@ -616,12 +695,14 @@  discard block
 block discarded – undo
616 695
      */
617 696
     private function isSingleton(Ctx $ctx): bool
618 697
     {
619
-        if (is_bool($ctx->singleton)) {
698
+        if (is_bool($ctx->singleton))
699
+        {
620 700
             return $ctx->singleton;
621 701
         }
622 702
 
623 703
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
624
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
704
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
705
+        {
625 706
             return true;
626 707
         }
627 708
 
@@ -635,7 +716,8 @@  discard block
 block discarded – undo
635 716
          * @var Attribute\Finalize|null $attribute
636 717
          */
637 718
         $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance();
638
-        if ($attribute === null) {
719
+        if ($attribute === null)
720
+        {
639 721
             return null;
640 722
         }
641 723
 
@@ -649,10 +731,14 @@  discard block
 block discarded – undo
649 731
     {
650 732
         $scope = $this->scope;
651 733
 
652
-        while ($scope !== null) {
653
-            foreach ($this->state->inflectors as $class => $inflectors) {
654
-                if ($instance instanceof $class) {
655
-                    foreach ($inflectors as $inflector) {
734
+        while ($scope !== null)
735
+        {
736
+            foreach ($this->state->inflectors as $class => $inflectors)
737
+            {
738
+                if ($instance instanceof $class)
739
+                {
740
+                    foreach ($inflectors as $inflector)
741
+                    {
656 742
                         $instance = $inflector->getParametersCount() > 1
657 743
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
658 744
                             : ($inflector->inflector)($instance);
@@ -668,9 +754,12 @@  discard block
 block discarded – undo
668 754
 
669 755
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
670 756
     {
671
-        try {
757
+        try
758
+        {
672 759
             $this->resolver->validateArguments($reflection, $arguments);
673
-        } catch (\Throwable) {
760
+        }
761
+        catch (\Throwable)
762
+        {
674 763
             return false;
675 764
         }
676 765
 
Please login to merge, or discard this patch.