Passed
Pull Request — master (#867)
by Aleksei
06:58
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,7 +118,8 @@  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
                 return $binding === $alias
109 124
                     ? $this->autowire($alias, $parameters, $context)
110 125
                     //Binding is pointing to something else
@@ -112,20 +127,26 @@  discard block
 block discarded – undo
112 127
             }
113 128
 
114 129
             unset($this->state->bindings[$alias]);
115
-            try {
130
+            try
131
+            {
116 132
                 $instance = $binding[0] === $alias
117 133
                     ? $this->autowire($alias, $parameters, $context)
118 134
                     : $this->evaluateBinding($alias, $binding[0], $parameters, $context);
119
-            } finally {
135
+            }
136
+            finally
137
+            {
120 138
                 /** @psalm-var class-string $alias */
121 139
                 $this->state->bindings[$alias] = $binding;
122 140
             }
123
-        } finally {
141
+        }
142
+        finally
143
+        {
124 144
             $this->tracer->pop(true);
125 145
             $this->tracer->pop(false);
126 146
         }
127 147
 
128
-        if ($binding[1]) {
148
+        if ($binding[1])
149
+        {
129 150
             // Indicates singleton
130 151
             /** @psalm-var class-string $alias */
131 152
             $this->state->bindings[$alias] = $instance;
@@ -179,18 +200,23 @@  discard block
 block discarded – undo
179 200
         array $parameters,
180 201
         string $context = null
181 202
     ): mixed {
182
-        if (\is_string($target)) {
203
+        if (\is_string($target))
204
+        {
183 205
             // Reference
184 206
             return $this->make($target, $parameters, $context);
185 207
         }
186 208
 
187
-        if ($target instanceof Autowire) {
209
+        if ($target instanceof Autowire)
210
+        {
188 211
             return $target->resolve($this, $parameters);
189 212
         }
190 213
 
191
-        try {
214
+        try
215
+        {
192 216
             return $this->invoker->invoke($target, $parameters);
193
-        } catch (NotCallableException $e) {
217
+        }
218
+        catch (NotCallableException $e)
219
+        {
194 220
             throw new ContainerException(
195 221
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)),
196 222
                 $e->getCode(),
@@ -214,20 +240,26 @@  discard block
 block discarded – undo
214 240
      */
215 241
     private function createInstance(string $class, array $parameters, string $context = null): object
216 242
     {
217
-        try {
243
+        try
244
+        {
218 245
             $reflection = new \ReflectionClass($class);
219
-        } catch (\ReflectionException $e) {
246
+        }
247
+        catch (\ReflectionException $e)
248
+        {
220 249
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
221 250
         }
222 251
 
223 252
         //We have to construct class using external injector when we know exact context
224
-        if ($parameters === [] && $this->binder->hasInjector($class)) {
253
+        if ($parameters === [] && $this->binder->hasInjector($class))
254
+        {
225 255
             $injector = $this->state->injectors[$reflection->getName()];
226 256
 
227
-            try {
257
+            try
258
+            {
228 259
                 $injectorInstance = $this->container->get($injector);
229 260
 
230
-                if (!$injectorInstance instanceof InjectorInterface) {
261
+                if (!$injectorInstance instanceof InjectorInterface)
262
+                {
231 263
                     throw new InjectionException(
232 264
                         \sprintf(
233 265
                             "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -242,7 +274,8 @@  discard block
 block discarded – undo
242 274
                  * @psalm-suppress RedundantCondition
243 275
                  */
244 276
                 $instance = $injectorInstance->createInjection($reflection, $context);
245
-                if (!$reflection->isInstance($instance)) {
277
+                if (!$reflection->isInstance($instance))
278
+                {
246 279
                     throw new InjectionException(
247 280
                         \sprintf(
248 281
                             "Invalid injection response for '%s'.",
@@ -252,12 +285,15 @@  discard block
 block discarded – undo
252 285
                 }
253 286
 
254 287
                 return $instance;
255
-            } finally {
288
+            }
289
+            finally
290
+            {
256 291
                 $this->state->injectors[$reflection->getName()] = $injector;
257 292
             }
258 293
         }
259 294
 
260
-        if (!$reflection->isInstantiable()) {
295
+        if (!$reflection->isInstantiable())
296
+        {
261 297
             $itIs = match (true) {
262 298
                 $reflection->isEnum() => 'Enum',
263 299
                 $reflection->isAbstract() => 'Abstract class',
@@ -270,12 +306,16 @@  discard block
 block discarded – undo
270 306
 
271 307
         $constructor = $reflection->getConstructor();
272 308
 
273
-        if ($constructor !== null) {
274
-            try {
309
+        if ($constructor !== null)
310
+        {
311
+            try
312
+            {
275 313
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
276 314
                 $this->tracer->push(true);
277 315
                 $arguments = $this->resolver->resolveArguments($constructor, $parameters);
278
-            } catch (ValidationException $e) {
316
+            }
317
+            catch (ValidationException $e)
318
+            {
279 319
                 throw new ContainerException(
280 320
                     $this->tracer->combineTraceMessage(
281 321
                         \sprintf(
@@ -285,22 +325,31 @@  discard block
 block discarded – undo
285 325
                         )
286 326
                     ),
287 327
                 );
288
-            } finally {
328
+            }
329
+            finally
330
+            {
289 331
                 $this->tracer->pop(true);
290 332
                 $this->tracer->pop(false);
291 333
             }
292
-            try {
334
+            try
335
+            {
293 336
                 // Using constructor with resolved arguments
294 337
                 $this->tracer->push(false, call: "$class::__construct", arguments: $arguments);
295 338
                 $this->tracer->push(true);
296 339
                 $instance = new $class(...$arguments);
297
-            } catch (\TypeError $e) {
340
+            }
341
+            catch (\TypeError $e)
342
+            {
298 343
                 throw new WrongTypeException($constructor, $e);
299
-            } finally {
344
+            }
345
+            finally
346
+            {
300 347
                 $this->tracer->pop(true);
301 348
                 $this->tracer->pop(false);
302 349
             }
303
-        } else {
350
+        }
351
+        else
352
+        {
304 353
             // No constructor specified
305 354
             $instance = $reflection->newInstance();
306 355
         }
@@ -321,9 +370,11 @@  discard block
 block discarded – undo
321 370
     private function registerInstance(object $instance): object
322 371
     {
323 372
         //Declarative singletons
324
-        if ($instance instanceof SingletonInterface) {
373
+        if ($instance instanceof SingletonInterface)
374
+        {
325 375
             $alias = $instance::class;
326
-            if (!isset($this->state->bindings[$alias])) {
376
+            if (!isset($this->state->bindings[$alias]))
377
+            {
327 378
                 $this->state->bindings[$alias] = $instance;
328 379
             }
329 380
         }
Please login to merge, or discard this patch.