Passed
Pull Request — master (#300)
by Kirill
03:34
created
src/Core/src/Container.php 1 patch
Braces   +142 added lines, -65 removed lines patch added patch discarded remove patch
@@ -98,7 +98,8 @@  discard block
 block discarded – undo
98 98
     {
99 99
         $arguments = [];
100 100
 
101
-        foreach ($reflection->getParameters() as $parameter) {
101
+        foreach ($reflection->getParameters() as $parameter)
102
+        {
102 103
             $type = $parameter->getType();
103 104
             $name = $parameter->getName();
104 105
             $class = null;
@@ -107,17 +108,22 @@  discard block
 block discarded – undo
107 108
             // Container do not currently support union types. In the future, we
108 109
             // can provide the possibility of autowiring based on priorities (TBD).
109 110
             //
110
-            if ($type instanceof \ReflectionUnionType) {
111
+            if ($type instanceof \ReflectionUnionType)
112
+            {
111 113
                 $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously';
112 114
                 $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection));
113 115
 
114 116
                 throw new ContainerException($error);
115 117
             }
116 118
 
117
-            if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
118
-                try {
119
+            if ($type instanceof \ReflectionNamedType && !$type->isBuiltin())
120
+            {
121
+                try
122
+                {
119 123
                     $class = new \ReflectionClass($type->getName());
120
-                } catch (\ReflectionException $e) {
124
+                }
125
+                catch (\ReflectionException $e)
126
+                {
121 127
                     $location = $this->getLocationString($reflection);
122 128
 
123 129
                     $error = 'Unable to resolve `\$%s` parameter in %s: %s';
@@ -127,11 +133,15 @@  discard block
 block discarded – undo
127 133
                 }
128 134
             }
129 135
 
130
-            if (isset($parameters[$name]) && is_object($parameters[$name])) {
131
-                if ($parameters[$name] instanceof Autowire) {
136
+            if (isset($parameters[$name]) && is_object($parameters[$name]))
137
+            {
138
+                if ($parameters[$name] instanceof Autowire)
139
+                {
132 140
                     //Supplied by user as late dependency
133 141
                     $arguments[] = $parameters[$name]->resolve($this);
134
-                } else {
142
+                }
143
+                else
144
+                {
135 145
                     //Supplied by user as object
136 146
                     $arguments[] = $parameters[$name];
137 147
                 }
@@ -139,16 +149,19 @@  discard block
 block discarded – undo
139 149
             }
140 150
 
141 151
             // no declared type or scalar type or array
142
-            if (!isset($class)) {
152
+            if (!isset($class))
153
+            {
143 154
                 //Provided from outside
144
-                if (\array_key_exists($name, $parameters)) {
155
+                if (\array_key_exists($name, $parameters))
156
+                {
145 157
                     //Make sure it's properly typed
146 158
                     $this->assertType($parameter, $reflection, $parameters[$name]);
147 159
                     $arguments[] = $parameters[$name];
148 160
                     continue;
149 161
                 }
150 162
 
151
-                if ($parameter->isDefaultValueAvailable()) {
163
+                if ($parameter->isDefaultValueAvailable())
164
+                {
152 165
                     //Default value
153 166
                     $arguments[] = $parameter->getDefaultValue();
154 167
                     continue;
@@ -158,12 +171,16 @@  discard block
 block discarded – undo
158 171
                 throw new ArgumentException($parameter, $reflection);
159 172
             }
160 173
 
161
-            try {
174
+            try
175
+            {
162 176
                 //Requesting for contextual dependency
163 177
                 $arguments[] = $this->get($class->getName(), $name);
164 178
                 continue;
165
-            } catch (AutowireException $e) {
166
-                if ($parameter->isOptional()) {
179
+            }
180
+            catch (AutowireException $e)
181
+            {
182
+                if ($parameter->isOptional())
183
+                {
167 184
                     //This is optional dependency, skip
168 185
                     $arguments[] = null;
169 186
                     continue;
@@ -193,7 +210,8 @@  discard block
 block discarded – undo
193 210
      */
194 211
     public function get($alias, string $context = null)
195 212
     {
196
-        if ($alias instanceof Autowire) {
213
+        if ($alias instanceof Autowire)
214
+        {
197 215
             return $alias->resolve($this);
198 216
         }
199 217
 
@@ -209,34 +227,44 @@  discard block
 block discarded – undo
209 227
      */
210 228
     public function make(string $alias, array $parameters = [], string $context = null)
211 229
     {
212
-        if (!isset($this->bindings[$alias])) {
230
+        if (!isset($this->bindings[$alias]))
231
+        {
213 232
             //No direct instructions how to construct class, make is automatically
214 233
             return $this->autowire($alias, $parameters, $context);
215 234
         }
216 235
 
217 236
         $binding = $this->bindings[$alias];
218
-        if (\is_object($binding)) {
237
+        if (\is_object($binding))
238
+        {
219 239
             //When binding is instance, assuming singleton
220 240
             return $binding;
221 241
         }
222 242
 
223
-        if (\is_string($binding)) {
243
+        if (\is_string($binding))
244
+        {
224 245
             //Binding is pointing to something else
225 246
             return $this->make($binding, $parameters, $context);
226 247
         }
227 248
 
228 249
         unset($this->bindings[$alias]);
229
-        try {
230
-            if ($binding[0] === $alias) {
250
+        try
251
+        {
252
+            if ($binding[0] === $alias)
253
+            {
231 254
                 $instance = $this->autowire($alias, $parameters, $context);
232
-            } else {
255
+            }
256
+            else
257
+            {
233 258
                 $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context);
234 259
             }
235
-        } finally {
260
+        }
261
+        finally
262
+        {
236 263
             $this->bindings[$alias] = $binding;
237 264
         }
238 265
 
239
-        if ($binding[1]) {
266
+        if ($binding[1])
267
+        {
240 268
             //Indicates singleton
241 269
             $this->bindings[$alias] = $instance;
242 270
         }
@@ -250,28 +278,38 @@  discard block
 block discarded – undo
250 278
     public function runScope(array $bindings, callable $scope)
251 279
     {
252 280
         $cleanup = $previous = [];
253
-        foreach ($bindings as $alias => $resolver) {
254
-            if (isset($this->bindings[$alias])) {
281
+        foreach ($bindings as $alias => $resolver)
282
+        {
283
+            if (isset($this->bindings[$alias]))
284
+            {
255 285
                 $previous[$alias] = $this->bindings[$alias];
256
-            } else {
286
+            }
287
+            else
288
+            {
257 289
                 $cleanup[] = $alias;
258 290
             }
259 291
 
260 292
             $this->bind($alias, $resolver);
261 293
         }
262 294
 
263
-        try {
264
-            if (ContainerScope::getContainer() !== $this) {
295
+        try
296
+        {
297
+            if (ContainerScope::getContainer() !== $this)
298
+            {
265 299
                 return ContainerScope::runScope($this, $scope);
266 300
             }
267 301
 
268 302
             return $scope();
269
-        } finally {
270
-            foreach (\array_reverse($previous) as $alias => $resolver) {
303
+        }
304
+        finally
305
+        {
306
+            foreach (\array_reverse($previous) as $alias => $resolver)
307
+            {
271 308
                 $this->bindings[$alias] = $resolver;
272 309
             }
273 310
 
274
-            foreach ($cleanup as $alias) {
311
+            foreach ($cleanup as $alias)
312
+            {
275 313
                 unset($this->bindings[$alias]);
276 314
             }
277 315
         }
@@ -287,7 +325,8 @@  discard block
 block discarded – undo
287 325
      */
288 326
     public function bind(string $alias, $resolver): void
289 327
     {
290
-        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) {
328
+        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire)
329
+        {
291 330
             // array means = execute me, false = not singleton
292 331
             $this->bindings[$alias] = [$resolver, false];
293 332
 
@@ -306,7 +345,8 @@  discard block
 block discarded – undo
306 345
      */
307 346
     public function bindSingleton(string $alias, $resolver): void
308 347
     {
309
-        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) {
348
+        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire)
349
+        {
310 350
             // direct binding to an instance
311 351
             $this->bindings[$alias] = $resolver;
312 352
 
@@ -324,11 +364,13 @@  discard block
 block discarded – undo
324 364
      */
325 365
     public function hasInstance(string $alias): bool
326 366
     {
327
-        if (!$this->has($alias)) {
367
+        if (!$this->has($alias))
368
+        {
328 369
             return false;
329 370
         }
330 371
 
331
-        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) {
372
+        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias]))
373
+        {
332 374
             //Checking alias tree
333 375
             $alias = $this->bindings[$alias];
334 376
         }
@@ -410,7 +452,8 @@  discard block
 block discarded – undo
410 452
      */
411 453
     protected function autowire(string $class, array $parameters, string $context = null)
412 454
     {
413
-        if (!\class_exists($class)) {
455
+        if (!\class_exists($class))
456
+        {
414 457
             throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class));
415 458
         }
416 459
 
@@ -429,7 +472,8 @@  discard block
 block discarded – undo
429 472
     {
430 473
         $location = $reflection->getName();
431 474
 
432
-        if ($reflection instanceof \ReflectionMethod) {
475
+        if ($reflection instanceof \ReflectionMethod)
476
+        {
433 477
             return "{$reflection->getDeclaringClass()->getName()}::{$location}()";
434 478
         }
435 479
 
@@ -448,7 +492,8 @@  discard block
 block discarded – undo
448 492
      */
449 493
     private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void
450 494
     {
451
-        if ($value === null) {
495
+        if ($value === null)
496
+        {
452 497
             if (
453 498
                 !$parameter->isOptional() &&
454 499
                 !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null)
@@ -460,20 +505,24 @@  discard block
 block discarded – undo
460 505
         }
461 506
 
462 507
         $type = $parameter->getType();
463
-        if ($type === null) {
508
+        if ($type === null)
509
+        {
464 510
             return;
465 511
         }
466 512
 
467 513
         $typeName = $type->getName();
468
-        if ($typeName === 'array' && !\is_array($value)) {
514
+        if ($typeName === 'array' && !\is_array($value))
515
+        {
469 516
             throw new ArgumentException($parameter, $context);
470 517
         }
471 518
 
472
-        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) {
519
+        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value))
520
+        {
473 521
             throw new ArgumentException($parameter, $context);
474 522
         }
475 523
 
476
-        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) {
524
+        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value))
525
+        {
477 526
             throw new ArgumentException($parameter, $context);
478 527
         }
479 528
     }
@@ -491,22 +540,28 @@  discard block
 block discarded – undo
491 540
      */
492 541
     private function createInstance(string $class, array $parameters, string $context = null)
493 542
     {
494
-        try {
543
+        try
544
+        {
495 545
             $reflection = new \ReflectionClass($class);
496
-        } catch (\ReflectionException $e) {
546
+        }
547
+        catch (\ReflectionException $e)
548
+        {
497 549
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
498 550
         }
499 551
 
500 552
         //We have to construct class using external injector when we know exact context
501
-        if ($parameters === [] && $this->checkInjector($reflection)) {
553
+        if ($parameters === [] && $this->checkInjector($reflection))
554
+        {
502 555
             $injector = $this->injectors[$reflection->getName()];
503 556
 
504 557
             $instance = null;
505
-            try {
558
+            try
559
+            {
506 560
                 /** @var InjectorInterface $injectorInstance */
507 561
                 $injectorInstance = $this->get($injector);
508 562
 
509
-                if (!$injectorInstance instanceof InjectorInterface) {
563
+                if (!$injectorInstance instanceof InjectorInterface)
564
+                {
510 565
                     throw new InjectionException(
511 566
                         \sprintf(
512 567
                             "Class '%s' must be an instance of InjectorInterface for '%s'",
@@ -517,7 +572,8 @@  discard block
 block discarded – undo
517 572
                 }
518 573
 
519 574
                 $instance = $injectorInstance->createInjection($reflection, $context);
520
-                if (!$reflection->isInstance($instance)) {
575
+                if (!$reflection->isInstance($instance))
576
+                {
521 577
                     throw new InjectionException(
522 578
                         \sprintf(
523 579
                             "Invalid injection response for '%s'",
@@ -525,23 +581,29 @@  discard block
 block discarded – undo
525 581
                         )
526 582
                     );
527 583
                 }
528
-            } finally {
584
+            }
585
+            finally
586
+            {
529 587
                 $this->injectors[$reflection->getName()] = $injector;
530 588
             }
531 589
 
532 590
             return $instance;
533 591
         }
534 592
 
535
-        if (!$reflection->isInstantiable()) {
593
+        if (!$reflection->isInstantiable())
594
+        {
536 595
             throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class));
537 596
         }
538 597
 
539 598
         $constructor = $reflection->getConstructor();
540 599
 
541
-        if ($constructor !== null) {
600
+        if ($constructor !== null)
601
+        {
542 602
             // Using constructor with resolved arguments
543 603
             $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters));
544
-        } else {
604
+        }
605
+        else
606
+        {
545 607
             // No constructor specified
546 608
             $instance = $reflection->newInstance();
547 609
         }
@@ -558,7 +620,8 @@  discard block
 block discarded – undo
558 620
     private function checkInjector(\ReflectionClass $reflection): bool
559 621
     {
560 622
         $class = $reflection->getName();
561
-        if (\array_key_exists($class, $this->injectors)) {
623
+        if (\array_key_exists($class, $this->injectors))
624
+        {
562 625
             return $this->injectors[$class] !== null;
563 626
         }
564 627
 
@@ -571,11 +634,13 @@  discard block
 block discarded – undo
571 634
             return true;
572 635
         }
573 636
 
574
-        if (!isset($this->injectorsCache[$class])) {
637
+        if (!isset($this->injectorsCache[$class]))
638
+        {
575 639
             $this->injectorsCache[$class] = null;
576 640
 
577 641
             // check interfaces
578
-            foreach ($this->injectors as $target => $injector) {
642
+            foreach ($this->injectors as $target => $injector)
643
+            {
579 644
                 if (
580 645
                     \class_exists($target, true)
581 646
                     && $reflection->isSubclassOf($target)
@@ -610,9 +675,11 @@  discard block
 block discarded – undo
610 675
     private function registerInstance($instance, array $parameters)
611 676
     {
612 677
         //Declarative singletons (only when class received via direct get)
613
-        if ($parameters === [] && $instance instanceof SingletonInterface) {
678
+        if ($parameters === [] && $instance instanceof SingletonInterface)
679
+        {
614 680
             $alias = \get_class($instance);
615
-            if (!isset($this->bindings[$alias])) {
681
+            if (!isset($this->bindings[$alias]))
682
+            {
616 683
                 $this->bindings[$alias] = $instance;
617 684
             }
618 685
         }
@@ -637,19 +704,25 @@  discard block
 block discarded – undo
637 704
         array $parameters,
638 705
         string $context = null
639 706
     ) {
640
-        if (\is_string($target)) {
707
+        if (\is_string($target))
708
+        {
641 709
             //Reference
642 710
             return $this->make($target, $parameters, $context);
643 711
         }
644 712
 
645
-        if ($target instanceof Autowire) {
713
+        if ($target instanceof Autowire)
714
+        {
646 715
             return $target->resolve($this, $parameters);
647 716
         }
648 717
 
649
-        if ($target instanceof \Closure) {
650
-            try {
718
+        if ($target instanceof \Closure)
719
+        {
720
+            try
721
+            {
651 722
                 $reflection = new \ReflectionFunction($target);
652
-            } catch (\ReflectionException $e) {
723
+            }
724
+            catch (\ReflectionException $e)
725
+            {
653 726
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
654 727
             }
655 728
 
@@ -659,16 +732,20 @@  discard block
 block discarded – undo
659 732
             );
660 733
         }
661 734
 
662
-        if (is_array($target) && isset($target[1])) {
735
+        if (is_array($target) && isset($target[1]))
736
+        {
663 737
             //In a form of resolver and method
664 738
             [$resolver, $method] = $target;
665 739
 
666 740
             //Resolver instance (i.e. [ClassName::class, 'method'])
667 741
             $resolver = $this->get($resolver);
668 742
 
669
-            try {
743
+            try
744
+            {
670 745
                 $method = new \ReflectionMethod($resolver, $method);
671
-            } catch (\ReflectionException $e) {
746
+            }
747
+            catch (\ReflectionException $e)
748
+            {
672 749
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
673 750
             }
674 751
 
Please login to merge, or discard this patch.