Passed
Pull Request — master (#1175)
by Aleksei
11:58
created
src/Core/tests/Scope/SideEffectTest.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -25,10 +25,10 @@  discard block
 block discarded – undo
25 25
         $root = new Container();
26 26
         $root->bind(LoggerInterface::class, KVLogger::class);
27 27
 
28
-        $root->runScope(new Scope(), static function (Container $c1) {
28
+        $root->runScope(new Scope(), static function (Container $c1){
29 29
             $c1->bind(LoggerInterface::class, FileLogger::class);
30 30
 
31
-            $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger) {
31
+            $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger){
32 32
                 // from the $root container
33 33
                 self::assertInstanceOf(KVLogger::class, $carrier->logger);
34 34
                 // from the $c1 container
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
         $root = new Container();
43 43
         $root->bind(LoggerInterface::class, KVLogger::class);
44 44
 
45
-        $root->runScope(new Scope(), static function (Container $c1) {
45
+        $root->runScope(new Scope(), static function (Container $c1){
46 46
             $c1->bind(LoggerInterface::class, FileLogger::class);
47 47
 
48 48
             self::assertInstanceOf(
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
             ->getBinder('test')
60 60
             ->bind(ScopeIndicatorLogger::class, new Container\Autowire(ScopeIndicatorLogger::class));
61 61
 
62
-        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
62
+        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger){
63 63
             return $logger;
64 64
         });
65 65
 
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
             ->getBinder('test')
75 75
             ->bind(ScopeIndicatorLogger::class, new Alias(ScopeIndicatorLogger::class));
76 76
 
77
-        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
77
+        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger){
78 78
             return $logger;
79 79
         });
80 80
 
Please login to merge, or discard this patch.
Braces   +10 added lines, -5 removed lines patch added patch discarded remove patch
@@ -25,10 +25,12 @@  discard block
 block discarded – undo
25 25
         $root = new Container();
26 26
         $root->bind(LoggerInterface::class, KVLogger::class);
27 27
 
28
-        $root->runScope(new Scope(), static function (Container $c1) {
28
+        $root->runScope(new Scope(), static function (Container $c1)
29
+        {
29 30
             $c1->bind(LoggerInterface::class, FileLogger::class);
30 31
 
31
-            $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger) {
32
+            $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger)
33
+            {
32 34
                 // from the $root container
33 35
                 self::assertInstanceOf(KVLogger::class, $carrier->logger);
34 36
                 // from the $c1 container
@@ -42,7 +44,8 @@  discard block
 block discarded – undo
42 44
         $root = new Container();
43 45
         $root->bind(LoggerInterface::class, KVLogger::class);
44 46
 
45
-        $root->runScope(new Scope(), static function (Container $c1) {
47
+        $root->runScope(new Scope(), static function (Container $c1)
48
+        {
46 49
             $c1->bind(LoggerInterface::class, FileLogger::class);
47 50
 
48 51
             self::assertInstanceOf(
@@ -59,7 +62,8 @@  discard block
 block discarded – undo
59 62
             ->getBinder('test')
60 63
             ->bind(ScopeIndicatorLogger::class, new Container\Autowire(ScopeIndicatorLogger::class));
61 64
 
62
-        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
65
+        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger)
66
+        {
63 67
             return $logger;
64 68
         });
65 69
 
@@ -74,7 +78,8 @@  discard block
 block discarded – undo
74 78
             ->getBinder('test')
75 79
             ->bind(ScopeIndicatorLogger::class, new Alias(ScopeIndicatorLogger::class));
76 80
 
77
-        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
81
+        $logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger)
82
+        {
78 83
             return $logger;
79 84
         });
80 85
 
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Spacing   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -68,19 +68,19 @@  discard block
 block discarded – undo
68 68
      *
69 69
      * @throws \Throwable
70 70
      */
71
-    public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed
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
             return $this->state->singletons[$alias];
75 75
         }
76 76
 
77 77
         $binding = $this->state->bindings[$alias] ?? null;
78 78
 
79
-        if ($binding === null) {
79
+        if ($binding === null){
80 80
             return $this->resolveWithoutBinding($alias, $parameters, $context);
81 81
         }
82 82
 
83
-        try {
83
+        try{
84 84
             $this->tracer->push(
85 85
                 false,
86 86
                 action: 'resolve from binding',
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
                     ->resolveWeakReference($binding, $alias, $context, $parameters),
111 111
                 default => $binding,
112 112
             };
113
-        } finally {
113
+        }finally{
114 114
             $this->state->bindings[$alias] ??= $binding;
115 115
             $this->tracer->pop(true);
116 116
             $this->tracer->pop(false);
@@ -124,18 +124,18 @@  discard block
 block discarded – undo
124 124
     private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments)
125 125
     {
126 126
         $context = $ctx->context;
127
-        try {
127
+        try{
128 128
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
129
-        } catch (\ReflectionException $e) {
129
+        }catch (\ReflectionException $e){
130 130
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
131 131
         }
132 132
 
133 133
         $injector = $binding->injector;
134 134
 
135
-        try {
135
+        try{
136 136
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
137 137
 
138
-            if (!$injectorInstance instanceof InjectorInterface) {
138
+            if (!$injectorInstance instanceof InjectorInterface){
139 139
                 throw new InjectionException(
140 140
                     \sprintf(
141 141
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
                 default => (string)$context,
163 163
             });
164 164
 
165
-            if (!$reflection->isInstance($instance)) {
165
+            if (!$reflection->isInstance($instance)){
166 166
                 throw new InjectionException(
167 167
                     \sprintf(
168 168
                         "Invalid injection response for '%s'.",
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
             }
173 173
 
174 174
             return $instance;
175
-        } finally {
175
+        }finally{
176 176
             $this->state->bindings[$ctx->class] ??= $binding;
177 177
         }
178 178
     }
@@ -180,27 +180,27 @@  discard block
 block discarded – undo
180 180
     private function resolveAlias(
181 181
         Config\Alias $binding,
182 182
         string $alias,
183
-        Stringable|string|null $context,
183
+        Stringable | string | null $context,
184 184
         array $arguments,
185 185
     ): mixed {
186 186
         $result = $binding->alias === $alias
187 187
             ? $this->autowire(
188
-                new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton),
188
+                new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton),
189 189
                 $arguments,
190 190
             )
191 191
             //Binding is pointing to something else
192 192
             : $this->make($binding->alias, $arguments, $context);
193 193
 
194
-        if ($binding->singleton && $arguments === []) {
194
+        if ($binding->singleton && $arguments === []){
195 195
             $this->state->singletons[$alias] = $result;
196 196
         }
197 197
 
198 198
         return $result;
199 199
     }
200 200
 
201
-    private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed
201
+    private function resolveProxy(Config\Proxy $binding, string $alias, Stringable | string | null $context): mixed
202 202
     {
203
-        if ($context instanceof RetryContext) {
203
+        if ($context instanceof RetryContext){
204 204
             return $binding->fallbackFactory === null
205 205
                 ? throw new RecursiveProxyException(
206 206
                     $alias,
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
 
212 212
         $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy());
213 213
 
214
-        if ($binding->singleton) {
214
+        if ($binding->singleton){
215 215
             $this->state->singletons[$alias] = $result;
216 216
         }
217 217
 
@@ -221,13 +221,13 @@  discard block
 block discarded – undo
221 221
     private function resolveShared(
222 222
         Config\Shared $binding,
223 223
         string $alias,
224
-        Stringable|string|null $context,
224
+        Stringable | string | null $context,
225 225
         array $arguments,
226 226
     ): object {
227 227
         $avoidCache = $arguments !== [];
228 228
         return $avoidCache
229 229
             ? $this->createInstance(
230
-                new Ctx(alias: $alias, class: $binding->value::class, context: $context),
230
+                new Ctx(alias : $alias, class : $binding->value::class, context : $context),
231 231
                 $arguments,
232 232
             )
233 233
             : $binding->value;
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
     private function resolveAutowire(
237 237
         Config\Autowire $binding,
238 238
         string $alias,
239
-        Stringable|string|null $context,
239
+        Stringable | string | null $context,
240 240
         array $arguments,
241 241
     ): mixed {
242 242
         $target = $binding->autowire->alias;
@@ -250,17 +250,17 @@  discard block
 block discarded – undo
250 250
     }
251 251
 
252 252
     private function resolveFactory(
253
-        Config\Factory|Config\DeferredFactory $binding,
253
+        Config\Factory | Config\DeferredFactory $binding,
254 254
         string $alias,
255
-        Stringable|string|null $context,
255
+        Stringable | string | null $context,
256 256
         array $arguments,
257 257
     ): mixed {
258 258
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
259
-        try {
259
+        try{
260 260
             $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0
261 261
                 ? ($binding->factory)()
262 262
                 : $this->invoker->invoke($binding->factory, $arguments);
263
-        } catch (NotCallableException $e) {
263
+        }catch (NotCallableException $e){
264 264
             throw new ContainerException(
265 265
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
266 266
                 $e->getCode(),
@@ -274,24 +274,24 @@  discard block
 block discarded – undo
274 274
     private function resolveWeakReference(
275 275
         Config\WeakReference $binding,
276 276
         string $alias,
277
-        Stringable|string|null $context,
277
+        Stringable | string | null $context,
278 278
         array $arguments,
279 279
     ): ?object {
280 280
         $avoidCache = $arguments !== [];
281 281
 
282
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
283
-            try {
282
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){
283
+            try{
284 284
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
285 285
 
286 286
                 $object = $this->createInstance(
287 287
                     new Ctx(alias: $alias, class: $alias, context: $context),
288 288
                     $arguments,
289 289
                 );
290
-                if ($avoidCache) {
290
+                if ($avoidCache){
291 291
                     return $object;
292 292
                 }
293 293
                 $binding->reference = WeakReference::create($object);
294
-            } catch (\Throwable) {
294
+            }catch (\Throwable){
295 295
                 throw new ContainerException(
296 296
                     $this->tracer->combineTraceMessage(
297 297
                         \sprintf(
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
                         )
302 302
                     )
303 303
                 );
304
-            } finally {
304
+            }finally{
305 305
                 $this->tracer->pop();
306 306
             }
307 307
         }
@@ -312,23 +312,23 @@  discard block
 block discarded – undo
312 312
     private function resolveWithoutBinding(
313 313
         string $alias,
314 314
         array $parameters = [],
315
-        Stringable|string|null $context = null
315
+        Stringable | string | null $context = null
316 316
     ): mixed {
317 317
         $parent = $this->scope->getParentFactory();
318 318
 
319
-        if ($parent !== null) {
320
-            try {
319
+        if ($parent !== null){
320
+            try{
321 321
                 $this->tracer->push(false, ...[
322 322
                     'current scope' => $this->scope->getScopeName(),
323 323
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
324 324
                 ]);
325 325
                 /** @psalm-suppress TooManyArguments */
326 326
                 return $parent->make($alias, $parameters, $context);
327
-            } catch (BadScopeException $e) {
328
-                if ($this->scope->getScopeName() !== $e->getScope()) {
327
+            }catch (BadScopeException $e){
328
+                if ($this->scope->getScopeName() !== $e->getScope()){
329 329
                     throw $e;
330 330
                 }
331
-            } catch (ContainerExceptionInterface $e) {
331
+            }catch (ContainerExceptionInterface $e){
332 332
                 $className = match (true) {
333 333
                     $e instanceof NotFoundException => NotFoundException::class,
334 334
                     $e instanceof RecursiveProxyException => throw $e,
@@ -338,19 +338,19 @@  discard block
 block discarded – undo
338 338
                     'Can\'t resolve `%s`.',
339 339
                     $alias,
340 340
                 )), previous: $e);
341
-            } finally {
341
+            }finally{
342 342
                 $this->tracer->pop(false);
343 343
             }
344 344
         }
345 345
 
346 346
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
347
-        try {
347
+        try{
348 348
             //No direct instructions how to construct class, make is automatically
349 349
             return $this->autowire(
350 350
                 new Ctx(alias: $alias, class: $alias, context: $context),
351 351
                 $parameters,
352 352
             );
353
-        } finally {
353
+        }finally{
354 354
             $this->tracer->pop(false);
355 355
         }
356 356
     }
@@ -371,7 +371,7 @@  discard block
 block discarded – undo
371 371
                 &&
372 372
                 (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class))
373 373
         ))
374
-        ) {
374
+        ){
375 375
             throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf(
376 376
                 'Can\'t resolve `%s`: undefined class or binding `%s`.',
377 377
                 $this->tracer->getRootAlias(),
@@ -397,13 +397,13 @@  discard block
 block discarded – undo
397 397
         Ctx $ctx,
398 398
         array $arguments,
399 399
     ): object {
400
-        if ($this->options->checkScope) {
400
+        if ($this->options->checkScope){
401 401
             // Check scope name
402 402
             $ctx->reflection = new \ReflectionClass($instance);
403 403
             $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
404
-            if ($scopeName !== null) {
404
+            if ($scopeName !== null){
405 405
                 $scope = $this->scope;
406
-                while ($scope->getScopeName() !== $scopeName) {
406
+                while ($scope->getScopeName() !== $scopeName){
407 407
                     $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class);
408 408
                 }
409 409
             }
@@ -432,26 +432,26 @@  discard block
 block discarded – undo
432 432
         array $arguments,
433 433
     ): object {
434 434
         $class = $ctx->class;
435
-        try {
435
+        try{
436 436
             $ctx->reflection = $reflection = new \ReflectionClass($class);
437
-        } catch (\ReflectionException $e) {
437
+        }catch (\ReflectionException $e){
438 438
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
439 439
         }
440 440
 
441 441
         // Check scope name
442
-        if ($this->options->checkScope) {
442
+        if ($this->options->checkScope){
443 443
             $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
444
-            if ($scope !== null && $scope !== $this->scope->getScopeName()) {
444
+            if ($scope !== null && $scope !== $this->scope->getScopeName()){
445 445
                 throw new BadScopeException($scope, $class);
446 446
             }
447 447
         }
448 448
 
449 449
         // We have to construct class using external injector when we know the exact context
450
-        if ($arguments === [] && $this->binder->hasInjector($class)) {
450
+        if ($arguments === [] && $this->binder->hasInjector($class)){
451 451
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments);
452 452
         }
453 453
 
454
-        if (!$reflection->isInstantiable()) {
454
+        if (!$reflection->isInstantiable()){
455 455
             $itIs = match (true) {
456 456
                 $reflection->isEnum() => 'Enum',
457 457
                 $reflection->isAbstract() => 'Abstract class',
@@ -464,12 +464,12 @@  discard block
 block discarded – undo
464 464
 
465 465
         $constructor = $reflection->getConstructor();
466 466
 
467
-        if ($constructor !== null) {
468
-            try {
467
+        if ($constructor !== null){
468
+            try{
469 469
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
470 470
                 $this->tracer->push(true);
471 471
                 $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments);
472
-            } catch (ValidationException $e) {
472
+            }catch (ValidationException $e){
473 473
                 throw new ContainerException(
474 474
                     $this->tracer->combineTraceMessage(
475 475
                         \sprintf(
@@ -479,22 +479,22 @@  discard block
 block discarded – undo
479 479
                         )
480 480
                     ),
481 481
                 );
482
-            } finally {
482
+            }finally{
483 483
                 $this->tracer->pop(true);
484 484
                 $this->tracer->pop(false);
485 485
             }
486
-            try {
486
+            try{
487 487
                 // Using constructor with resolved arguments
488 488
                 $this->tracer->push(false, call: "$class::__construct", arguments: $args);
489 489
                 $this->tracer->push(true);
490 490
                 $instance = new $class(...$args);
491
-            } catch (\TypeError $e) {
491
+            }catch (\TypeError $e){
492 492
                 throw new WrongTypeException($constructor, $e);
493
-            } finally {
493
+            }finally{
494 494
                 $this->tracer->pop(true);
495 495
                 $this->tracer->pop(false);
496 496
             }
497
-        } else {
497
+        }else{
498 498
             // No constructor specified
499 499
             $instance = $reflection->newInstance();
500 500
         }
@@ -512,13 +512,13 @@  discard block
 block discarded – undo
512 512
         $instance = $this->runInflector($instance);
513 513
 
514 514
         //Declarative singletons
515
-        if ($this->isSingleton($ctx)) {
515
+        if ($this->isSingleton($ctx)){
516 516
             $this->state->singletons[$ctx->alias] = $instance;
517 517
         }
518 518
 
519 519
         // Register finalizer
520 520
         $finalizer = $this->getFinalizer($ctx, $instance);
521
-        if ($finalizer !== null) {
521
+        if ($finalizer !== null){
522 522
             $this->state->finalizers[] = $finalizer;
523 523
         }
524 524
 
@@ -530,12 +530,12 @@  discard block
 block discarded – undo
530 530
      */
531 531
     private function isSingleton(Ctx $ctx): bool
532 532
     {
533
-        if ($ctx->singleton === true) {
533
+        if ($ctx->singleton === true){
534 534
             return true;
535 535
         }
536 536
 
537 537
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
538
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
538
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class)){
539 539
             return true;
540 540
         }
541 541
 
@@ -549,7 +549,7 @@  discard block
 block discarded – undo
549 549
          * @var Attribute\Finalize|null $attribute
550 550
          */
551 551
         $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance();
552
-        if ($attribute === null) {
552
+        if ($attribute === null){
553 553
             return null;
554 554
         }
555 555
 
@@ -563,10 +563,10 @@  discard block
 block discarded – undo
563 563
     {
564 564
         $scope = $this->scope;
565 565
 
566
-        while ($scope !== null) {
567
-            foreach ($this->state->inflectors as $class => $inflectors) {
568
-                if ($instance instanceof $class) {
569
-                    foreach ($inflectors as $inflector) {
566
+        while ($scope !== null){
567
+            foreach ($this->state->inflectors as $class => $inflectors){
568
+                if ($instance instanceof $class){
569
+                    foreach ($inflectors as $inflector){
570 570
                         $instance = $inflector->getParametersCount() > 1
571 571
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
572 572
                             : ($inflector->inflector)($instance);
@@ -582,9 +582,9 @@  discard block
 block discarded – undo
582 582
 
583 583
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
584 584
     {
585
-        try {
585
+        try{
586 586
             $this->resolver->validateArguments($reflection, $arguments);
587
-        } catch (\Throwable) {
587
+        }catch (\Throwable){
588 588
             return false;
589 589
         }
590 590
 
Please login to merge, or discard this patch.
src/Core/src/Container/Autowire.php 1 patch
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
     public function __construct(
27 27
         public readonly string $alias,
28 28
         public readonly array $parameters = []
29
-    ) {
29
+    ){
30 30
     }
31 31
 
32 32
     public static function __set_state(array $anArray): static
@@ -41,22 +41,22 @@  discard block
 block discarded – undo
41 41
      */
42 42
     public static function wire(mixed $definition): Autowire
43 43
     {
44
-        if ($definition instanceof self) {
44
+        if ($definition instanceof self){
45 45
             return $definition;
46 46
         }
47 47
 
48
-        if (\is_string($definition)) {
48
+        if (\is_string($definition)){
49 49
             return new self($definition);
50 50
         }
51 51
 
52
-        if (\is_array($definition) && isset($definition['class'])) {
52
+        if (\is_array($definition) && isset($definition['class'])){
53 53
             return new self(
54 54
                 $definition['class'],
55 55
                 $definition['options'] ?? $definition['params'] ?? []
56 56
             );
57 57
         }
58 58
 
59
-        if (\is_object($definition)) {
59
+        if (\is_object($definition)){
60 60
             $autowire = new self($definition::class, []);
61 61
             $autowire->target = $definition;
62 62
             return $autowire;
Please login to merge, or discard this patch.