Passed
Push — master ( 634fd7...347780 )
by Aleksei
02:32 queued 15s
created
src/Core/src/Internal/Factory.php 1 patch
Braces   +89 added lines, -38 removed lines patch added patch discarded remove patch
@@ -54,19 +54,24 @@  discard block
 block discarded – undo
54 54
      */
55 55
     public function make(string $alias, array $parameters = [], string $context = null): mixed
56 56
     {
57
-        if (!isset($this->state->bindings[$alias])) {
57
+        if (!isset($this->state->bindings[$alias]))
58
+        {
58 59
             $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
59
-            try {
60
+            try
61
+            {
60 62
                 //No direct instructions how to construct class, make is automatically
61 63
                 return $this->autowire($alias, $parameters, $context);
62
-            } finally {
64
+            }
65
+            finally
66
+            {
63 67
                 $this->tracer->pop(false);
64 68
             }
65 69
         }
66 70
 
67 71
         $avoidCache = $parameters !== [];
68 72
         $binding = $this->state->bindings[$alias];
69
-        try {
73
+        try
74
+        {
70 75
             $this->tracer->push(
71 76
                 false,
72 77
                 action: 'resolve from binding',
@@ -76,23 +81,32 @@  discard block
 block discarded – undo
76 81
             );
77 82
             $this->tracer->push(true);
78 83
 
79
-            if (\is_object($binding)) {
80
-                if ($binding::class === WeakReference::class) {
81
-                    if (($avoidCache || $binding->get() === null) && \class_exists($alias)) {
82
-                        try {
84
+            if (\is_object($binding))
85
+            {
86
+                if ($binding::class === WeakReference::class)
87
+                {
88
+                    if (($avoidCache || $binding->get() === null) && \class_exists($alias))
89
+                    {
90
+                        try
91
+                        {
83 92
                             $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
84 93
                             $object = $this->createInstance($alias, $parameters, $context);
85
-                            if ($avoidCache) {
94
+                            if ($avoidCache)
95
+                            {
86 96
                                 return $object;
87 97
                             }
88 98
                             $binding = $this->state->bindings[$alias] = WeakReference::create($object);
89
-                        } catch (\Throwable) {
99
+                        }
100
+                        catch (\Throwable)
101
+                        {
90 102
                             throw new ContainerException($this->tracer->combineTraceMessage(\sprintf(
91 103
                                 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.',
92 104
                                 $this->tracer->getRootAlias(),
93 105
                                 $alias,
94 106
                             )));
95
-                        } finally {
107
+                        }
108
+                        finally
109
+                        {
96 110
                             $this->tracer->pop();
97 111
                         }
98 112
                     }
@@ -104,26 +118,33 @@  discard block
 block discarded – undo
104 118
                     : $binding;
105 119
             }
106 120
 
107
-            if (\is_string($binding)) {
121
+            if (\is_string($binding))
122
+            {
108 123
                 //Binding is pointing to something else
109 124
                 return $this->make($binding, $parameters, $context);
110 125
             }
111 126
 
112 127
             unset($this->state->bindings[$alias]);
113
-            try {
128
+            try
129
+            {
114 130
                 $instance = $binding[0] === $alias
115 131
                     ? $this->autowire($alias, $parameters, $context)
116 132
                     : $this->evaluateBinding($alias, $binding[0], $parameters, $context);
117
-            } finally {
133
+            }
134
+            finally
135
+            {
118 136
                 /** @psalm-var class-string $alias */
119 137
                 $this->state->bindings[$alias] = $binding;
120 138
             }
121
-        } finally {
139
+        }
140
+        finally
141
+        {
122 142
             $this->tracer->pop(true);
123 143
             $this->tracer->pop(false);
124 144
         }
125 145
 
126
-        if ($binding[1]) {
146
+        if ($binding[1])
147
+        {
127 148
             // Indicates singleton
128 149
             /** @psalm-var class-string $alias */
129 150
             $this->state->bindings[$alias] = $instance;
@@ -177,18 +198,23 @@  discard block
 block discarded – undo
177 198
         array $parameters,
178 199
         string $context = null
179 200
     ): mixed {
180
-        if (\is_string($target)) {
201
+        if (\is_string($target))
202
+        {
181 203
             // Reference
182 204
             return $this->make($target, $parameters, $context);
183 205
         }
184 206
 
185
-        if ($target instanceof Autowire) {
207
+        if ($target instanceof Autowire)
208
+        {
186 209
             return $target->resolve($this, $parameters);
187 210
         }
188 211
 
189
-        try {
212
+        try
213
+        {
190 214
             return $this->invoker->invoke($target, $parameters);
191
-        } catch (NotCallableException $e) {
215
+        }
216
+        catch (NotCallableException $e)
217
+        {
192 218
             throw new ContainerException(
193 219
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)),
194 220
                 $e->getCode(),
@@ -212,20 +238,26 @@  discard block
 block discarded – undo
212 238
      */
213 239
     private function createInstance(string $class, array $parameters, string $context = null): object
214 240
     {
215
-        try {
241
+        try
242
+        {
216 243
             $reflection = new \ReflectionClass($class);
217
-        } catch (\ReflectionException $e) {
244
+        }
245
+        catch (\ReflectionException $e)
246
+        {
218 247
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
219 248
         }
220 249
 
221 250
         //We have to construct class using external injector when we know exact context
222
-        if ($parameters === [] && $this->binder->hasInjector($class)) {
251
+        if ($parameters === [] && $this->binder->hasInjector($class))
252
+        {
223 253
             $injector = $this->state->injectors[$reflection->getName()];
224 254
 
225
-            try {
255
+            try
256
+            {
226 257
                 $injectorInstance = $this->container->get($injector);
227 258
 
228
-                if (!$injectorInstance instanceof InjectorInterface) {
259
+                if (!$injectorInstance instanceof InjectorInterface)
260
+                {
229 261
                     throw new InjectionException(
230 262
                         \sprintf(
231 263
                             "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -237,7 +269,8 @@  discard block
 block discarded – undo
237 269
 
238 270
                 /** @var InjectorInterface<TObject> $injectorInstance */
239 271
                 $instance = $injectorInstance->createInjection($reflection, $context);
240
-                if (!$reflection->isInstance($instance)) {
272
+                if (!$reflection->isInstance($instance))
273
+                {
241 274
                     throw new InjectionException(
242 275
                         \sprintf(
243 276
                             "Invalid injection response for '%s'.",
@@ -247,12 +280,15 @@  discard block
 block discarded – undo
247 280
                 }
248 281
 
249 282
                 return $instance;
250
-            } finally {
283
+            }
284
+            finally
285
+            {
251 286
                 $this->state->injectors[$reflection->getName()] = $injector;
252 287
             }
253 288
         }
254 289
 
255
-        if (!$reflection->isInstantiable()) {
290
+        if (!$reflection->isInstantiable())
291
+        {
256 292
             $itIs = match (true) {
257 293
                 $reflection->isEnum() => 'Enum',
258 294
                 $reflection->isAbstract() => 'Abstract class',
@@ -265,12 +301,16 @@  discard block
 block discarded – undo
265 301
 
266 302
         $constructor = $reflection->getConstructor();
267 303
 
268
-        if ($constructor !== null) {
269
-            try {
304
+        if ($constructor !== null)
305
+        {
306
+            try
307
+            {
270 308
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
271 309
                 $this->tracer->push(true);
272 310
                 $arguments = $this->resolver->resolveArguments($constructor, $parameters);
273
-            } catch (ValidationException $e) {
311
+            }
312
+            catch (ValidationException $e)
313
+            {
274 314
                 throw new ContainerException(
275 315
                     $this->tracer->combineTraceMessage(
276 316
                         \sprintf(
@@ -280,22 +320,31 @@  discard block
 block discarded – undo
280 320
                         )
281 321
                     ),
282 322
                 );
283
-            } finally {
323
+            }
324
+            finally
325
+            {
284 326
                 $this->tracer->pop(true);
285 327
                 $this->tracer->pop(false);
286 328
             }
287
-            try {
329
+            try
330
+            {
288 331
                 // Using constructor with resolved arguments
289 332
                 $this->tracer->push(false, call: "$class::__construct", arguments: $arguments);
290 333
                 $this->tracer->push(true);
291 334
                 $instance = new $class(...$arguments);
292
-            } catch (\TypeError $e) {
335
+            }
336
+            catch (\TypeError $e)
337
+            {
293 338
                 throw new WrongTypeException($constructor, $e);
294
-            } finally {
339
+            }
340
+            finally
341
+            {
295 342
                 $this->tracer->pop(true);
296 343
                 $this->tracer->pop(false);
297 344
             }
298
-        } else {
345
+        }
346
+        else
347
+        {
299 348
             // No constructor specified
300 349
             $instance = $reflection->newInstance();
301 350
         }
@@ -316,9 +365,11 @@  discard block
 block discarded – undo
316 365
     private function registerInstance(object $instance): object
317 366
     {
318 367
         //Declarative singletons
319
-        if ($instance instanceof SingletonInterface) {
368
+        if ($instance instanceof SingletonInterface)
369
+        {
320 370
             $alias = $instance::class;
321
-            if (!isset($this->state->bindings[$alias])) {
371
+            if (!isset($this->state->bindings[$alias]))
372
+            {
322 373
                 $this->state->bindings[$alias] = $instance;
323 374
             }
324 375
         }
Please login to merge, or discard this patch.