Passed
Push — master ( 72934f...dfb406 )
by Aleksei
05:59 queued 26s
created
src/Core/tests/Internal/Proxy/ProxyTest.php 1 patch
Braces   +22 added lines, -11 removed lines patch added patch discarded remove patch
@@ -35,7 +35,8 @@  discard block
 block discarded – undo
35 35
         $root->invoke(static function (
36 36
             #[Proxy] MockInterface $mock,
37 37
             #[Proxy(/*proxyOverloads: true*/)] EmptyInterface $empty,
38
-        ) use ($var) {
38
+        ) use ($var)
39
+        {
39 40
             /** @var MockInterfaceImpl $proxy */
40 41
             $proxy = $$var;
41 42
             $proxy->bar(name: 'foo'); // Possible to run
@@ -52,7 +53,8 @@  discard block
 block discarded – undo
52 53
 
53 54
         self::expectExceptionMessageMatches('/Call to undefined method/i');
54 55
 
55
-        $root->invoke(static function (#[Proxy] EmptyInterface $proxy) {
56
+        $root->invoke(static function (#[Proxy] EmptyInterface $proxy)
57
+        {
56 58
             $proxy->bar(name: 'foo'); // Possible to run
57 59
         });
58 60
     }
@@ -67,7 +69,8 @@  discard block
 block discarded – undo
67 69
         $root->invoke(static function (
68 70
             #[Proxy] MockInterface $mock,
69 71
             #[Proxy(/*proxyOverloads: true*/)] EmptyInterface $empty,
70
-        ) use ($var) {
72
+        ) use ($var)
73
+        {
71 74
             /** @var MockInterfaceImpl $proxy */
72 75
             $proxy = $$var;
73 76
             self::assertSame(['foo', 'bar', 'baz', 69], $proxy->extra('foo', 'bar', 'baz', 69));
@@ -87,7 +90,8 @@  discard block
 block discarded – undo
87 90
         $root->invoke(static function (
88 91
             #[Proxy] MockInterface $mock,
89 92
             #[Proxy(/*proxyOverloads: true*/)] EmptyInterface $empty,
90
-        ) use ($var) {
93
+        ) use ($var)
94
+        {
91 95
             /** @var MockInterfaceImpl $proxy */
92 96
             $proxy = $$var;
93 97
             self::assertSame(['foo', 'bar', 'baz', 69], $proxy->extraVariadic('foo', 'bar', 'baz', 69));
@@ -109,7 +113,8 @@  discard block
 block discarded – undo
109 113
         $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class);
110 114
         $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class);
111 115
 
112
-        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) {
116
+        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var)
117
+        {
113 118
             /** @var MockInterfaceImpl $proxy */
114 119
             $proxy = $$var;
115 120
             $str = 'bar';
@@ -129,7 +134,8 @@  discard block
 block discarded – undo
129 134
         $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class);
130 135
         $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class);
131 136
 
132
-        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) {
137
+        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var)
138
+        {
133 139
             /** @var MockInterfaceImpl $proxy */
134 140
             $proxy = $$var;
135 141
 
@@ -152,7 +158,8 @@  discard block
 block discarded – undo
152 158
         $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class);
153 159
         $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class);
154 160
 
155
-        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) {
161
+        $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var)
162
+        {
156 163
             /** @var MockInterfaceImpl $proxy */
157 164
             $proxy = $$var;
158 165
             $str1 = 'bar';
@@ -193,7 +200,8 @@  discard block
 block discarded – undo
193 200
         $this->assertInstanceOf($interface, $proxy);
194 201
         $this->assertNotInstanceOf(MockInterfaceImpl::class, $proxy);
195 202
 
196
-        $root->runScope(new Scope('foo'), static function (Container $container) use ($interface, $proxy) {
203
+        $root->runScope(new Scope('foo'), static function (Container $container) use ($interface, $proxy)
204
+        {
197 205
             $proxy->bar(name: 'foo'); // Possible to run
198 206
             self::assertSame('foo', $proxy->baz('foo', 42));
199 207
             self::assertSame(123, $proxy->qux(age: 123));
@@ -243,7 +251,8 @@  discard block
 block discarded – undo
243 251
         $proxy = $root->get($interface);
244 252
         $this->assertInstanceOf($interface, $proxy);
245 253
 
246
-        $root->runScope(new Scope('foo'), static function () use ($proxy) {
254
+        $root->runScope(new Scope('foo'), static function () use ($proxy)
255
+        {
247 256
             $proxy->bar(name: 'foo'); // Possible to run
248 257
             self::assertSame('foo', $proxy->baz('foo', 42));
249 258
             self::assertSame(123, $proxy->qux(age: 123));
@@ -271,7 +280,8 @@  discard block
 block discarded – undo
271 280
         $proxy = $root->get($interface);
272 281
         $this->assertInstanceOf($interface, $proxy);
273 282
 
274
-        $root->runScope(new Scope('foo'), static function () use ($proxy) {
283
+        $root->runScope(new Scope('foo'), static function () use ($proxy)
284
+        {
275 285
             $proxy->bar(name: 'foo'); // Possible to run
276 286
             self::assertSame('foo', $proxy->baz('foo', 42));
277 287
             self::assertSame(123, $proxy->qux(age: 123));
@@ -293,7 +303,8 @@  discard block
 block discarded – undo
293 303
         $root->getBinder('foo')->bindSingleton($interface, Stub\MockInterfaceImpl::class);
294 304
         $root->bindSingleton($interface, new Config\DeprecationProxy($interface, true, 'foo', '4.0'));
295 305
 
296
-        $root->runScope(new Scope('foo'), static function (Container $container) use ($interface) {
306
+        $root->runScope(new Scope('foo'), static function (Container $container) use ($interface)
307
+        {
297 308
             $proxy = $container->get($interface);
298 309
 
299 310
             $proxy->bar(name: 'foo'); // Possible to run
Please login to merge, or discard this patch.
src/Core/src/Config/DeprecationProxy.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -16,7 +16,8 @@
 block discarded – undo
16 16
         private readonly ?string $version = null,
17 17
         private readonly ?string $message = null,
18 18
     ) {
19
-        if (($scope === null || $version === null) && $message === null) {
19
+        if (($scope === null || $version === null) && $message === null)
20
+        {
20 21
             throw new \InvalidArgumentException('Scope and version or custom message must be provided.');
21 22
         }
22 23
 
Please login to merge, or discard this patch.
src/Core/src/Config/Proxy.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -13,7 +13,8 @@
 block discarded – undo
13 13
         protected readonly string $interface,
14 14
         public readonly bool $singleton = false,
15 15
     ) {
16
-        if (!\interface_exists($interface)) {
16
+        if (!\interface_exists($interface))
17
+        {
17 18
             throw new \InvalidArgumentException(\sprintf('Interface `%s` does not exist.', $interface));
18 19
         }
19 20
     }
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Braces   +121 added lines, -52 removed lines patch added patch discarded remove patch
@@ -65,17 +65,20 @@  discard block
 block discarded – undo
65 65
      */
66 66
     public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed
67 67
     {
68
-        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) {
68
+        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons))
69
+        {
69 70
             return $this->state->singletons[$alias];
70 71
         }
71 72
 
72 73
         $binding = $this->state->bindings[$alias] ?? null;
73 74
 
74
-        if ($binding === null) {
75
+        if ($binding === null)
76
+        {
75 77
             return $this->resolveWithoutBinding($alias, $parameters, $context);
76 78
         }
77 79
 
78
-        try {
80
+        try
81
+        {
79 82
             $this->tracer->push(
80 83
                 false,
81 84
                 action: 'resolve from binding',
@@ -105,7 +108,9 @@  discard block
 block discarded – undo
105 108
                     ->resolveWeakReference($binding, $alias, $context, $parameters),
106 109
                 default => $binding,
107 110
             };
108
-        } finally {
111
+        }
112
+        finally
113
+        {
109 114
             $this->state->bindings[$alias] ??= $binding;
110 115
             $this->tracer->pop(true);
111 116
             $this->tracer->pop(false);
@@ -119,18 +124,23 @@  discard block
 block discarded – undo
119 124
     private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments)
120 125
     {
121 126
         $context = $ctx->context;
122
-        try {
127
+        try
128
+        {
123 129
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
124
-        } catch (\ReflectionException $e) {
130
+        }
131
+        catch (\ReflectionException $e)
132
+        {
125 133
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
126 134
         }
127 135
 
128 136
         $injector = $binding->injector;
129 137
 
130
-        try {
138
+        try
139
+        {
131 140
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
132 141
 
133
-            if (!$injectorInstance instanceof InjectorInterface) {
142
+            if (!$injectorInstance instanceof InjectorInterface)
143
+            {
134 144
                 throw new InjectionException(
135 145
                     \sprintf(
136 146
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -157,7 +167,8 @@  discard block
 block discarded – undo
157 167
                 default => (string)$context,
158 168
             });
159 169
 
160
-            if (!$reflection->isInstance($instance)) {
170
+            if (!$reflection->isInstance($instance))
171
+            {
161 172
                 throw new InjectionException(
162 173
                     \sprintf(
163 174
                         "Invalid injection response for '%s'.",
@@ -167,7 +178,9 @@  discard block
 block discarded – undo
167 178
             }
168 179
 
169 180
             return $instance;
170
-        } finally {
181
+        }
182
+        finally
183
+        {
171 184
             $this->state->bindings[$ctx->class] ??= $binding;
172 185
         }
173 186
     }
@@ -186,7 +199,8 @@  discard block
 block discarded – undo
186 199
             //Binding is pointing to something else
187 200
             : $this->make($binding->alias, $arguments, $context);
188 201
 
189
-        if ($binding->singleton && $arguments === []) {
202
+        if ($binding->singleton && $arguments === [])
203
+        {
190 204
             $this->state->singletons[$alias] = $result;
191 205
         }
192 206
 
@@ -197,7 +211,8 @@  discard block
 block discarded – undo
197 211
     {
198 212
         $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy());
199 213
 
200
-        if ($binding->singleton) {
214
+        if ($binding->singleton)
215
+        {
201 216
             $this->state->singletons[$alias] = $result;
202 217
         }
203 218
 
@@ -238,11 +253,14 @@  discard block
 block discarded – undo
238 253
         array $arguments,
239 254
     ): mixed {
240 255
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
241
-        try {
256
+        try
257
+        {
242 258
             $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0
243 259
                 ? ($binding->factory)()
244 260
                 : $this->invoker->invoke($binding->factory, $arguments);
245
-        } catch (NotCallableException $e) {
261
+        }
262
+        catch (NotCallableException $e)
263
+        {
246 264
             throw new ContainerException(
247 265
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
248 266
                 $e->getCode(),
@@ -261,19 +279,24 @@  discard block
 block discarded – undo
261 279
     ): ?object {
262 280
         $avoidCache = $arguments !== [];
263 281
 
264
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
265
-            try {
282
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
283
+        {
284
+            try
285
+            {
266 286
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
267 287
 
268 288
                 $object = $this->createInstance(
269 289
                     new Ctx(alias: $alias, class: $alias, context: $context),
270 290
                     $arguments,
271 291
                 );
272
-                if ($avoidCache) {
292
+                if ($avoidCache)
293
+                {
273 294
                     return $object;
274 295
                 }
275 296
                 $binding->reference = WeakReference::create($object);
276
-            } catch (\Throwable) {
297
+            }
298
+            catch (\Throwable)
299
+            {
277 300
                 throw new ContainerException(
278 301
                     $this->tracer->combineTraceMessage(
279 302
                         \sprintf(
@@ -283,7 +306,9 @@  discard block
 block discarded – undo
283 306
                         )
284 307
                     )
285 308
                 );
286
-            } finally {
309
+            }
310
+            finally
311
+            {
287 312
                 $this->tracer->pop();
288 313
             }
289 314
         }
@@ -298,19 +323,26 @@  discard block
 block discarded – undo
298 323
     ): mixed {
299 324
         $parent = $this->scope->getParentFactory();
300 325
 
301
-        if ($parent !== null) {
302
-            try {
326
+        if ($parent !== null)
327
+        {
328
+            try
329
+            {
303 330
                 $this->tracer->push(false, ...[
304 331
                     'current scope' => $this->scope->getScopeName(),
305 332
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
306 333
                 ]);
307 334
                 /** @psalm-suppress TooManyArguments */
308 335
                 return $parent->make($alias, $parameters, $context);
309
-            } catch (BadScopeException $e) {
310
-                if ($this->scope->getScopeName() !== $e->getScope()) {
336
+            }
337
+            catch (BadScopeException $e)
338
+            {
339
+                if ($this->scope->getScopeName() !== $e->getScope())
340
+                {
311 341
                     throw $e;
312 342
                 }
313
-            } catch (ContainerExceptionInterface $e) {
343
+            }
344
+            catch (ContainerExceptionInterface $e)
345
+            {
314 346
                 $className = match (true) {
315 347
                     $e instanceof NotFoundException => NotFoundException::class,
316 348
                     default => ContainerException::class,
@@ -319,19 +351,24 @@  discard block
 block discarded – undo
319 351
                     'Can\'t resolve `%s`.',
320 352
                     $alias,
321 353
                 )), previous: $e);
322
-            } finally {
354
+            }
355
+            finally
356
+            {
323 357
                 $this->tracer->pop(false);
324 358
             }
325 359
         }
326 360
 
327 361
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
328
-        try {
362
+        try
363
+        {
329 364
             //No direct instructions how to construct class, make is automatically
330 365
             return $this->autowire(
331 366
                 new Ctx(alias: $alias, class: $alias, context: $context),
332 367
                 $parameters,
333 368
             );
334
-        } finally {
369
+        }
370
+        finally
371
+        {
335 372
             $this->tracer->pop(false);
336 373
         }
337 374
     }
@@ -381,7 +418,8 @@  discard block
 block discarded – undo
381 418
         // Check scope name
382 419
         $ctx->reflection = new \ReflectionClass($instance);
383 420
         $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
384
-        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) {
421
+        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName())
422
+        {
385 423
             throw new BadScopeException($scopeName, $instance::class);
386 424
         }
387 425
 
@@ -408,24 +446,30 @@  discard block
 block discarded – undo
408 446
         array $arguments,
409 447
     ): object {
410 448
         $class = $ctx->class;
411
-        try {
449
+        try
450
+        {
412 451
             $ctx->reflection = $reflection = new \ReflectionClass($class);
413
-        } catch (\ReflectionException $e) {
452
+        }
453
+        catch (\ReflectionException $e)
454
+        {
414 455
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
415 456
         }
416 457
 
417 458
         // Check scope name
418 459
         $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
419
-        if ($scope !== null && $scope !== $this->scope->getScopeName()) {
460
+        if ($scope !== null && $scope !== $this->scope->getScopeName())
461
+        {
420 462
             throw new BadScopeException($scope, $class);
421 463
         }
422 464
 
423 465
         // We have to construct class using external injector when we know the exact context
424
-        if ($arguments === [] && $this->binder->hasInjector($class)) {
466
+        if ($arguments === [] && $this->binder->hasInjector($class))
467
+        {
425 468
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments);
426 469
         }
427 470
 
428
-        if (!$reflection->isInstantiable()) {
471
+        if (!$reflection->isInstantiable())
472
+        {
429 473
             $itIs = match (true) {
430 474
                 $reflection->isEnum() => 'Enum',
431 475
                 $reflection->isAbstract() => 'Abstract class',
@@ -438,12 +482,16 @@  discard block
 block discarded – undo
438 482
 
439 483
         $constructor = $reflection->getConstructor();
440 484
 
441
-        if ($constructor !== null) {
442
-            try {
485
+        if ($constructor !== null)
486
+        {
487
+            try
488
+            {
443 489
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
444 490
                 $this->tracer->push(true);
445 491
                 $args = $this->resolver->resolveArguments($constructor, $arguments);
446
-            } catch (ValidationException $e) {
492
+            }
493
+            catch (ValidationException $e)
494
+            {
447 495
                 throw new ContainerException(
448 496
                     $this->tracer->combineTraceMessage(
449 497
                         \sprintf(
@@ -453,22 +501,31 @@  discard block
 block discarded – undo
453 501
                         )
454 502
                     ),
455 503
                 );
456
-            } finally {
504
+            }
505
+            finally
506
+            {
457 507
                 $this->tracer->pop(true);
458 508
                 $this->tracer->pop(false);
459 509
             }
460
-            try {
510
+            try
511
+            {
461 512
                 // Using constructor with resolved arguments
462 513
                 $this->tracer->push(false, call: "$class::__construct", arguments: $args);
463 514
                 $this->tracer->push(true);
464 515
                 $instance = new $class(...$args);
465
-            } catch (\TypeError $e) {
516
+            }
517
+            catch (\TypeError $e)
518
+            {
466 519
                 throw new WrongTypeException($constructor, $e);
467
-            } finally {
520
+            }
521
+            finally
522
+            {
468 523
                 $this->tracer->pop(true);
469 524
                 $this->tracer->pop(false);
470 525
             }
471
-        } else {
526
+        }
527
+        else
528
+        {
472 529
             // No constructor specified
473 530
             $instance = $reflection->newInstance();
474 531
         }
@@ -486,13 +543,15 @@  discard block
 block discarded – undo
486 543
         $instance = $this->runInflector($instance);
487 544
 
488 545
         //Declarative singletons
489
-        if ($this->isSingleton($ctx)) {
546
+        if ($this->isSingleton($ctx))
547
+        {
490 548
             $this->state->singletons[$ctx->alias] = $instance;
491 549
         }
492 550
 
493 551
         // Register finalizer
494 552
         $finalizer = $this->getFinalizer($ctx, $instance);
495
-        if ($finalizer !== null) {
553
+        if ($finalizer !== null)
554
+        {
496 555
             $this->state->finalizers[] = $finalizer;
497 556
         }
498 557
 
@@ -504,12 +563,14 @@  discard block
 block discarded – undo
504 563
      */
505 564
     private function isSingleton(Ctx $ctx): bool
506 565
     {
507
-        if ($ctx->singleton === true) {
566
+        if ($ctx->singleton === true)
567
+        {
508 568
             return true;
509 569
         }
510 570
 
511 571
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
512
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
572
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
573
+        {
513 574
             return true;
514 575
         }
515 576
 
@@ -523,7 +584,8 @@  discard block
 block discarded – undo
523 584
          * @var Attribute\Finalize|null $attribute
524 585
          */
525 586
         $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance();
526
-        if ($attribute === null) {
587
+        if ($attribute === null)
588
+        {
527 589
             return null;
528 590
         }
529 591
 
@@ -537,10 +599,14 @@  discard block
 block discarded – undo
537 599
     {
538 600
         $scope = $this->scope;
539 601
 
540
-        while ($scope !== null) {
541
-            foreach ($this->state->inflectors as $class => $inflectors) {
542
-                if ($instance instanceof $class) {
543
-                    foreach ($inflectors as $inflector) {
602
+        while ($scope !== null)
603
+        {
604
+            foreach ($this->state->inflectors as $class => $inflectors)
605
+            {
606
+                if ($instance instanceof $class)
607
+                {
608
+                    foreach ($inflectors as $inflector)
609
+                    {
544 610
                         $instance = $inflector->getParametersCount() > 1
545 611
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
546 612
                             : ($inflector->inflector)($instance);
@@ -556,9 +622,12 @@  discard block
 block discarded – undo
556 622
 
557 623
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
558 624
     {
559
-        try {
625
+        try
626
+        {
560 627
             $this->resolver->validateArguments($reflection, $arguments);
561
-        } catch (\Throwable) {
628
+        }
629
+        catch (\Throwable)
630
+        {
562 631
             return false;
563 632
         }
564 633
 
Please login to merge, or discard this patch.