Passed
Pull Request — master (#1045)
by Aleksei
10:03
created
src/Core/src/Internal/Container.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -49,7 +49,8 @@
 block discarded – undo
49 49
      */
50 50
     public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed
51 51
     {
52
-        if ($id instanceof Autowire) {
52
+        if ($id instanceof Autowire)
53
+        {
53 54
             return $id->resolve($this->factory);
54 55
         }
55 56
 
Please login to merge, or discard this patch.
src/Core/tests/Internal/Proxy/ProxyClassRendererTest.php 1 patch
Braces   +10 added lines, -4 removed lines patch added patch discarded remove patch
@@ -72,22 +72,28 @@
 block discarded – undo
72 72
 
73 73
     public static function provideRenderMethod(): iterable
74 74
     {
75
-        $class = new class {
75
+        $class = new class
76
+        {
76 77
             public const INT_CONST = 42;
77 78
 
78 79
             #[ExpectedAttribute('public function test1(...$variadic)')]
79
-            public function test1(...$variadic) {}
80
+            public function test1(...$variadic)
81
+            {
82
+}
80 83
             #[ExpectedAttribute('public function test2(string|int $string = self::INT_CONST): string|int')]
81 84
             public function test2(string|int $string = self::INT_CONST): string|int {}
82 85
             #[ExpectedAttribute('public function test3(object $obj = new \stdClass(new \stdClass(), new \stdClass()))')]
83
-            public function test3(object $obj = new stdClass(new stdClass(), new stdClass())) {}
86
+            public function test3(object $obj = new stdClass(new stdClass(), new stdClass()))
87
+            {
88
+}
84 89
             #[ExpectedAttribute('public function test4(): \\' . ProxyClassRendererTest::class)]
85 90
             public function test4(): ProxyClassRendererTest {}
86 91
             #[ExpectedAttribute('public function &test5(): string')]
87 92
             public function &test5(): string {}
88 93
         };
89 94
 
90
-        foreach ((new \ReflectionClass($class))->getMethods() as $method) {
95
+        foreach ((new \ReflectionClass($class))->getMethods() as $method)
96
+        {
91 97
             $expected = $method->getAttributes(ExpectedAttribute::class)[0]->newInstance();
92 98
 
93 99
             yield [$method, $expected->value];
Please login to merge, or discard this patch.
src/Core/tests/Internal/Proxy/Stub/MockInterfaceImpl.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -48,7 +48,8 @@
 block discarded – undo
48 48
 
49 49
     public function concatMultiple(string $prefix, string &...$byLink): array
50 50
     {
51
-        foreach ($byLink as $k => $link) {
51
+        foreach ($byLink as $k => $link)
52
+        {
52 53
             $byLink[$k] = $prefix . $link;
53 54
             unset($link);
54 55
         }
Please login to merge, or discard this patch.
src/Core/tests/Scope/FibersTest.php 1 patch
Braces   +21 added lines, -10 removed lines patch added patch discarded remove patch
@@ -30,7 +30,8 @@  discard block
 block discarded – undo
30 30
 
31 31
         FiberHelper::runInFiber(
32 32
             self::functionScopedTestDataIterator(),
33
-            static function (mixed $suspendValue) {
33
+            static function (mixed $suspendValue)
34
+            {
34 35
                 self::assertNull(ContainerScope::getContainer());
35 36
                 self::assertTrue(\in_array($suspendValue, self::TEST_DATA, true));
36 37
             },
@@ -63,7 +64,8 @@  discard block
 block discarded – undo
63 64
         );
64 65
 
65 66
         self::assertCount(5, $result);
66
-        foreach ($result as $suspendValue) {
67
+        foreach ($result as $suspendValue)
68
+        {
67 69
             self::assertSame(self::TEST_DATA, $suspendValue);
68 70
         }
69 71
     }
@@ -81,7 +83,8 @@  discard block
 block discarded – undo
81 83
         );
82 84
 
83 85
         self::assertCount(2, $result);
84
-        foreach ($result as $suspendValue) {
86
+        foreach ($result as $suspendValue)
87
+        {
85 88
             self::assertSame(self::TEST_DATA, $suspendValue);
86 89
         }
87 90
     }
@@ -92,7 +95,8 @@  discard block
 block discarded – undo
92 95
         $this->expectExceptionMessage('test');
93 96
 
94 97
         FiberHelper::runInFiber(
95
-            static function () {
98
+            static function ()
99
+            {
96 100
                 return (new Container())->runScoped(
97 101
                     function (): string {
98 102
                         $result = '';
@@ -114,15 +118,19 @@  discard block
 block discarded – undo
114 118
     public function testCatchThrownException(): void
115 119
     {
116 120
         $result = FiberHelper::runInFiber(
117
-            static function () {
121
+            static function ()
122
+            {
118 123
                 return (new Container())->runScoped(
119 124
                     function (): string {
120 125
                         $result = '';
121 126
                         $result .= Fiber::suspend('foo');
122 127
                         $result .= Fiber::suspend('bar');
123
-                        try {
128
+                        try
129
+                        {
124 130
                             $result .= Fiber::suspend('error');
125
-                        } catch (\Throwable $e) {
131
+                        }
132
+                        catch (\Throwable $e)
133
+                        {
126 134
                             $result .= $e->getMessage();
127 135
                         }
128 136
                         $result .= Fiber::suspend('baz');
@@ -160,20 +168,23 @@  discard block
 block discarded – undo
160 168
             $c1 = $container ?? new Container();
161 169
             $c1->bindSingleton('resource', new stdClass());
162 170
 
163
-            $result = $c1->runScoped(static function (Container $c2) use ($load) {
171
+            $result = $c1->runScoped(static function (Container $c2) use ($load)
172
+            {
164 173
                 // check local binding
165 174
                 self::assertTrue($c2->has('foo'));
166 175
                 self::assertInstanceOf(DateTime::class, $c2->get('foo'));
167 176
 
168 177
                 return $c2->runScoped(
169
-                    static function (ContainerInterface $c3) use ($load) {
178
+                    static function (ContainerInterface $c3) use ($load)
179
+                    {
170 180
                         // check local binding
171 181
                         self::assertTrue($c3->has('bar'));
172 182
 
173 183
                         $resource = $c3->get('resource');
174 184
                         self::assertInstanceOf(DateTimeImmutable::class, $c3->get('bar'));
175 185
                         self::assertInstanceOf(stdClass::class, $resource);
176
-                        foreach (self::TEST_DATA as $key => $value) {
186
+                        foreach (self::TEST_DATA as $key => $value)
187
+                        {
177 188
                             $resource->$key = $value;
178 189
                             $load === null or $load();
179 190
                             Fiber::suspend($value);
Please login to merge, or discard this patch.
src/Core/tests/Scope/FiberHelper.php 1 patch
Braces   +26 added lines, -12 removed lines patch added patch discarded remove patch
@@ -22,11 +22,16 @@  discard block
 block discarded – undo
22 22
     {
23 23
         $fiber = new Fiber($callable);
24 24
         $value = $fiber->start();
25
-        while (!$fiber->isTerminated()) {
26
-            if ($check !== null) {
27
-                try {
25
+        while (!$fiber->isTerminated())
26
+        {
27
+            if ($check !== null)
28
+            {
29
+                try
30
+                {
28 31
                     $value = $check($value);
29
-                } catch (\Throwable $e) {
32
+                }
33
+                catch (\Throwable $e)
34
+                {
30 35
                     $value = $fiber->throw($e);
31 36
                     continue;
32 37
                 }
@@ -50,14 +55,17 @@  discard block
 block discarded – undo
50 55
         /** Values that were suspended by the fiber. */
51 56
         $suspends = [];
52 57
         $results = [];
53
-        foreach ($callables as $key => $callable) {
54
-            $fiberGenerators[$key] = (static function () use ($callable) {
58
+        foreach ($callables as $key => $callable)
59
+        {
60
+            $fiberGenerators[$key] = (static function () use ($callable)
61
+            {
55 62
                 $fiber = new Fiber($callable);
56 63
                 // Get ready
57 64
                 yield null;
58 65
 
59 66
                 $value = yield $fiber->start();
60
-                while (!$fiber->isTerminated()) {
67
+                while (!$fiber->isTerminated())
68
+                {
61 69
                     $value = yield $fiber->resume($value);
62 70
                 }
63 71
                 return $fiber->getReturn();
@@ -67,15 +75,21 @@  discard block
 block discarded – undo
67 75
         }
68 76
 
69 77
 
70
-        while ($fiberGenerators !== []) {
71
-            foreach ($fiberGenerators as $key => $generator) {
72
-                try {
78
+        while ($fiberGenerators !== [])
79
+        {
80
+            foreach ($fiberGenerators as $key => $generator)
81
+            {
82
+                try
83
+                {
73 84
                     $suspends[$key] = $generator->send($suspends[$key]);
74
-                    if (!$generator->valid()) {
85
+                    if (!$generator->valid())
86
+                    {
75 87
                         $results[$key] = $generator->getReturn();
76 88
                         unset($fiberGenerators[$key]);
77 89
                     }
78
-                } catch (\Throwable $e) {
90
+                }
91
+                catch (\Throwable $e)
92
+                {
79 93
                     unset($fiberGenerators[$key]);
80 94
                     $results[$key] = $e;
81 95
                 }
Please login to merge, or discard this patch.
src/Core/tests/Scope/UseCaseTest.php 1 patch
Braces   +18 added lines, -9 removed lines patch added patch discarded remove patch
@@ -28,7 +28,8 @@  discard block
 block discarded – undo
28 28
         $root = new Container();
29 29
         $root->bind('foo', SampleClass::class);
30 30
 
31
-        $root->runScoped(function (ContainerInterface $c1) {
31
+        $root->runScoped(function (ContainerInterface $c1)
32
+        {
32 33
             $c1->get('foo');
33 34
         }, bindings: ['foo' => SampleClass::class]);
34 35
 
@@ -61,7 +62,8 @@  discard block
 block discarded – undo
61 62
     {
62 63
         $root = new Container();
63 64
 
64
-        $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) {
65
+        $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias)
66
+        {
65 67
             $obj1 = $c1->get($alias);
66 68
             $obj2 = $c1->get($alias);
67 69
 
@@ -86,14 +88,16 @@  discard block
 block discarded – undo
86 88
     {
87 89
         $root = new Container();
88 90
 
89
-        $root->runScoped(function (ContainerInterface $c1) use ($root) {
91
+        $root->runScoped(function (ContainerInterface $c1) use ($root)
92
+        {
90 93
             $obj1 = $c1->get('foo');
91 94
             $this->weakMap->offsetSet($obj1, true);
92 95
 
93 96
             self::assertNotSame($root, $c1);
94 97
             self::assertInstanceOf(stdClass::class, $obj1);
95 98
 
96
-            $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) {
99
+            $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1)
100
+            {
97 101
                 $obj2 = $c2->get('foo');
98 102
                 $this->weakMap->offsetSet($obj2, true);
99 103
 
@@ -122,14 +126,16 @@  discard block
 block discarded – undo
122 126
         $root->bindSingleton('bar', [Factory::class, 'makeStdClass']);
123 127
         $root->bind(stdClass::class, new stdClass());
124 128
 
125
-        $root->runScoped(function (ContainerInterface $c1) use ($root) {
129
+        $root->runScoped(function (ContainerInterface $c1) use ($root)
130
+        {
126 131
             $obj1 = $c1->get('foo');
127 132
             $this->weakMap->offsetSet($obj1, true);
128 133
 
129 134
             self::assertInstanceOf(stdClass::class, $obj1);
130 135
             // Singleton must be the same
131 136
             self::assertSame($c1->get('bar'), $root->get('bar'));
132
-            $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) {
137
+            $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1)
138
+            {
133 139
                 $obj2 = $c2->get('foo');
134 140
 
135 141
                 self::assertInstanceOf(stdClass::class, $obj2);
@@ -262,7 +268,8 @@  discard block
 block discarded – undo
262 268
     {
263 269
         $root = new Container();
264 270
 
265
-        $root->invoke(static function () use ($root) {
271
+        $root->invoke(static function () use ($root)
272
+        {
266 273
             self::assertNotNull(\Spiral\Core\ContainerScope::getContainer());
267 274
             self::assertSame($root, \Spiral\Core\ContainerScope::getContainer());
268 275
         });
@@ -271,7 +278,8 @@  discard block
 block discarded – undo
271 278
     public function testRegisterContainerOnGet(): void
272 279
     {
273 280
         $root = new Container();
274
-        $root->bind('foo', function () use ($root) {
281
+        $root->bind('foo', function () use ($root)
282
+        {
275 283
             self::assertNotNull(\Spiral\Core\ContainerScope::getContainer());
276 284
             self::assertSame($root, \Spiral\Core\ContainerScope::getContainer());
277 285
         });
@@ -282,7 +290,8 @@  discard block
 block discarded – undo
282 290
     public function testRegisterContainerOnMake(): void
283 291
     {
284 292
         $root = new Container();
285
-        $root->bind('foo', function () use ($root) {
293
+        $root->bind('foo', function () use ($root)
294
+        {
286 295
             self::assertNotNull(\Spiral\Core\ContainerScope::getContainer());
287 296
             self::assertSame($root, \Spiral\Core\ContainerScope::getContainer());
288 297
         });
Please login to merge, or discard this patch.
src/Core/src/Container.php 1 patch
Braces   +46 added lines, -21 removed lines patch added patch discarded remove patch
@@ -170,23 +170,29 @@  discard block
 block discarded – undo
170 170
      */
171 171
     public function runScope(Scope|array $bindings, callable $scope): mixed
172 172
     {
173
-        if (!\is_array($bindings)) {
173
+        if (!\is_array($bindings))
174
+        {
174 175
             return $this->runIsolatedScope($bindings, $scope);
175 176
         }
176 177
 
177 178
         $binds = &$this->state->bindings;
178 179
         $singletons = &$this->state->singletons;
179 180
         $cleanup = $previous = $prevSin = [];
180
-        foreach ($bindings as $alias => $resolver) {
181
+        foreach ($bindings as $alias => $resolver)
182
+        {
181 183
             // Store previous bindings
182
-            if (isset($binds[$alias])) {
184
+            if (isset($binds[$alias]))
185
+            {
183 186
                 $previous[$alias] = $binds[$alias];
184
-            } else {
187
+            }
188
+            else
189
+            {
185 190
                 // Store bindings to be removed
186 191
                 $cleanup[] = $alias;
187 192
             }
188 193
             // Store previous singletons
189
-            if (isset($singletons[$alias])) {
194
+            if (isset($singletons[$alias]))
195
+            {
190 196
                 $prevSin[$alias] = $singletons[$alias];
191 197
                 unset($singletons[$alias]);
192 198
             }
@@ -194,21 +200,27 @@  discard block
 block discarded – undo
194 200
             $this->binder->bind($alias, $resolver);
195 201
         }
196 202
 
197
-        try {
203
+        try
204
+        {
198 205
             return ContainerScope::getContainer() !== $this
199 206
                 ? ContainerScope::runScope($this, $scope)
200 207
                 : $scope($this);
201
-        } finally {
208
+        }
209
+        finally
210
+        {
202 211
             // Remove new bindings
203
-            foreach ($cleanup as $alias) {
212
+            foreach ($cleanup as $alias)
213
+            {
204 214
                 unset($binds[$alias], $singletons[$alias]);
205 215
             }
206 216
             // Restore previous bindings
207
-            foreach ($previous as $alias => $resolver) {
217
+            foreach ($previous as $alias => $resolver)
218
+            {
208 219
                 $binds[$alias] = $resolver;
209 220
             }
210 221
             // Restore singletons
211
-            foreach ($prevSin as $alias => $instance) {
222
+            foreach ($prevSin as $alias => $instance)
223
+            {
212 224
                 $singletons[$alias] = $instance;
213 225
             }
214 226
         }
@@ -265,7 +277,8 @@  discard block
 block discarded – undo
265 277
      */
266 278
     public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void
267 279
     {
268
-        if ($force) {
280
+        if ($force)
281
+        {
269 282
             $this->binder->removeBinding($alias);
270 283
         }
271 284
 
@@ -336,8 +349,10 @@  discard block
 block discarded – undo
336 349
         ]);
337 350
 
338 351
         // Create container services
339
-        foreach ($container->config as $property => $class) {
340
-            if (\property_exists($container, $property)) {
352
+        foreach ($container->config as $property => $class)
353
+        {
354
+            if (\property_exists($container, $property))
355
+            {
341 356
                 $container->$property = $constructor->get($property, $class);
342 357
             }
343 358
         }
@@ -351,7 +366,8 @@  discard block
 block discarded – undo
351 366
     private function closeScope(): void
352 367
     {
353 368
         /** @psalm-suppress RedundantPropertyInitializationCheck */
354
-        if (!isset($this->scope)) {
369
+        if (!isset($this->scope))
370
+        {
355 371
             $this->destruct();
356 372
             return;
357 373
         }
@@ -360,10 +376,14 @@  discard block
 block discarded – undo
360 376
 
361 377
         // Run finalizers
362 378
         $errors = [];
363
-        foreach ($this->state->finalizers as $finalizer) {
364
-            try {
379
+        foreach ($this->state->finalizers as $finalizer)
380
+        {
381
+            try
382
+            {
365 383
                 $this->invoker->invoke($finalizer);
366
-            } catch (\Throwable $e) {
384
+            }
385
+            catch (\Throwable $e)
386
+            {
367 387
                 $errors[] = $e;
368 388
             }
369 389
         }
@@ -372,7 +392,8 @@  discard block
 block discarded – undo
372 392
         $this->destruct();
373 393
 
374 394
         // Throw collected errors
375
-        if ($errors !== []) {
395
+        if ($errors !== [])
396
+        {
376 397
             throw new FinalizersException($scopeName, $errors);
377 398
         }
378 399
     }
@@ -394,18 +415,22 @@  discard block
 block discarded – undo
394 415
         $container->scope->setParent($this, $this->scope);
395 416
 
396 417
         // Add specific bindings
397
-        foreach ($config->bindings as $alias => $resolver) {
418
+        foreach ($config->bindings as $alias => $resolver)
419
+        {
398 420
             $container->binder->bind($alias, $resolver);
399 421
         }
400 422
 
401 423
         return ContainerScope::runScope(
402 424
             $container,
403 425
             static function (self $container) use ($config, $closure): mixed {
404
-                try {
426
+                try
427
+                {
405 428
                     return $config->autowire
406 429
                         ? $container->invoke($closure)
407 430
                         : $closure($container);
408
-                } finally {
431
+                }
432
+                finally
433
+                {
409 434
                     $container->closeScope();
410 435
                 }
411 436
             }
Please login to merge, or discard this patch.
src/Core/src/Internal/Proxy/Resolver.php 1 patch
Braces   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -16,11 +16,14 @@
 block discarded – undo
16 16
     {
17 17
         $c = ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.');
18 18
 
19
-        try {
19
+        try
20
+        {
20 21
             $result = $c->get($alias, $context) ?? throw new ContainerException(
21 22
                 'Resolved `null` from the container.',
22 23
             );
23
-        } catch (ContainerException $e) {
24
+        }
25
+        catch (ContainerException $e)
26
+        {
24 27
             throw new ContainerException(
25 28
                 // todo : find required scope
26 29
                 \sprintf('Unable to resolve `%s` in a Proxy.', $alias),
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Braces   +119 added lines, -51 removed lines patch added patch discarded remove patch
@@ -68,17 +68,20 @@  discard block
 block discarded – undo
68 68
      */
69 69
     public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed
70 70
     {
71
-        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) {
71
+        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons))
72
+        {
72 73
             return $this->state->singletons[$alias];
73 74
         }
74 75
 
75 76
         $binding = $this->state->bindings[$alias] ?? null;
76 77
 
77
-        if ($binding === null) {
78
+        if ($binding === null)
79
+        {
78 80
             return $this->resolveWithoutBinding($alias, $parameters, $context);
79 81
         }
80 82
 
81
-        try {
83
+        try
84
+        {
82 85
             $this->tracer->push(
83 86
                 false,
84 87
                 action: 'resolve from binding',
@@ -106,7 +109,9 @@  discard block
 block discarded – undo
106 109
                     ->resolveWeakReference($binding, $alias, $context, $parameters),
107 110
                 default => $binding,
108 111
             };
109
-        } finally {
112
+        }
113
+        finally
114
+        {
110 115
             $this->state->bindings[$alias] ??= $binding;
111 116
             $this->tracer->pop(true);
112 117
             $this->tracer->pop(false);
@@ -120,18 +125,23 @@  discard block
 block discarded – undo
120 125
     private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments)
121 126
     {
122 127
         $context = $ctx->context;
123
-        try {
128
+        try
129
+        {
124 130
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
125
-        } catch (\ReflectionException $e) {
131
+        }
132
+        catch (\ReflectionException $e)
133
+        {
126 134
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
127 135
         }
128 136
 
129 137
         $injector = $binding->injector;
130 138
 
131
-        try {
139
+        try
140
+        {
132 141
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
133 142
 
134
-            if (!$injectorInstance instanceof InjectorInterface) {
143
+            if (!$injectorInstance instanceof InjectorInterface)
144
+            {
135 145
                 throw new InjectionException(
136 146
                     \sprintf(
137 147
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -158,7 +168,8 @@  discard block
 block discarded – undo
158 168
                 default => (string)$context,
159 169
             });
160 170
 
161
-            if (!$reflection->isInstance($instance)) {
171
+            if (!$reflection->isInstance($instance))
172
+            {
162 173
                 throw new InjectionException(
163 174
                     \sprintf(
164 175
                         "Invalid injection response for '%s'.",
@@ -168,7 +179,9 @@  discard block
 block discarded – undo
168 179
             }
169 180
 
170 181
             return $instance;
171
-        } finally {
182
+        }
183
+        finally
184
+        {
172 185
             $this->state->bindings[$ctx->class] ??= $binding;
173 186
         }
174 187
     }
@@ -187,7 +200,8 @@  discard block
 block discarded – undo
187 200
             //Binding is pointing to something else
188 201
             : $this->make($binding->alias, $arguments, $context);
189 202
 
190
-        if ($binding->singleton && $arguments === []) {
203
+        if ($binding->singleton && $arguments === [])
204
+        {
191 205
             $this->state->singletons[$alias] = $result;
192 206
         }
193 207
 
@@ -228,11 +242,14 @@  discard block
 block discarded – undo
228 242
         array $arguments,
229 243
     ): mixed {
230 244
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
231
-        try {
245
+        try
246
+        {
232 247
             $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0
233 248
                 ? ($binding->factory)()
234 249
                 : $this->invoker->invoke($binding->factory, $arguments);
235
-        } catch (NotCallableException $e) {
250
+        }
251
+        catch (NotCallableException $e)
252
+        {
236 253
             throw new ContainerException(
237 254
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
238 255
                 $e->getCode(),
@@ -251,19 +268,24 @@  discard block
 block discarded – undo
251 268
     ): ?object {
252 269
         $avoidCache = $arguments !== [];
253 270
 
254
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
255
-            try {
271
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
272
+        {
273
+            try
274
+            {
256 275
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
257 276
 
258 277
                 $object = $this->createInstance(
259 278
                     new Ctx(alias: $alias, class: $alias, context: $context),
260 279
                     $arguments,
261 280
                 );
262
-                if ($avoidCache) {
281
+                if ($avoidCache)
282
+                {
263 283
                     return $object;
264 284
                 }
265 285
                 $binding->reference = WeakReference::create($object);
266
-            } catch (\Throwable) {
286
+            }
287
+            catch (\Throwable)
288
+            {
267 289
                 throw new ContainerException(
268 290
                     $this->tracer->combineTraceMessage(
269 291
                         \sprintf(
@@ -273,7 +295,9 @@  discard block
 block discarded – undo
273 295
                         )
274 296
                     )
275 297
                 );
276
-            } finally {
298
+            }
299
+            finally
300
+            {
277 301
                 $this->tracer->pop();
278 302
             }
279 303
         }
@@ -288,18 +312,25 @@  discard block
 block discarded – undo
288 312
     ): mixed {
289 313
         $parent = $this->scope->getParent();
290 314
 
291
-        if ($parent !== null) {
292
-            try {
315
+        if ($parent !== null)
316
+        {
317
+            try
318
+            {
293 319
                 $this->tracer->push(false, ...[
294 320
                     'current scope' => $this->scope->getScopeName(),
295 321
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
296 322
                 ]);
297 323
                 return $parent->make($alias, $parameters, $context);
298
-            } catch (BadScopeException $e) {
299
-                if ($this->scope->getScopeName() !== $e->getScope()) {
324
+            }
325
+            catch (BadScopeException $e)
326
+            {
327
+                if ($this->scope->getScopeName() !== $e->getScope())
328
+                {
300 329
                     throw $e;
301 330
                 }
302
-            } catch (ContainerExceptionInterface $e) {
331
+            }
332
+            catch (ContainerExceptionInterface $e)
333
+            {
303 334
                 $className = match (true) {
304 335
                     $e instanceof NotFoundException => NotFoundException::class,
305 336
                     default => ContainerException::class,
@@ -308,19 +339,24 @@  discard block
 block discarded – undo
308 339
                     'Can\'t resolve `%s`.',
309 340
                     $alias,
310 341
                 )), previous: $e);
311
-            } finally {
342
+            }
343
+            finally
344
+            {
312 345
                 $this->tracer->pop(false);
313 346
             }
314 347
         }
315 348
 
316 349
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
317
-        try {
350
+        try
351
+        {
318 352
             //No direct instructions how to construct class, make is automatically
319 353
             return $this->autowire(
320 354
                 new Ctx(alias: $alias, class: $alias, context: $context),
321 355
                 $parameters,
322 356
             );
323
-        } finally {
357
+        }
358
+        finally
359
+        {
324 360
             $this->tracer->pop(false);
325 361
         }
326 362
     }
@@ -370,7 +406,8 @@  discard block
 block discarded – undo
370 406
         // Check scope name
371 407
         $ctx->reflection = new \ReflectionClass($instance);
372 408
         $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name;
373
-        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) {
409
+        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName())
410
+        {
374 411
             throw new BadScopeException($scopeName, $instance::class);
375 412
         }
376 413
 
@@ -397,24 +434,30 @@  discard block
 block discarded – undo
397 434
         array $arguments,
398 435
     ): object {
399 436
         $class = $ctx->class;
400
-        try {
437
+        try
438
+        {
401 439
             $ctx->reflection = $reflection = new \ReflectionClass($class);
402
-        } catch (\ReflectionException $e) {
440
+        }
441
+        catch (\ReflectionException $e)
442
+        {
403 443
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
404 444
         }
405 445
 
406 446
         // Check scope name
407 447
         $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name;
408
-        if ($scope !== null && $scope !== $this->scope->getScopeName()) {
448
+        if ($scope !== null && $scope !== $this->scope->getScopeName())
449
+        {
409 450
             throw new BadScopeException($scope, $class);
410 451
         }
411 452
 
412 453
         // We have to construct class using external injector when we know the exact context
413
-        if ($arguments === [] && $this->binder->hasInjector($class)) {
454
+        if ($arguments === [] && $this->binder->hasInjector($class))
455
+        {
414 456
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments);
415 457
         }
416 458
 
417
-        if (!$reflection->isInstantiable()) {
459
+        if (!$reflection->isInstantiable())
460
+        {
418 461
             $itIs = match (true) {
419 462
                 $reflection->isEnum() => 'Enum',
420 463
                 $reflection->isAbstract() => 'Abstract class',
@@ -427,12 +470,16 @@  discard block
 block discarded – undo
427 470
 
428 471
         $constructor = $reflection->getConstructor();
429 472
 
430
-        if ($constructor !== null) {
431
-            try {
473
+        if ($constructor !== null)
474
+        {
475
+            try
476
+            {
432 477
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
433 478
                 $this->tracer->push(true);
434 479
                 $args = $this->resolver->resolveArguments($constructor, $arguments);
435
-            } catch (ValidationException $e) {
480
+            }
481
+            catch (ValidationException $e)
482
+            {
436 483
                 throw new ContainerException(
437 484
                     $this->tracer->combineTraceMessage(
438 485
                         \sprintf(
@@ -442,22 +489,31 @@  discard block
 block discarded – undo
442 489
                         )
443 490
                     ),
444 491
                 );
445
-            } finally {
492
+            }
493
+            finally
494
+            {
446 495
                 $this->tracer->pop(true);
447 496
                 $this->tracer->pop(false);
448 497
             }
449
-            try {
498
+            try
499
+            {
450 500
                 // Using constructor with resolved arguments
451 501
                 $this->tracer->push(false, call: "$class::__construct", arguments: $args);
452 502
                 $this->tracer->push(true);
453 503
                 $instance = new $class(...$args);
454
-            } catch (\TypeError $e) {
504
+            }
505
+            catch (\TypeError $e)
506
+            {
455 507
                 throw new WrongTypeException($constructor, $e);
456
-            } finally {
508
+            }
509
+            finally
510
+            {
457 511
                 $this->tracer->pop(true);
458 512
                 $this->tracer->pop(false);
459 513
             }
460
-        } else {
514
+        }
515
+        else
516
+        {
461 517
             // No constructor specified
462 518
             $instance = $reflection->newInstance();
463 519
         }
@@ -475,13 +531,15 @@  discard block
 block discarded – undo
475 531
         $instance = $this->runInflector($instance);
476 532
 
477 533
         //Declarative singletons
478
-        if ($this->isSingleton($ctx)) {
534
+        if ($this->isSingleton($ctx))
535
+        {
479 536
             $this->state->singletons[$ctx->alias] = $instance;
480 537
         }
481 538
 
482 539
         // Register finalizer
483 540
         $finalizer = $this->getFinalizer($ctx, $instance);
484
-        if ($finalizer !== null) {
541
+        if ($finalizer !== null)
542
+        {
485 543
             $this->state->finalizers[] = $finalizer;
486 544
         }
487 545
 
@@ -493,12 +551,14 @@  discard block
 block discarded – undo
493 551
      */
494 552
     private function isSingleton(Ctx $ctx): bool
495 553
     {
496
-        if ($ctx->singleton === true) {
554
+        if ($ctx->singleton === true)
555
+        {
497 556
             return true;
498 557
         }
499 558
 
500 559
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
501
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
560
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
561
+        {
502 562
             return true;
503 563
         }
504 564
 
@@ -512,7 +572,8 @@  discard block
 block discarded – undo
512 572
          * @var Finalize|null $attribute
513 573
          */
514 574
         $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance();
515
-        if ($attribute === null) {
575
+        if ($attribute === null)
576
+        {
516 577
             return null;
517 578
         }
518 579
 
@@ -526,10 +587,14 @@  discard block
 block discarded – undo
526 587
     {
527 588
         $scope = $this->scope;
528 589
 
529
-        while ($scope !== null) {
530
-            foreach ($this->state->inflectors as $class => $inflectors) {
531
-                if ($instance instanceof $class) {
532
-                    foreach ($inflectors as $inflector) {
590
+        while ($scope !== null)
591
+        {
592
+            foreach ($this->state->inflectors as $class => $inflectors)
593
+            {
594
+                if ($instance instanceof $class)
595
+                {
596
+                    foreach ($inflectors as $inflector)
597
+                    {
533 598
                         $instance = $inflector->getParametersCount() > 1
534 599
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
535 600
                             : ($inflector->inflector)($instance);
@@ -545,9 +610,12 @@  discard block
 block discarded – undo
545 610
 
546 611
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
547 612
     {
548
-        try {
613
+        try
614
+        {
549 615
             $this->resolver->validateArguments($reflection, $arguments);
550
-        } catch (\Throwable) {
616
+        }
617
+        catch (\Throwable)
618
+        {
551 619
             return false;
552 620
         }
553 621
 
Please login to merge, or discard this patch.