Passed
Pull Request — master (#300)
by Kirill
03:34
created
src/Core/tests/Fixtures/UnionTypes.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@
 block discarded – undo
13 13
 
14 14
 class UnionTypes
15 15
 {
16
-    public static function example(SampleClass|TypedClass $example)
16
+    public static function example(SampleClass | TypedClass $example)
17 17
     {
18 18
     }
19 19
 }
Please login to merge, or discard this patch.
src/Core/src/Container.php 1 patch
Spacing   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -98,7 +98,7 @@  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
             $type = $parameter->getType();
103 103
             $name = $parameter->getName();
104 104
             $class = null;
@@ -107,17 +107,17 @@  discard block
 block discarded – undo
107 107
             // Container do not currently support union types. In the future, we
108 108
             // can provide the possibility of autowiring based on priorities (TBD).
109 109
             //
110
-            if ($type instanceof \ReflectionUnionType) {
110
+            if ($type instanceof \ReflectionUnionType){
111 111
                 $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously';
112 112
                 $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection));
113 113
 
114 114
                 throw new ContainerException($error);
115 115
             }
116 116
 
117
-            if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
118
-                try {
117
+            if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()){
118
+                try{
119 119
                     $class = new \ReflectionClass($type->getName());
120
-                } catch (\ReflectionException $e) {
120
+                }catch (\ReflectionException $e){
121 121
                     $location = $this->getLocationString($reflection);
122 122
 
123 123
                     $error = 'Unable to resolve `\$%s` parameter in %s: %s';
@@ -127,11 +127,11 @@  discard block
 block discarded – undo
127 127
                 }
128 128
             }
129 129
 
