Passed
Pull Request — master (#1077)
by Maxim
12:35
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/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
 
@@ -201,7 +215,8 @@  discard block
 block discarded – undo
201 215
     ): mixed {
202 216
         $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy());
203 217
 
204
-        if ($binding->singleton && $arguments === []) {
218
+        if ($binding->singleton && $arguments === [])
219
+        {
205 220
             $this->state->singletons[$alias] = $result;
206 221
         }
207 222
 
@@ -242,11 +257,14 @@  discard block
 block discarded – undo
242 257
         array $arguments,
243 258
     ): mixed {
244 259
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
245
-        try {
260
+        try
261
+        {
246 262
             $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0
247 263
                 ? ($binding->factory)()
248 264
                 : $this->invoker->invoke($binding->factory, $arguments);
249
-        } catch (NotCallableException $e) {
265
+        }
266
+        catch (NotCallableException $e)
267
+        {
250 268
             throw new ContainerException(
251 269
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
252 270
                 $e->getCode(),
@@ -265,19 +283,24 @@  discard block
 block discarded – undo
265 283
     ): ?object {
266 284
         $avoidCache = $arguments !== [];
267 285
 
268
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
269
-            try {
286
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
287
+        {
288
+            try
289
+            {
270 290
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
271 291
 
272 292
                 $object = $this->createInstance(
273 293
                     new Ctx(alias: $alias, class: $alias, context: $context),
274 294
                     $arguments,
275 295
                 );
276
-                if ($avoidCache) {
296
+                if ($avoidCache)
297
+                {
277 298
                     return $object;
278 299
                 }
279 300
                 $binding->reference = WeakReference::create($object);
280
-            } catch (\Throwable) {
301
+            }
302
+            catch (\Throwable)
303
+            {
281 304
                 throw new ContainerException(
282 305
                     $this->tracer->combineTraceMessage(
283 306
                         \sprintf(
@@ -287,7 +310,9 @@  discard block
 block discarded – undo
287 310
                         )
288 311
                     )
289 312
                 );
290
-            } finally {
313
+            }
314
+            finally
315
+            {
291 316
                 $this->tracer->pop();
292 317
             }
293 318
         }
@@ -302,19 +327,26 @@  discard block
 block discarded – undo
302 327
     ): mixed {
303 328
         $parent = $this->scope->getParentFactory();
304 329
 
305
-        if ($parent !== null) {
306
-            try {
330
+        if ($parent !== null)
331
+        {
332
+            try
333
+            {
307 334
                 $this->tracer->push(false, ...[
308 335
                     'current scope' => $this->scope->getScopeName(),
309 336
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
310 337
                 ]);
311 338
                 /** @psalm-suppress TooManyArguments */
312 339
                 return $parent->make($alias, $parameters, $context);
313
-            } catch (BadScopeException $e) {
314
-                if ($this->scope->getScopeName() !== $e->getScope()) {
340
+            }
341
+            catch (BadScopeException $e)
342
+            {
343
+                if ($this->scope->getScopeName() !== $e->getScope())
344
+                {
315 345
                     throw $e;
316 346
                 }
317
-            } catch (ContainerExceptionInterface $e) {
347
+            }
348
+            catch (ContainerExceptionInterface $e)
349
+            {
318 350
                 $className = match (true) {
319 351
                     $e instanceof NotFoundException => NotFoundException::class,
320 352
                     default => ContainerException::class,
@@ -323,19 +355,24 @@  discard block
 block discarded – undo
323 355
                     'Can\'t resolve `%s`.',
324 356
                     $alias,
325 357
                 )), previous: $e);
326
-            } finally {
358
+            }
359
+            finally
360
+            {
327 361
                 $this->tracer->pop(false);
328 362
             }
329 363
         }
330 364
 
331 365
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
332
-        try {
366
+        try
367
+        {
333 368
             //No direct instructions how to construct class, make is automatically
334 369
             return $this->autowire(
335 370
                 new Ctx(alias: $alias, class: $alias, context: $context),
336 371
                 $parameters,
337 372
             );
338
-        } finally {
373
+        }
374
+        finally
375
+        {
339 376
             $this->tracer->pop(false);
340 377
         }
341 378
     }
@@ -385,7 +422,8 @@  discard block
 block discarded – undo
385 422
         // Check scope name
386 423
         $ctx->reflection = new \ReflectionClass($instance);
387 424
         $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
388
-        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) {
425
+        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName())
426
+        {
389 427
             throw new BadScopeException($scopeName, $instance::class);
390 428
         }
391 429
 
@@ -412,24 +450,30 @@  discard block
 block discarded – undo
412 450
         array $arguments,
413 451
     ): object {
414 452
         $class = $ctx->class;
415
-        try {
453
+        try
454
+        {
416 455
             $ctx->reflection = $reflection = new \ReflectionClass($class);
417
-        } catch (\ReflectionException $e) {
456
+        }
457
+        catch (\ReflectionException $e)
458
+        {
418 459
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
419 460
         }
420 461
 
421 462
         // Check scope name
422 463
         $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name;
423
-        if ($scope !== null && $scope !== $this->scope->getScopeName()) {
464
+        if ($scope !== null && $scope !== $this->scope->getScopeName())
465
+        {
424 466
             throw new BadScopeException($scope, $class);
425 467
         }
426 468
 
427 469
         // We have to construct class using external injector when we know the exact context
428
-        if ($arguments === [] && $this->binder->hasInjector($class)) {
470
+        if ($arguments === [] && $this->binder->hasInjector($class))
471
+        {
429 472
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments);
430 473
         }
431 474
 
432
-        if (!$reflection->isInstantiable()) {
475
+        if (!$reflection->isInstantiable())
476
+        {
433 477
             $itIs = match (true) {
434 478
                 $reflection->isEnum() => 'Enum',
435 479
                 $reflection->isAbstract() => 'Abstract class',
@@ -442,12 +486,16 @@  discard block
 block discarded – undo
442 486
 
443 487
         $constructor = $reflection->getConstructor();
444 488
 
445
-        if ($constructor !== null) {
446
-            try {
489
+        if ($constructor !== null)
490
+        {
491
+            try
492
+            {
447 493
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
448 494
                 $this->tracer->push(true);
449 495
                 $args = $this->resolver->resolveArguments($constructor, $arguments);
450
-            } catch (ValidationException $e) {
496
+            }
497
+            catch (ValidationException $e)
498
+            {
451 499
                 throw new ContainerException(
452 500
                     $this->tracer->combineTraceMessage(
453 501
                         \sprintf(
@@ -457,22 +505,31 @@  discard block
 block discarded – undo
457 505
                         )
458 506
                     ),
459 507
                 );
460
-            } finally {
508
+            }
509
+            finally
510
+            {
461 511
                 $this->tracer->pop(true);
462 512
                 $this->tracer->pop(false);
463 513
             }
464
-            try {
514
+            try
515
+            {
465 516
                 // Using constructor with resolved arguments
466 517
                 $this->tracer->push(false, call: "$class::__construct", arguments: $args);
467 518
                 $this->tracer->push(true);
468 519
                 $instance = new $class(...$args);
469
-            } catch (\TypeError $e) {
520
+            }
521
+            catch (\TypeError $e)
522
+            {
470 523
                 throw new WrongTypeException($constructor, $e);
471
-            } finally {
524
+            }
525
+            finally
526
+            {
472 527
                 $this->tracer->pop(true);
473 528
                 $this->tracer->pop(false);
474 529
             }
475
-        } else {
530
+        }
531
+        else
532
+        {
476 533
             // No constructor specified
477 534
             $instance = $reflection->newInstance();
478 535
         }
@@ -490,13 +547,15 @@  discard block
 block discarded – undo
490 547
         $instance = $this->runInflector($instance);
491 548
 
492 549
         //Declarative singletons
493
-        if ($this->isSingleton($ctx)) {
550
+        if ($this->isSingleton($ctx))
551
+        {
494 552
             $this->state->singletons[$ctx->alias] = $instance;
495 553
         }
496 554
 
497 555
         // Register finalizer
498 556
         $finalizer = $this->getFinalizer($ctx, $instance);
499
-        if ($finalizer !== null) {
557
+        if ($finalizer !== null)
558
+        {
500 559
             $this->state->finalizers[] = $finalizer;
501 560
         }
502 561
 
@@ -508,12 +567,14 @@  discard block
 block discarded – undo
508 567
      */
509 568
     private function isSingleton(Ctx $ctx): bool
510 569
     {
511
-        if ($ctx->singleton === true) {
570
+        if ($ctx->singleton === true)
571
+        {
512 572
             return true;
513 573
         }
514 574
 
515 575
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
516
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
576
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
577
+        {
517 578
             return true;
518 579
         }
519 580
 
@@ -527,7 +588,8 @@  discard block
 block discarded – undo
527 588
          * @var Attribute\Finalize|null $attribute
528 589
          */
529 590
         $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance();
530
-        if ($attribute === null) {
591
+        if ($attribute === null)
592
+        {
531 593
             return null;
532 594
         }
533 595
 
@@ -541,10 +603,14 @@  discard block
 block discarded – undo
541 603
     {
542 604
         $scope = $this->scope;
543 605
 
544
-        while ($scope !== null) {
545
-            foreach ($this->state->inflectors as $class => $inflectors) {
546
-                if ($instance instanceof $class) {
547
-                    foreach ($inflectors as $inflector) {
606
+        while ($scope !== null)
607
+        {
608
+            foreach ($this->state->inflectors as $class => $inflectors)
609
+            {
610
+                if ($instance instanceof $class)
611
+                {
612
+                    foreach ($inflectors as $inflector)
613
+                    {
548 614
                         $instance = $inflector->getParametersCount() > 1
549 615
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
550 616
                             : ($inflector->inflector)($instance);
@@ -560,9 +626,12 @@  discard block
 block discarded – undo
560 626
 
561 627
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
562 628
     {
563
-        try {
629
+        try
630
+        {
564 631
             $this->resolver->validateArguments($reflection, $arguments);
565
-        } catch (\Throwable) {
632
+        }
633
+        catch (\Throwable)
634
+        {
566 635
             return false;
567 636
         }
568 637
 
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.