Passed
Push — master ( 9f2c4f...e942df )
by Aleksei
17:57 queued 05:15
created
src/Core/src/Config/Shared.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -12,7 +12,8 @@
 block discarded – undo
12 12
     public function __construct(
13 13
         public readonly object $value,
14 14
         public readonly bool $singleton = false,
15
-    ) {}
15
+    ) {
16
+}
16 17
 
17 18
     public function __toString(): string
18 19
     {
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Braces   +133 added lines, -58 removed lines patch added patch discarded remove patch
@@ -70,17 +70,20 @@  discard block
 block discarded – undo
70 70
      */
71 71
     public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed
72 72
     {
73
-        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) {
73
+        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons))
74
+        {
74 75
             return $this->state->singletons[$alias];
75 76
         }
76 77
 
77 78
         $binding = $this->state->bindings[$alias] ?? null;
78 79
 
79
-        if ($binding === null) {
80
+        if ($binding === null)
81
+        {
80 82
             return $this->resolveWithoutBinding($alias, $parameters, $context);
81 83
         }
82 84
 
83
-        try {
85
+        try
86
+        {
84 87
             $this->tracer->push(
85 88
                 false,
86 89
                 action: 'resolve from binding',
@@ -110,7 +113,9 @@  discard block
 block discarded – undo
110 113
                     ->resolveWeakReference($binding, $alias, $context, $parameters),
111 114
                 default => $binding,
112 115
             };
113
-        } finally {
116
+        }
117
+        finally
118
+        {
114 119
             $this->state->bindings[$alias] ??= $binding;
115 120
             $this->tracer->pop(true);
116 121
             $this->tracer->pop(false);
@@ -124,18 +129,23 @@  discard block
 block discarded – undo
124 129
     private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments)
125 130
     {
126 131
         $context = $ctx->context;
127
-        try {
132
+        try
133
+        {
128 134
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
129
-        } catch (\ReflectionException $e) {
135
+        }
136
+        catch (\ReflectionException $e)
137
+        {
130 138
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
131 139
         }
132 140
 
133 141
         $injector = $binding->injector;
134 142
 
135
-        try {
143
+        try
144
+        {
136 145
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
137 146
 
138
-            if (!$injectorInstance instanceof InjectorInterface) {
147
+            if (!$injectorInstance instanceof InjectorInterface)
148
+            {
139 149
                 throw new InjectionException(
140 150
                     \sprintf(
141 151
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -162,7 +172,8 @@  discard block
 block discarded – undo
162 172
                 default => (string)$context,
163 173
             });
164 174
 
165
-            if (!$reflection->isInstance($instance)) {
175
+            if (!$reflection->isInstance($instance))
176
+            {
166 177
                 throw new InjectionException(
167 178
                     \sprintf(
168 179
                         "Invalid injection response for '%s'.",
@@ -172,7 +183,9 @@  discard block
 block discarded – undo
172 183
             }
173 184
 
174 185
             return $instance;
175
-        } finally {
186
+        }
187
+        finally
188
+        {
176 189
             $this->state->bindings[$ctx->class] ??= $binding;
177 190
         }
178 191
     }
@@ -191,7 +204,8 @@  discard block
 block discarded – undo
191 204
             //Binding is pointing to something else
192 205
             : $this->make($binding->alias, $arguments, $context);
193 206
 
194
-        if ($binding->singleton && $arguments === []) {
207
+        if ($binding->singleton && $arguments === [])
208
+        {
195 209
             $this->state->singletons[$alias] = $result;
196 210
         }
197 211
 
@@ -200,7 +214,8 @@  discard block
 block discarded – undo
200 214
 
201 215
     private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed
202 216
     {
203
-        if ($context instanceof RetryContext) {
217
+        if ($context instanceof RetryContext)
218
+        {
204 219
             return $binding->fallbackFactory === null
205 220
                 ? throw new RecursiveProxyException(
206 221
                     $alias,
@@ -211,7 +226,8 @@  discard block
 block discarded – undo
211 226
 
212 227
         $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy());
213 228
 
214
-        if ($binding->singleton) {
229
+        if ($binding->singleton)
230
+        {
215 231
             $this->state->singletons[$alias] = $result;
216 232
         }
217 233
 
@@ -226,14 +242,16 @@  discard block
 block discarded – undo
226 242
     ): object {
227 243
         $avoidCache = $arguments !== [];
228 244
 
229
-        if ($avoidCache) {
245
+        if ($avoidCache)
246
+        {
230 247
             return $this->createInstance(
231 248
                 new Ctx(alias: $alias, class: $binding->value::class, context: $context),
232 249
                 $arguments,
233 250
             );
234 251
         }
235 252
 
236
-        if ($binding->singleton) {
253
+        if ($binding->singleton)
254
+        {
237 255
             $this->state->singletons[$alias] = $binding->value;
238 256
         }
239 257
 
@@ -263,11 +281,14 @@  discard block
 block discarded – undo
263 281
         array $arguments,
264 282
     ): mixed {
265 283
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
266
-        try {
284
+        try
285
+        {
267 286
             $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0
268 287
                 ? ($binding->factory)()
269 288
                 : $this->invoker->invoke($binding->factory, $arguments);
270
-        } catch (NotCallableException $e) {
289
+        }
290
+        catch (NotCallableException $e)
291
+        {
271 292
             throw new ContainerException(
272 293
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
273 294
                 $e->getCode(),
@@ -286,19 +307,24 @@  discard block
 block discarded – undo
286 307
     ): ?object {
287 308
         $avoidCache = $arguments !== [];
288 309
 
289
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
290
-            try {
310
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
311
+        {
312
+            try
313
+            {
291 314
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
292 315
 
293 316
                 $object = $this->createInstance(
294 317
                     new Ctx(alias: $alias, class: $alias, context: $context),
295 318
                     $arguments,
296 319
                 );
297
-                if ($avoidCache) {
320
+                if ($avoidCache)
321
+                {
298 322
                     return $object;
299 323
                 }
300 324
                 $binding->reference = WeakReference::create($object);
301
-            } catch (\Throwable) {
325
+            }
326
+            catch (\Throwable)
327
+            {
302 328
                 throw new ContainerException(
303 329
                     $this->tracer->combineTraceMessage(
304 330
                         \sprintf(
@@ -308,7 +334,9 @@  discard block
 block discarded – undo
308 334
                         )
309 335
                     )
310 336
                 );
311
-            } finally {
337
+            }
338
+            finally
339
+            {
312 340
                 $this->tracer->pop();
313 341
             }
314 342
         }
@@ -323,19 +351,26 @@  discard block
 block discarded – undo
323 351
     ): mixed {
324 352
         $parent = $this->scope->getParentFactory();
325 353
 
326
-        if ($parent !== null) {
327
-            try {
354
+        if ($parent !== null)
355
+        {
356
+            try
357
+            {
328 358
                 $this->tracer->push(false, ...[
329 359
                     'current scope' => $this->scope->getScopeName(),
330 360
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
331 361
                 ]);
332 362
                 /** @psalm-suppress TooManyArguments */
333 363
                 return $parent->make($alias, $parameters, $context);
334
-            } catch (BadScopeException $e) {
335
-                if ($this->scope->getScopeName() !== $e->getScope()) {
364
+            }
365
+            catch (BadScopeException $e)
366
+            {
367
+                if ($this->scope->getScopeName() !== $e->getScope())
368
+                {
336 369
                     throw $e;
337 370
                 }
338
-            } catch (ContainerExceptionInterface $e) {
371
+            }
372
+            catch (ContainerExceptionInterface $e)
373
+            {
339 374
                 $className = match (true) {
340 375
                     $e instanceof NotFoundException => NotFoundException::class,
341 376
                     $e instanceof RecursiveProxyException => throw $e,
@@ -345,19 +380,24 @@  discard block
 block discarded – undo
345 380
                     'Can\'t resolve `%s`.',
346 381
                     $alias,
347 382
                 )), previous: $e);
348
-            } finally {
383
+            }
384
+            finally
385
+            {
349 386
                 $this->tracer->pop(false);
350 387
             }
351 388
         }
352 389
 
353 390
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
354
-        try {
391
+        try
392
+        {
355 393
             //No direct instructions how to construct class, make is automatically
356 394
             return $this->autowire(
357 395
                 new Ctx(alias: $alias, class: $alias, context: $context),
358 396
                 $parameters,
359 397
             );
360
-        } finally {
398
+        }
399
+        finally
400
+        {
361 401
             $this->tracer->pop(false);
362 402
         }
363 403
     }
@@ -404,13 +444,16 @@  discard block
 block discarded – undo
404 444
         Ctx $ctx,
405 445
         array $arguments,
406 446
     ): object {
407
-        if ($this->options->checkScope) {
447
+        if ($this->options->checkScope)
448
+        {
408 449
             // Check scope name
409 450
             $ctx->reflection = new \ReflectionClass($instance);
410 451
             $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
411
-            if ($scopeName !== null) {
452
+            if ($scopeName !== null)
453
+            {
412 454
                 $scope = $this->scope;
413
-                while ($scope->getScopeName() !== $scopeName) {
455
+                while ($scope->getScopeName() !== $scopeName)
456
+                {
414 457
                     $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class);
415 458
                 }
416 459
             }
@@ -439,26 +482,33 @@  discard block
 block discarded – undo
439 482
         array $arguments,
440 483
     ): object {
441 484
         $class = $ctx->class;
442
-        try {
485
+        try
486
+        {
443 487
             $ctx->reflection = $reflection = new \ReflectionClass($class);
444
-        } catch (\ReflectionException $e) {
488
+        }
489
+        catch (\ReflectionException $e)
490
+        {
445 491
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
446 492
         }
447 493
 
448 494
         // Check scope name
449
-        if ($this->options->checkScope) {
495
+        if ($this->options->checkScope)
496
+        {
450 497
             $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
451
-            if ($scope !== null && $scope !== $this->scope->getScopeName()) {
498
+            if ($scope !== null && $scope !== $this->scope->getScopeName())
499
+            {
452 500
                 throw new BadScopeException($scope, $class);
453 501
             }
454 502
         }
455 503
 
456 504
         // We have to construct class using external injector when we know the exact context
457
-        if ($arguments === [] && $this->binder->hasInjector($class)) {
505
+        if ($arguments === [] && $this->binder->hasInjector($class))
506
+        {
458 507
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments);
459 508
         }
460 509
 
461
-        if (!$reflection->isInstantiable()) {
510
+        if (!$reflection->isInstantiable())
511
+        {
462 512
             $itIs = match (true) {
463 513
                 $reflection->isEnum() => 'Enum',
464 514
                 $reflection->isAbstract() => 'Abstract class',
@@ -471,12 +521,16 @@  discard block
 block discarded – undo
471 521
 
472 522
         $constructor = $reflection->getConstructor();
473 523
 
474
-        if ($constructor !== null) {
475
-            try {
524
+        if ($constructor !== null)
525
+        {
526
+            try
527
+            {
476 528
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
477 529
                 $this->tracer->push(true);
478 530
                 $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments);
479
-            } catch (ValidationException $e) {
531
+            }
532
+            catch (ValidationException $e)
533
+            {
480 534
                 throw new ContainerException(
481 535
                     $this->tracer->combineTraceMessage(
482 536
                         \sprintf(
@@ -486,22 +540,31 @@  discard block
 block discarded – undo
486 540
                         )
487 541
                     ),
488 542
                 );
489
-            } finally {
543
+            }
544
+            finally
545
+            {
490 546
                 $this->tracer->pop(true);
491 547
                 $this->tracer->pop(false);
492 548
             }
493
-            try {
549
+            try
550
+            {
494 551
                 // Using constructor with resolved arguments
495 552
                 $this->tracer->push(false, call: "$class::__construct", arguments: $args);
496 553
                 $this->tracer->push(true);
497 554
                 $instance = new $class(...$args);
498
-            } catch (\TypeError $e) {
555
+            }
556
+            catch (\TypeError $e)
557
+            {
499 558
                 throw new WrongTypeException($constructor, $e);
500
-            } finally {
559
+            }
560
+            finally
561
+            {
501 562
                 $this->tracer->pop(true);
502 563
                 $this->tracer->pop(false);
503 564
             }
504
-        } else {
565
+        }
566
+        else
567
+        {
505 568
             // No constructor specified
506 569
             $instance = $reflection->newInstance();
507 570
         }
@@ -519,13 +582,15 @@  discard block
 block discarded – undo
519 582
         $instance = $this->runInflector($instance);
520 583
 
521 584
         //Declarative singletons
522
-        if ($this->isSingleton($ctx)) {
585
+        if ($this->isSingleton($ctx))
586
+        {
523 587
             $this->state->singletons[$ctx->alias] = $instance;
524 588
         }
525 589
 
526 590
         // Register finalizer
527 591
         $finalizer = $this->getFinalizer($ctx, $instance);
528
-        if ($finalizer !== null) {
592
+        if ($finalizer !== null)
593
+        {
529 594
             $this->state->finalizers[] = $finalizer;
530 595
         }
531 596
 
@@ -537,12 +602,14 @@  discard block
 block discarded – undo
537 602
      */
538 603
     private function isSingleton(Ctx $ctx): bool
539 604
     {
540
-        if ($ctx->singleton === true) {
605
+        if ($ctx->singleton === true)
606
+        {
541 607
             return true;
542 608
         }
543 609
 
544 610
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
545
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
611
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
612
+        {
546 613
             return true;
547 614
         }
548 615
 
@@ -556,7 +623,8 @@  discard block
 block discarded – undo
556 623
          * @var Attribute\Finalize|null $attribute
557 624
          */
558 625
         $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance();
559
-        if ($attribute === null) {
626
+        if ($attribute === null)
627
+        {
560 628
             return null;
561 629
         }
562 630
 
@@ -570,10 +638,14 @@  discard block
 block discarded – undo
570 638
     {
571 639
         $scope = $this->scope;
572 640
 
573
-        while ($scope !== null) {
574
-            foreach ($this->state->inflectors as $class => $inflectors) {
575
-                if ($instance instanceof $class) {
576
-                    foreach ($inflectors as $inflector) {
641
+        while ($scope !== null)
642
+        {
643
+            foreach ($this->state->inflectors as $class => $inflectors)
644
+            {
645
+                if ($instance instanceof $class)
646
+                {
647
+                    foreach ($inflectors as $inflector)
648
+                    {
577 649
                         $instance = $inflector->getParametersCount() > 1
578 650
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
579 651
                             : ($inflector->inflector)($instance);
@@ -589,9 +661,12 @@  discard block
 block discarded – undo
589 661
 
590 662
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
591 663
     {
592
-        try {
664
+        try
665
+        {
593 666
             $this->resolver->validateArguments($reflection, $arguments);
594
-        } catch (\Throwable) {
667
+        }
668
+        catch (\Throwable)
669
+        {
595 670
             return false;
596 671
         }
597 672
 
Please login to merge, or discard this patch.
src/Core/src/Container.php 1 patch
Braces   +48 added lines, -22 removed lines patch added patch discarded remove patch
@@ -63,7 +63,8 @@  discard block
 block discarded – undo
63 63
         string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME,
64 64
         private Options $options = new Options(),
65 65
     ) {
66
-        if (\is_object($scopeName)) {
66
+        if (\is_object($scopeName))
67
+        {
67 68
             $scopeName = (string) $scopeName->value;
68 69
         }
69 70
 
@@ -178,23 +179,29 @@  discard block
 block discarded – undo
178 179
      */
179 180
     public function runScope(Scope|array $bindings, callable $scope): mixed
180 181
     {
181
-        if (!\is_array($bindings)) {
182
+        if (!\is_array($bindings))
183
+        {
182 184
             return $this->runIsolatedScope($bindings, $scope);
183 185
         }
184 186
 
185 187
         $binds = &$this->state->bindings;
186 188
         $singletons = &$this->state->singletons;
187 189
         $cleanup = $previous = $prevSin = [];
188
-        foreach ($bindings as $alias => $resolver) {
190
+        foreach ($bindings as $alias => $resolver)
191
+        {
189 192
             // Store previous bindings
190
-            if (isset($binds[$alias])) {
193
+            if (isset($binds[$alias]))
194
+            {
191 195
                 $previous[$alias] = $binds[$alias];
192
-            } else {
196
+            }
197
+            else
198
+            {
193 199
                 // Store bindings to be removed
194 200
                 $cleanup[] = $alias;
195 201
             }
196 202
             // Store previous singletons
197
-            if (isset($singletons[$alias])) {
203
+            if (isset($singletons[$alias]))
204
+            {
198 205
                 $prevSin[$alias] = $singletons[$alias];
199 206
                 unset($singletons[$alias]);
200 207
             }
@@ -202,21 +209,27 @@  discard block
 block discarded – undo
202 209
             $this->binder->bind($alias, $resolver);
203 210
         }
204 211
 
205
-        try {
212
+        try
213
+        {
206 214
             return ContainerScope::getContainer() !== $this
207 215
                 ? ContainerScope::runScope($this, $scope)
208 216
                 : $scope($this);
209
-        } finally {
217
+        }
218
+        finally
219
+        {
210 220
             // Remove new bindings
211
-            foreach ($cleanup as $alias) {
221
+            foreach ($cleanup as $alias)
222
+            {
212 223
                 unset($binds[$alias], $singletons[$alias]);
213 224
             }
214 225
             // Restore previous bindings
215
-            foreach ($previous as $alias => $resolver) {
226
+            foreach ($previous as $alias => $resolver)
227
+            {
216 228
                 $binds[$alias] = $resolver;
217 229
             }
218 230
             // Restore singletons
219
-            foreach ($prevSin as $alias => $instance) {
231
+            foreach ($prevSin as $alias => $instance)
232
+            {
220 233
                 $singletons[$alias] = $instance;
221 234
             }
222 235
         }
@@ -266,7 +279,8 @@  discard block
 block discarded – undo
266 279
      */
267 280
     public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void
268 281
     {
269
-        if ($force ?? $this->options->allowSingletonsRebinding) {
282
+        if ($force ?? $this->options->allowSingletonsRebinding)
283
+        {
270 284
             $this->binder->removeBinding($alias);
271 285
         }
272 286
 
@@ -337,8 +351,10 @@  discard block
 block discarded – undo
337 351
         ], $this->options);
338 352
 
339 353
         // Create container services
340
-        foreach ($container->config as $property => $class) {
341
-            if (\property_exists($container, $property)) {
354
+        foreach ($container->config as $property => $class)
355
+        {
356
+            if (\property_exists($container, $property))
357
+            {
342 358
                 $container->$property = $constructor->get($property, $class);
343 359
             }
344 360
         }
@@ -352,7 +368,8 @@  discard block
 block discarded – undo
352 368
     private function closeScope(): void
353 369
     {
354 370
         /** @psalm-suppress RedundantPropertyInitializationCheck */
355
-        if (!isset($this->scope)) {
371
+        if (!isset($this->scope))
372
+        {
356 373
             $this->destruct();
357 374
             return;
358 375
         }
@@ -361,10 +378,14 @@  discard block
 block discarded – undo
361 378
 
362 379
         // Run finalizers
363 380
         $errors = [];
364
-        foreach ($this->state->finalizers as $finalizer) {
365
-            try {
381
+        foreach ($this->state->finalizers as $finalizer)
382
+        {
383
+            try
384
+            {
366 385
                 $this->invoker->invoke($finalizer);
367
-            } catch (\Throwable $e) {
386
+            }
387
+            catch (\Throwable $e)
388
+            {
368 389
                 $errors[] = $e;
369 390
             }
370 391
         }
@@ -373,7 +394,8 @@  discard block
 block discarded – undo
373 394
         $this->destruct();
374 395
 
375 396
         // Throw collected errors
376
-        if ($errors !== []) {
397
+        if ($errors !== [])
398
+        {
377 399
             throw new FinalizersException($scopeName, $errors);
378 400
         }
379 401
     }
@@ -395,18 +417,22 @@  discard block
 block discarded – undo
395 417
         $container->scope->setParent($this, $this->scope, $this->factory);
396 418
 
397 419
         // Add specific bindings
398
-        foreach ($config->bindings as $alias => $resolver) {
420
+        foreach ($config->bindings as $alias => $resolver)
421
+        {
399 422
             $container->binder->bind($alias, $resolver);
400 423
         }
401 424
 
402 425
         return ContainerScope::runScope(
403 426
             $container,
404 427
             static function (self $container) use ($config, $closure): mixed {
405
-                try {
428
+                try
429
+                {
406 430
                     return $config->autowire
407 431
                         ? $container->invoke($closure)
408 432
                         : $closure($container);
409
-                } finally {
433
+                }
434
+                finally
435
+                {
410 436
                     $container->closeScope();
411 437
                 }
412 438
             }
Please login to merge, or discard this patch.