130
-            if (isset($parameters[$name]) && is_object($parameters[$name])) {
131
-                if ($parameters[$name] instanceof Autowire) {
130
+            if (isset($parameters[$name]) && is_object($parameters[$name])){
131
+                if ($parameters[$name] instanceof Autowire){
132 132
                     //Supplied by user as late dependency
133 133
                     $arguments[] = $parameters[$name]->resolve($this);
134
-                } else {
134
+                }else{
135 135
                     //Supplied by user as object
136 136
                     $arguments[] = $parameters[$name];
137 137
                 }
@@ -139,16 +139,16 @@  discard block
 block discarded – undo
139 139
             }
140 140
 
141 141
             // no declared type or scalar type or array
142
-            if (!isset($class)) {
142
+            if (!isset($class)){
143 143
                 //Provided from outside
144
-                if (\array_key_exists($name, $parameters)) {
144
+                if (\array_key_exists($name, $parameters)){
145 145
                     //Make sure it's properly typed
146 146
                     $this->assertType($parameter, $reflection, $parameters[$name]);
147 147
                     $arguments[] = $parameters[$name];
148 148
                     continue;
149 149
                 }
150 150
 
151
-                if ($parameter->isDefaultValueAvailable()) {
151
+                if ($parameter->isDefaultValueAvailable()){
152 152
                     //Default value
153 153
                     $arguments[] = $parameter->getDefaultValue();
154 154
                     continue;
@@ -158,12 +158,12 @@  discard block
 block discarded – undo
158 158
                 throw new ArgumentException($parameter, $reflection);
159 159
             }
160 160
 
161
-            try {
161
+            try{
162 162
                 //Requesting for contextual dependency
163 163
                 $arguments[] = $this->get($class->getName(), $name);
164 164
                 continue;
165
-            } catch (AutowireException $e) {
166
-                if ($parameter->isOptional()) {
165
+            }catch (AutowireException $e){
166
+                if ($parameter->isOptional()){
167 167
                     //This is optional dependency, skip
168 168
                     $arguments[] = null;
169 169
                     continue;
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
      */
194 194
     public function get($alias, string $context = null)
195 195
     {
196
-        if ($alias instanceof Autowire) {
196
+        if ($alias instanceof Autowire){
197 197
             return $alias->resolve($this);
198 198
         }
199 199
 
@@ -209,34 +209,34 @@  discard block
 block discarded – undo
209 209
      */
210 210
     public function make(string $alias, array $parameters = [], string $context = null)
211 211
     {
212
-        if (!isset($this->bindings[$alias])) {
212
+        if (!isset($this->bindings[$alias])){
213 213
             //No direct instructions how to construct class, make is automatically
214 214
             return $this->autowire($alias, $parameters, $context);
215 215
         }
216 216
 
217 217
         $binding = $this->bindings[$alias];
218
-        if (\is_object($binding)) {
218
+        if (\is_object($binding)){
219 219
             //When binding is instance, assuming singleton
220 220
             return $binding;
221 221
         }
222 222
 
223
-        if (\is_string($binding)) {
223
+        if (\is_string($binding)){
224 224
             //Binding is pointing to something else
225 225
             return $this->make($binding, $parameters, $context);
226 226
         }
227 227
 
228 228
         unset($this->bindings[$alias]);
229
-        try {
230
-            if ($binding[0] === $alias) {
229
+        try{
230
+            if ($binding[0] === $alias){
231 231
                 $instance = $this->autowire($alias, $parameters, $context);
232
-            } else {
232
+            }else{
233 233
                 $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context);
234 234
             }
235
-        } finally {
235
+        }finally{
236 236
             $this->bindings[$alias] = $binding;
237 237
         }
238 238
 
239
-        if ($binding[1]) {
239
+        if ($binding[1]){
240 240
             //Indicates singleton
241 241
             $this->bindings[$alias] = $instance;
242 242
         }
@@ -250,28 +250,28 @@  discard block
 block discarded – undo
250 250
     public function runScope(array $bindings, callable $scope)
251 251
     {
252 252
         $cleanup = $previous = [];
253
-        foreach ($bindings as $alias => $resolver) {
254
-            if (isset($this->bindings[$alias])) {
253
+        foreach ($bindings as $alias => $resolver){
254
+            if (isset($this->bindings[$alias])){
255 255
                 $previous[$alias] = $this->bindings[$alias];
256
-            } else {
256
+            }else{
257 257
                 $cleanup[] = $alias;
258 258
             }
259 259
 
260 260
             $this->bind($alias, $resolver);
261 261
         }
262 262
 
263
-        try {
264
-            if (ContainerScope::getContainer() !== $this) {
263
+        try{
264
+            if (ContainerScope::getContainer() !== $this){
265 265
                 return ContainerScope::runScope($this, $scope);
266 266
             }
267 267
 
268 268
             return $scope();
269
-        } finally {
270
-            foreach (\array_reverse($previous) as $alias => $resolver) {
269
+        }finally{
270
+            foreach (\array_reverse($previous) as $alias => $resolver){
271 271
                 $this->bindings[$alias] = $resolver;
272 272
             }
273 273
 
274
-            foreach ($cleanup as $alias) {
274
+            foreach ($cleanup as $alias){
275 275
                 unset($this->bindings[$alias]);
276 276
             }
277 277
         }
@@ -287,7 +287,7 @@  discard block
 block discarded – undo
287 287
      */
288 288
     public function bind(string $alias, $resolver): void
289 289
     {
290
-        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) {
290
+        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){
291 291
             // array means = execute me, false = not singleton
292 292
             $this->bindings[$alias] = [$resolver, false];
293 293
 
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
      */
307 307
     public function bindSingleton(string $alias, $resolver): void
308 308
     {
309
-        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) {
309
+        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){
310 310
             // direct binding to an instance
311 311
             $this->bindings[$alias] = $resolver;
312 312
 
@@ -324,11 +324,11 @@  discard block
 block discarded – undo
324 324
      */
325 325
     public function hasInstance(string $alias): bool
326 326
     {
327
-        if (!$this->has($alias)) {
327
+        if (!$this->has($alias)){
328 328
             return false;
329 329
         }
330 330
 
331
-        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) {
331
+        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){
332 332
             //Checking alias tree
333 333
             $alias = $this->bindings[$alias];
334 334
         }
@@ -410,7 +410,7 @@  discard block
 block discarded – undo
410 410
      */
411 411
     protected function autowire(string $class, array $parameters, string $context = null)
412 412
     {
413
-        if (!\class_exists($class)) {
413
+        if (!\class_exists($class)){
414 414
             throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class));
415 415
         }
416 416
 
@@ -429,7 +429,7 @@  discard block
 block discarded – undo
429 429
     {
430 430
         $location = $reflection->getName();
431 431
 
432
-        if ($reflection instanceof \ReflectionMethod) {
432
+        if ($reflection instanceof \ReflectionMethod){
433 433
             return "{$reflection->getDeclaringClass()->getName()}::{$location}()";
434 434
         }
435 435
 
@@ -448,11 +448,11 @@  discard block
 block discarded – undo
448 448
      */
449 449
     private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void
450 450
     {
451
-        if ($value === null) {
451
+        if ($value === null){
452 452
             if (
453 453
                 !$parameter->isOptional() &&
454 454
                 !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null)
455
-            ) {
455
+            ){
456 456
                 throw new ArgumentException($parameter, $context);
457 457
             }
458 458
 
@@ -460,20 +460,20 @@  discard block
 block discarded – undo
460 460
         }
461 461
 
462 462
         $type = $parameter->getType();
463
-        if ($type === null) {
463
+        if ($type === null){
464 464
             return;
465 465
         }
466 466
 
467 467
         $typeName = $type->getName();
468
-        if ($typeName === 'array' && !\is_array($value)) {
468
+        if ($typeName === 'array' && !\is_array($value)){
469 469
             throw new ArgumentException($parameter, $context);
470 470
         }
471 471
 
472
-        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) {
472
+        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){
473 473
             throw new ArgumentException($parameter, $context);
474 474
         }
475 475
 
476
-        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) {
476
+        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){
477 477
             throw new ArgumentException($parameter, $context);
478 478
         }
479 479
     }
@@ -491,22 +491,22 @@  discard block
 block discarded – undo
491 491
      */
492 492
     private function createInstance(string $class, array $parameters, string $context = null)
493 493
     {
494
-        try {
494
+        try{
495 495
             $reflection = new \ReflectionClass($class);
496
-        } catch (\ReflectionException $e) {
496
+        }catch (\ReflectionException $e){
497 497
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
498 498
         }
499 499
 
500 500
         //We have to construct class using external injector when we know exact context
501
-        if ($parameters === [] && $this->checkInjector($reflection)) {
501
+        if ($parameters === [] && $this->checkInjector($reflection)){
502 502
             $injector = $this->injectors[$reflection->getName()];
503 503
 
504 504
             $instance = null;
505
-            try {
505
+            try{
506 506
                 /** @var InjectorInterface $injectorInstance */
507 507
                 $injectorInstance = $this->get($injector);
508 508
 
509
-                if (!$injectorInstance instanceof InjectorInterface) {
509
+                if (!$injectorInstance instanceof InjectorInterface){
510 510
                     throw new InjectionException(
511 511
                         \sprintf(
512 512
                             "Class '%s' must be an instance of InjectorInterface for '%s'",
@@ -517,7 +517,7 @@  discard block
 block discarded – undo
517 517
                 }
518 518
 
519 519
                 $instance = $injectorInstance->createInjection($reflection, $context);
520
-                if (!$reflection->isInstance($instance)) {
520
+                if (!$reflection->isInstance($instance)){
521 521
                     throw new InjectionException(
522 522
                         \sprintf(
523 523
                             "Invalid injection response for '%s'",
@@ -525,23 +525,23 @@  discard block
 block discarded – undo
525 525
                         )
526 526
                     );
527 527
                 }
528
-            } finally {
528
+            }finally{
529 529
                 $this->injectors[$reflection->getName()] = $injector;
530 530
             }
531 531
 
532 532
             return $instance;
533 533
         }
534 534
 
535
-        if (!$reflection->isInstantiable()) {
535
+        if (!$reflection->isInstantiable()){
536 536
             throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class));
537 537
         }
538 538
 
539 539
         $constructor = $reflection->getConstructor();
540 540
 
541
-        if ($constructor !== null) {
541
+        if ($constructor !== null){
542 542
             // Using constructor with resolved arguments
543 543
             $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters));
544
-        } else {
544
+        }else{
545 545
             // No constructor specified
546 546
             $instance = $reflection->newInstance();
547 547
         }
@@ -558,28 +558,28 @@  discard block
 block discarded – undo
558 558
     private function checkInjector(\ReflectionClass $reflection): bool
559 559
     {
560 560
         $class = $reflection->getName();
561
-        if (\array_key_exists($class, $this->injectors)) {
561
+        if (\array_key_exists($class, $this->injectors)){
562 562
             return $this->injectors[$class] !== null;
563 563
         }
564 564
 
565 565
         if (
566 566
             $reflection->implementsInterface(InjectableInterface::class)
567 567
             && $reflection->hasConstant('INJECTOR')
568
-        ) {
568
+        ){
569 569
             $this->injectors[$class] = $reflection->getConstant('INJECTOR');
570 570
 
571 571
             return true;
572 572
         }
573 573
 
574
-        if (!isset($this->injectorsCache[$class])) {
574
+        if (!isset($this->injectorsCache[$class])){
575 575
             $this->injectorsCache[$class] = null;
576 576
 
577 577
             // check interfaces
578
-            foreach ($this->injectors as $target => $injector) {
578
+            foreach ($this->injectors as $target => $injector){
579 579
                 if (
580 580
                     \class_exists($target, true)
581 581
                     && $reflection->isSubclassOf($target)
582
-                ) {
582
+                ){
583 583
                     $this->injectors[$class] = $injector;
584 584
 
585 585
                     return true;
@@ -588,7 +588,7 @@  discard block
 block discarded – undo
588 588
                 if (
589 589
                     \interface_exists($target, true)
590 590
                     && $reflection->implementsInterface($target)
591
-                ) {
591
+                ){
592 592
                     $this->injectors[$class] = $injector;
593 593
 
594 594
                     return true;
@@ -610,9 +610,9 @@  discard block
 block discarded – undo
610 610
     private function registerInstance($instance, array $parameters)
611 611
     {
612 612
         //Declarative singletons (only when class received via direct get)
613
-        if ($parameters === [] && $instance instanceof SingletonInterface) {
613
+        if ($parameters === [] && $instance instanceof SingletonInterface){
614 614
             $alias = \get_class($instance);
615
-            if (!isset($this->bindings[$alias])) {
615
+            if (!isset($this->bindings[$alias])){
616 616
                 $this->bindings[$alias] = $instance;
617 617
             }
618 618
         }
@@ -636,20 +636,20 @@  discard block
 block discarded – undo
636 636
         $target,
637 637
         array $parameters,
638 638
         string $context = null
639
-    ) {
640
-        if (\is_string($target)) {
639
+    ){
640
+        if (\is_string($target)){
641 641
             //Reference
642 642
             return $this->make($target, $parameters, $context);
643 643
         }
644 644
 
645
-        if ($target instanceof Autowire) {
645
+        if ($target instanceof Autowire){
646 646
             return $target->resolve($this, $parameters);
647 647
         }
648 648
 
649
-        if ($target instanceof \Closure) {
650
-            try {
649
+        if ($target instanceof \Closure){
650
+            try{
651 651
                 $reflection = new \ReflectionFunction($target);
652
-            } catch (\ReflectionException $e) {
652
+            }catch (\ReflectionException $e){
653 653
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
654 654
             }
655 655
 
@@ -659,16 +659,16 @@  discard block
 block discarded – undo
659 659
             );
660 660
         }
661 661
 
662
-        if (is_array($target) && isset($target[1])) {
662
+        if (is_array($target) && isset($target[1])){
663 663
             //In a form of resolver and method
664 664
             [$resolver, $method] = $target;
665 665
 
666 666
             //Resolver instance (i.e. [ClassName::class, 'method'])
667 667
             $resolver = $this->get($resolver);
668 668
 
669
-            try {
669
+            try{
670 670
                 $method = new \ReflectionMethod($resolver, $method);
671
-            } catch (\ReflectionException $e) {
671
+            }catch (\ReflectionException $e){
672 672
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
673 673
             }
674 674
 
Please login to merge, or discard this patch.