Passed
Pull Request — master (#300)
by Kirill
04:04
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 contain union type hint which 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
                     $error = "Unable to resolve `\${$parameter->getName()}` parameter in {$location}: {$e->getMessage()}";
123 123
 
@@ -125,11 +125,11 @@  discard block
 block discarded – undo
125 125
                 }
126 126
             }
127 127
 
128
-            if (isset($parameters[$name]) && is_object($parameters[$name])) {
129
-                if ($parameters[$name] instanceof Autowire) {
128
+            if (isset($parameters[$name]) && is_object($parameters[$name])){
129
+                if ($parameters[$name] instanceof Autowire){
130 130
                     //Supplied by user as late dependency
131 131
                     $arguments[] = $parameters[$name]->resolve($this);
132
-                } else {
132
+                }else{
133 133
                     //Supplied by user as object
134 134
                     $arguments[] = $parameters[$name];
135 135
                 }
@@ -137,16 +137,16 @@  discard block
 block discarded – undo
137 137
             }
138 138
 
139 139
             // no declared type or scalar type or array
140
-            if (!isset($class)) {
140
+            if (!isset($class)){
141 141
                 //Provided from outside
142
-                if (\array_key_exists($name, $parameters)) {
142
+                if (\array_key_exists($name, $parameters)){
143 143
                     //Make sure it's properly typed
144 144
                     $this->assertType($parameter, $reflection, $parameters[$name]);
145 145
                     $arguments[] = $parameters[$name];
146 146
                     continue;
147 147
                 }
148 148
 
149
-                if ($parameter->isDefaultValueAvailable()) {
149
+                if ($parameter->isDefaultValueAvailable()){
150 150
                     //Default value
151 151
                     $arguments[] = $parameter->getDefaultValue();
152 152
                     continue;
@@ -156,12 +156,12 @@  discard block
 block discarded – undo
156 156
                 throw new ArgumentException($parameter, $reflection);
157 157
             }
158 158
 
159
-            try {
159
+            try{
160 160
                 //Requesting for contextual dependency
161 161
                 $arguments[] = $this->get($class->getName(), $name);
162 162
                 continue;
163
-            } catch (AutowireException $e) {
164
-                if ($parameter->isOptional()) {
163
+            }catch (AutowireException $e){
164
+                if ($parameter->isOptional()){
165 165
                     //This is optional dependency, skip
166 166
                     $arguments[] = null;
167 167
                     continue;
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
      */
192 192
     public function get($alias, string $context = null)
193 193
     {
194
-        if ($alias instanceof Autowire) {
194
+        if ($alias instanceof Autowire){
195 195
             return $alias->resolve($this);
196 196
         }
197 197
 
@@ -207,34 +207,34 @@  discard block
 block discarded – undo
207 207
      */
208 208
     public function make(string $alias, array $parameters = [], string $context = null)
209 209
     {
210
-        if (!isset($this->bindings[$alias])) {
210
+        if (!isset($this->bindings[$alias])){
211 211
             //No direct instructions how to construct class, make is automatically
212 212
             return $this->autowire($alias, $parameters, $context);
213 213
         }
214 214
 
215 215
         $binding = $this->bindings[$alias];
216
-        if (\is_object($binding)) {
216
+        if (\is_object($binding)){
217 217
             //When binding is instance, assuming singleton
218 218
             return $binding;
219 219
         }
220 220
 
221
-        if (\is_string($binding)) {
221
+        if (\is_string($binding)){
222 222
             //Binding is pointing to something else
223 223
             return $this->make($binding, $parameters, $context);
224 224
         }
225 225
 
226 226
         unset($this->bindings[$alias]);
227
-        try {
228
-            if ($binding[0] === $alias) {
227
+        try{
228
+            if ($binding[0] === $alias){
229 229
                 $instance = $this->autowire($alias, $parameters, $context);
230
-            } else {
230
+            }else{
231 231
                 $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context);
232 232
             }
233
-        } finally {
233
+        }finally{
234 234
             $this->bindings[$alias] = $binding;
235 235
         }
236 236
 
237
-        if ($binding[1]) {
237
+        if ($binding[1]){
238 238
             //Indicates singleton
239 239
             $this->bindings[$alias] = $instance;
240 240
         }
@@ -248,28 +248,28 @@  discard block
 block discarded – undo
248 248
     public function runScope(array $bindings, callable $scope)
249 249
     {
250 250
         $cleanup = $previous = [];
251
-        foreach ($bindings as $alias => $resolver) {
252
-            if (isset($this->bindings[$alias])) {
251
+        foreach ($bindings as $alias => $resolver){
252
+            if (isset($this->bindings[$alias])){
253 253
                 $previous[$alias] = $this->bindings[$alias];
254
-            } else {
254
+            }else{
255 255
                 $cleanup[] = $alias;
256 256
             }
257 257
 
258 258
             $this->bind($alias, $resolver);
259 259
         }
260 260
 
261
-        try {
262
-            if (ContainerScope::getContainer() !== $this) {
261
+        try{
262
+            if (ContainerScope::getContainer() !== $this){
263 263
                 return ContainerScope::runScope($this, $scope);
264 264
             }
265 265
 
266 266
             return $scope();
267
-        } finally {
268
-            foreach (\array_reverse($previous) as $alias => $resolver) {
267
+        }finally{
268
+            foreach (\array_reverse($previous) as $alias => $resolver){
269 269
                 $this->bindings[$alias] = $resolver;
270 270
             }
271 271
 
272
-            foreach ($cleanup as $alias) {
272
+            foreach ($cleanup as $alias){
273 273
                 unset($this->bindings[$alias]);
274 274
             }
275 275
         }
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
      */
286 286
     public function bind(string $alias, $resolver): void
287 287
     {
288
-        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) {
288
+        if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){
289 289
             // array means = execute me, false = not singleton
290 290
             $this->bindings[$alias] = [$resolver, false];
291 291
 
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
      */
305 305
     public function bindSingleton(string $alias, $resolver): void
306 306
     {
307
-        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) {
307
+        if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){
308 308
             // direct binding to an instance
309 309
             $this->bindings[$alias] = $resolver;
310 310
 
@@ -322,11 +322,11 @@  discard block
 block discarded – undo
322 322
      */
323 323
     public function hasInstance(string $alias): bool
324 324
     {
325
-        if (!$this->has($alias)) {
325
+        if (!$this->has($alias)){
326 326
             return false;
327 327
         }
328 328
 
329
-        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) {
329
+        while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){
330 330
             //Checking alias tree
331 331
             $alias = $this->bindings[$alias];
332 332
         }
@@ -408,7 +408,7 @@  discard block
 block discarded – undo
408 408
      */
409 409
     protected function autowire(string $class, array $parameters, string $context = null)
410 410
     {
411
-        if (!\class_exists($class)) {
411
+        if (!\class_exists($class)){
412 412
             throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class));
413 413
         }
414 414
 
@@ -427,7 +427,7 @@  discard block
 block discarded – undo
427 427
     {
428 428
         $location = $reflection->getName();
429 429
 
430
-        if ($reflection instanceof \ReflectionMethod) {
430
+        if ($reflection instanceof \ReflectionMethod){
431 431
             return "{$reflection->getDeclaringClass()->getName()}::{$location}()";
432 432
         }
433 433
 
@@ -446,11 +446,11 @@  discard block
 block discarded – undo
446 446
      */
447 447
     private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void
448 448
     {
449
-        if ($value === null) {
449
+        if ($value === null){
450 450
             if (
451 451
                 !$parameter->isOptional() &&
452 452
                 !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null)
453
-            ) {
453
+            ){
454 454
                 throw new ArgumentException($parameter, $context);
455 455
             }
456 456
 
@@ -458,20 +458,20 @@  discard block
 block discarded – undo
458 458
         }
459 459
 
460 460
         $type = $parameter->getType();
461
-        if ($type === null) {
461
+        if ($type === null){
462 462
             return;
463 463
         }
464 464
 
465 465
         $typeName = $type->getName();
466
-        if ($typeName === 'array' && !\is_array($value)) {
466
+        if ($typeName === 'array' && !\is_array($value)){
467 467
             throw new ArgumentException($parameter, $context);
468 468
         }
469 469
 
470
-        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) {
470
+        if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){
471 471
             throw new ArgumentException($parameter, $context);
472 472
         }
473 473
 
474
-        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) {
474
+        if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){
475 475
             throw new ArgumentException($parameter, $context);
476 476
         }
477 477
     }
@@ -489,22 +489,22 @@  discard block
 block discarded – undo
489 489
      */
490 490
     private function createInstance(string $class, array $parameters, string $context = null)
491 491
     {
492
-        try {
492
+        try{
493 493
             $reflection = new \ReflectionClass($class);
494
-        } catch (\ReflectionException $e) {
494
+        }catch (\ReflectionException $e){
495 495
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
496 496
         }
497 497
 
498 498
         //We have to construct class using external injector when we know exact context
499
-        if ($parameters === [] && $this->checkInjector($reflection)) {
499
+        if ($parameters === [] && $this->checkInjector($reflection)){
500 500
             $injector = $this->injectors[$reflection->getName()];
501 501
 
502 502
             $instance = null;
503
-            try {
503
+            try{
504 504
                 /** @var InjectorInterface $injectorInstance */
505 505
                 $injectorInstance = $this->get($injector);
506 506
 
507
-                if (!$injectorInstance instanceof InjectorInterface) {
507
+                if (!$injectorInstance instanceof InjectorInterface){
508 508
                     throw new InjectionException(
509 509
                         \sprintf(
510 510
                             "Class '%s' must be an instance of InjectorInterface for '%s'",
@@ -515,7 +515,7 @@  discard block
 block discarded – undo
515 515
                 }
516 516
 
517 517
                 $instance = $injectorInstance->createInjection($reflection, $context);
518
-                if (!$reflection->isInstance($instance)) {
518
+                if (!$reflection->isInstance($instance)){
519 519
                     throw new InjectionException(
520 520
                         \sprintf(
521 521
                             "Invalid injection response for '%s'",
@@ -523,23 +523,23 @@  discard block
 block discarded – undo
523 523
                         )
524 524
                     );
525 525
                 }
526
-            } finally {
526
+            }finally{
527 527
                 $this->injectors[$reflection->getName()] = $injector;
528 528
             }
529 529
 
530 530
             return $instance;
531 531
         }
532 532
 
533
-        if (!$reflection->isInstantiable()) {
533
+        if (!$reflection->isInstantiable()){
534 534
             throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class));
535 535
         }
536 536
 
537 537
         $constructor = $reflection->getConstructor();
538 538
 
539
-        if ($constructor !== null) {
539
+        if ($constructor !== null){
540 540
             // Using constructor with resolved arguments
541 541
             $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters));
542
-        } else {
542
+        }else{
543 543
             // No constructor specified
544 544
             $instance = $reflection->newInstance();
545 545
         }
@@ -556,28 +556,28 @@  discard block
 block discarded – undo
556 556
     private function checkInjector(\ReflectionClass $reflection): bool
557 557
     {
558 558
         $class = $reflection->getName();
559
-        if (\array_key_exists($class, $this->injectors)) {
559
+        if (\array_key_exists($class, $this->injectors)){
560 560
             return $this->injectors[$class] !== null;
561 561
         }
562 562
 
563 563
         if (
564 564
             $reflection->implementsInterface(InjectableInterface::class)
565 565
             && $reflection->hasConstant('INJECTOR')
566
-        ) {
566
+        ){
567 567
             $this->injectors[$class] = $reflection->getConstant('INJECTOR');
568 568
 
569 569
             return true;
570 570
         }
571 571
 
572
-        if (!isset($this->injectorsCache[$class])) {
572
+        if (!isset($this->injectorsCache[$class])){
573 573
             $this->injectorsCache[$class] = null;
574 574
 
575 575
             // check interfaces
576
-            foreach ($this->injectors as $target => $injector) {
576
+            foreach ($this->injectors as $target => $injector){
577 577
                 if (
578 578
                     \class_exists($target, true)
579 579
                     && $reflection->isSubclassOf($target)
580
-                ) {
580
+                ){
581 581
                     $this->injectors[$class] = $injector;
582 582
 
583 583
                     return true;
@@ -586,7 +586,7 @@  discard block
 block discarded – undo
586 586
                 if (
587 587
                     \interface_exists($target, true)
588 588
                     && $reflection->implementsInterface($target)
589
-                ) {
589
+                ){
590 590
                     $this->injectors[$class] = $injector;
591 591
 
592 592
                     return true;
@@ -608,9 +608,9 @@  discard block
 block discarded – undo
608 608
     private function registerInstance($instance, array $parameters)
609 609
     {
610 610
         //Declarative singletons (only when class received via direct get)
611
-        if ($parameters === [] && $instance instanceof SingletonInterface) {
611
+        if ($parameters === [] && $instance instanceof SingletonInterface){
612 612
             $alias = \get_class($instance);
613
-            if (!isset($this->bindings[$alias])) {
613
+            if (!isset($this->bindings[$alias])){
614 614
                 $this->bindings[$alias] = $instance;
615 615
             }
616 616
         }
@@ -634,20 +634,20 @@  discard block
 block discarded – undo
634 634
         $target,
635 635
         array $parameters,
636 636
         string $context = null
637
-    ) {
638
-        if (\is_string($target)) {
637
+    ){
638
+        if (\is_string($target)){
639 639
             //Reference
640 640
             return $this->make($target, $parameters, $context);
641 641
         }
642 642
 
643
-        if ($target instanceof Autowire) {
643
+        if ($target instanceof Autowire){
644 644
             return $target->resolve($this, $parameters);
645 645
         }
646 646
 
647
-        if ($target instanceof \Closure) {
648
-            try {
647
+        if ($target instanceof \Closure){
648
+            try{
649 649
                 $reflection = new \ReflectionFunction($target);
650
-            } catch (\ReflectionException $e) {
650
+            }catch (\ReflectionException $e){
651 651
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
652 652
             }
653 653
 
@@ -657,16 +657,16 @@  discard block
 block discarded – undo
657 657
             );
658 658
         }
659 659
 
660
-        if (is_array($target) && isset($target[1])) {
660
+        if (is_array($target) && isset($target[1])){
661 661
             //In a form of resolver and method
662 662
             [$resolver, $method] = $target;
663 663
 
664 664
             //Resolver instance (i.e. [ClassName::class, 'method'])
665 665
             $resolver = $this->get($resolver);
666 666
 
667
-            try {
667
+            try{
668 668
                 $method = new \ReflectionMethod($resolver, $method);
669
-            } catch (\ReflectionException $e) {
669
+            }catch (\ReflectionException $e){
670 670
                 throw new ContainerException($e->getMessage(), $e->getCode(), $e);
671 671
             }
672 672
 
Please login to merge, or discard this patch.