Test Failed
Pull Request — master (#1041)
by Aleksei
06:38
created
src/Core/src/Internal/Container.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -49,7 +49,8 @@
 block discarded – undo
49 49
      */
50 50
     public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed
51 51
     {
52
-        if ($id instanceof Autowire) {
52
+        if ($id instanceof Autowire)
53
+        {
53 54
             return $id->resolve($this->factory);
54 55
         }
55 56
 
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Braces   +119 added lines, -51 removed lines patch added patch discarded remove patch
@@ -68,17 +68,20 @@  discard block
 block discarded – undo
68 68
      */
69 69
     public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed
70 70
     {
71
-        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) {
71
+        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons))
72
+        {
72 73
             return $this->state->singletons[$alias];
73 74
         }
74 75
 
75 76
         $binding = $this->state->bindings[$alias] ?? null;
76 77
 
77
-        if ($binding === null) {
78
+        if ($binding === null)
79
+        {
78 80
             return $this->resolveWithoutBinding($alias, $parameters, $context);
79 81
         }
80 82
 
81
-        try {
83
+        try
84
+        {
82 85
             $this->tracer->push(
83 86
                 false,
84 87
                 action: 'resolve from binding',
@@ -106,7 +109,9 @@  discard block
 block discarded – undo
106 109
                     ->resolveWeakReference($binding, $alias, $context, $parameters),
107 110
                 default => $binding,
108 111
             };
109
-        } finally {
112
+        }
113
+        finally
114
+        {
110 115
             $this->state->bindings[$alias] ??= $binding;
111 116
             $this->tracer->pop(true);
112 117
             $this->tracer->pop(false);
@@ -116,18 +121,23 @@  discard block
 block discarded – undo
116 121
     private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments)
117 122
     {
118 123
         $context = $ctx->context;
119
-        try {
124
+        try
125
+        {
120 126
             $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class);
121
-        } catch (\ReflectionException $e) {
127
+        }
128
+        catch (\ReflectionException $e)
129
+        {
122 130
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
123 131
         }
124 132
 
125 133
         $injector = $binding->injector;
126 134
 
127
-        try {
135
+        try
136
+        {
128 137
             $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector);
129 138
 
130
-            if (!$injectorInstance instanceof InjectorInterface) {
139
+            if (!$injectorInstance instanceof InjectorInterface)
140
+            {
131 141
                 throw new InjectionException(
132 142
                     \sprintf(
133 143
                         "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -155,7 +165,8 @@  discard block
 block discarded – undo
155 165
                 default => (string)$context,
156 166
             });
157 167
 
158
-            if (!$reflection->isInstance($instance)) {
168
+            if (!$reflection->isInstance($instance))
169
+            {
159 170
                 throw new InjectionException(
160 171
                     \sprintf(
161 172
                         "Invalid injection response for '%s'.",
@@ -165,7 +176,9 @@  discard block
 block discarded – undo
165 176
             }
166 177
 
167 178
             return $instance;
168
-        } finally {
179
+        }
180
+        finally
181
+        {
169 182
             $this->state->bindings[$reflection->getName()] ??= $binding;
170 183
         }
171 184
     }
@@ -184,7 +197,8 @@  discard block
 block discarded – undo
184 197
             //Binding is pointing to something else
185 198
             : $this->make($binding->alias, $arguments, $context);
186 199
 
187
-        if ($binding->singleton && $arguments === []) {
200
+        if ($binding->singleton && $arguments === [])
201
+        {
188 202
             $this->state->singletons[$alias] = $result;
189 203
         }
190 204
 
@@ -225,11 +239,14 @@  discard block
 block discarded – undo
225 239
         array $arguments,
226 240
     ): mixed {
227 241
         $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
228
-        try {
242
+        try
243
+        {
229 244
             $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0
230 245
                 ? ($binding->factory)()
231 246
                 : $this->invoker->invoke($binding->factory, $arguments);
232
-        } catch (NotCallableException $e) {
247
+        }
248
+        catch (NotCallableException $e)
249
+        {
233 250
             throw new ContainerException(
234 251
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)),
235 252
                 $e->getCode(),
@@ -248,19 +265,24 @@  discard block
 block discarded – undo
248 265
     ): ?object {
249 266
         $avoidCache = $arguments !== [];
250 267
 
251
-        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) {
252
-            try {
268
+        if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias))
269
+        {
270
+            try
271
+            {
253 272
                 $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context);
254 273
 
255 274
                 $object = $this->createInstance(
256 275
                     new Ctx(alias: $alias, class: $alias, context: $context),
257 276
                     $arguments,
258 277
                 );
259
-                if ($avoidCache) {
278
+                if ($avoidCache)
279
+                {
260 280
                     return $object;
261 281
                 }
262 282
                 $binding->reference = WeakReference::create($object);
263
-            } catch (\Throwable) {
283
+            }
284
+            catch (\Throwable)
285
+            {
264 286
                 throw new ContainerException(
265 287
                     $this->tracer->combineTraceMessage(
266 288
                         \sprintf(
@@ -270,7 +292,9 @@  discard block
 block discarded – undo
270 292
                         )
271 293
                     )
272 294
                 );
273
-            } finally {
295
+            }
296
+            finally
297
+            {
274 298
                 $this->tracer->pop();
275 299
             }
276 300
         }
@@ -285,18 +309,25 @@  discard block
 block discarded – undo
285 309
     ): mixed {
286 310
         $parent = $this->scope->getParent();
287 311
 
288
-        if ($parent !== null) {
289
-            try {
312
+        if ($parent !== null)
313
+        {
314
+            try
315
+            {
290 316
                 $this->tracer->push(false, ...[
291 317
                     'current scope' => $this->scope->getScopeName(),
292 318
                     'jump to parent scope' => $this->scope->getParentScope()->getScopeName(),
293 319
                 ]);
294 320
                 return $parent->make($alias, $parameters, $context);
295
-            } catch (BadScopeException $e) {
296
-                if ($this->scope->getScopeName() !== $e->getScope()) {
321
+            }
322
+            catch (BadScopeException $e)
323
+            {
324
+                if ($this->scope->getScopeName() !== $e->getScope())
325
+                {
297 326
                     throw $e;
298 327
                 }
299
-            } catch (ContainerExceptionInterface $e) {
328
+            }
329
+            catch (ContainerExceptionInterface $e)
330
+            {
300 331
                 $className = match (true) {
301 332
                     $e instanceof NotFoundException => NotFoundException::class,
302 333
                     default => ContainerException::class,
@@ -305,19 +336,24 @@  discard block
 block discarded – undo
305 336
                     'Can\'t resolve `%s`.',
306 337
                     $alias,
307 338
                 )), previous: $e);
308
-            } finally {
339
+            }
340
+            finally
341
+            {
309 342
                 $this->tracer->pop(false);
310 343
             }
311 344
         }
312 345
 
313 346
         $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context);
314
-        try {
347
+        try
348
+        {
315 349
             //No direct instructions how to construct class, make is automatically
316 350
             return $this->autowire(
317 351
                 new Ctx(alias: $alias, class: $alias, context: $context),
318 352
                 $parameters,
319 353
             );
320
-        } finally {
354
+        }
355
+        finally
356
+        {
321 357
             $this->tracer->pop(false);
322 358
         }
323 359
     }
@@ -367,7 +403,8 @@  discard block
 block discarded – undo
367 403
         // Check scope name
368 404
         $ctx->reflection = new \ReflectionClass($instance);
369 405
         $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name;
370
-        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) {
406
+        if ($scopeName !== null && $scopeName !== $this->scope->getScopeName())
407
+        {
371 408
             throw new BadScopeException($scopeName, $instance::class);
372 409
         }
373 410
 
@@ -394,24 +431,30 @@  discard block
 block discarded – undo
394 431
         array $parameters,
395 432
     ): object {
396 433
         $class = $ctx->class;
397
-        try {
434
+        try
435
+        {
398 436
             $ctx->reflection = $reflection = new \ReflectionClass($class);
399
-        } catch (\ReflectionException $e) {
437
+        }
438
+        catch (\ReflectionException $e)
439
+        {
400 440
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
401 441
         }
402 442
 
403 443
         // Check scope name
404 444
         $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name;
405
-        if ($scope !== null && $scope !== $this->scope->getScopeName()) {
445
+        if ($scope !== null && $scope !== $this->scope->getScopeName())
446
+        {
406 447
             throw new BadScopeException($scope, $class);
407 448
         }
408 449
 
409 450
         //We have to construct class using external injector when we know exact context
410
-        if ($this->binder->hasInjector($class)) {
451
+        if ($this->binder->hasInjector($class))
452
+        {
411 453
             return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $parameters);
412 454
         }
413 455
 
414
-        if (!$reflection->isInstantiable()) {
456
+        if (!$reflection->isInstantiable())
457
+        {
415 458
             $itIs = match (true) {
416 459
                 $reflection->isEnum() => 'Enum',
417 460
                 $reflection->isAbstract() => 'Abstract class',
@@ -424,12 +467,16 @@  discard block
 block discarded – undo
424 467
 
425 468
         $constructor = $reflection->getConstructor();
426 469
 
427
-        if ($constructor !== null) {
428
-            try {
470
+        if ($constructor !== null)
471
+        {
472
+            try
473
+            {
429 474
                 $this->tracer->push(false, action: 'resolve arguments', signature: $constructor);
430 475
                 $this->tracer->push(true);
431 476
                 $arguments = $this->resolver->resolveArguments($constructor, $parameters);
432
-            } catch (ValidationException $e) {
477
+            }
478
+            catch (ValidationException $e)
479
+            {
433 480
                 throw new ContainerException(
434 481
                     $this->tracer->combineTraceMessage(
435 482
                         \sprintf(
@@ -439,22 +486,31 @@  discard block
 block discarded – undo
439 486
                         )
440 487
                     ),
441 488
                 );
442
-            } finally {
489
+            }
490
+            finally
491
+            {
443 492
                 $this->tracer->pop(true);
444 493
                 $this->tracer->pop(false);
445 494
             }
446
-            try {
495
+            try
496
+            {
447 497
                 // Using constructor with resolved arguments
448 498
                 $this->tracer->push(false, call: "$class::__construct", arguments: $arguments);
449 499
                 $this->tracer->push(true);
450 500
                 $instance = new $class(...$arguments);
451
-            } catch (\TypeError $e) {
501
+            }
502
+            catch (\TypeError $e)
503
+            {
452 504
                 throw new WrongTypeException($constructor, $e);
453
-            } finally {
505
+            }
506
+            finally
507
+            {
454 508
                 $this->tracer->pop(true);
455 509
                 $this->tracer->pop(false);
456 510
             }
457
-        } else {
511
+        }
512
+        else
513
+        {
458 514
             // No constructor specified
459 515
             $instance = $reflection->newInstance();
460 516
         }
@@ -472,13 +528,15 @@  discard block
 block discarded – undo
472 528
         $instance = $this->runInflector($instance);
473 529
 
474 530
         //Declarative singletons
475
-        if ($this->isSingleton($ctx)) {
531
+        if ($this->isSingleton($ctx))
532
+        {
476 533
             $this->state->singletons[$ctx->alias] = $instance;
477 534
         }
478 535
 
479 536
         // Register finalizer
480 537
         $finalizer = $this->getFinalizer($ctx, $instance);
481
-        if ($finalizer !== null) {
538
+        if ($finalizer !== null)
539
+        {
482 540
             $this->state->finalizers[] = $finalizer;
483 541
         }
484 542
 
@@ -490,12 +548,14 @@  discard block
 block discarded – undo
490 548
      */
491 549
     private function isSingleton(Ctx $ctx): bool
492 550
     {
493
-        if ($ctx->singleton === true) {
551
+        if ($ctx->singleton === true)
552
+        {
494 553
             return true;
495 554
         }
496 555
 
497 556
         /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */
498
-        if ($ctx->reflection->implementsInterface(SingletonInterface::class)) {
557
+        if ($ctx->reflection->implementsInterface(SingletonInterface::class))
558
+        {
499 559
             return true;
500 560
         }
501 561
 
@@ -509,7 +569,8 @@  discard block
 block discarded – undo
509 569
          * @var Finalize|null $attribute
510 570
          */
511 571
         $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance();
512
-        if ($attribute === null) {
572
+        if ($attribute === null)
573
+        {
513 574
             return null;
514 575
         }
515 576
 
@@ -523,10 +584,14 @@  discard block
 block discarded – undo
523 584
     {
524 585
         $scope = $this->scope;
525 586
 
526
-        while ($scope !== null) {
527
-            foreach ($this->state->inflectors as $class => $inflectors) {
528
-                if ($instance instanceof $class) {
529
-                    foreach ($inflectors as $inflector) {
587
+        while ($scope !== null)
588
+        {
589
+            foreach ($this->state->inflectors as $class => $inflectors)
590
+            {
591
+                if ($instance instanceof $class)
592
+                {
593
+                    foreach ($inflectors as $inflector)
594
+                    {
530 595
                         $instance = $inflector->getParametersCount() > 1
531 596
                             ? $this->invoker->invoke($inflector->inflector, [$instance])
532 597
                             : ($inflector->inflector)($instance);
@@ -542,9 +607,12 @@  discard block
 block discarded – undo
542 607
 
543 608
     private function validateArguments(ContextFunction $reflection, array $arguments = []): bool
544 609
     {
545
-        try {
610
+        try
611
+        {
546 612
             $this->resolver->validateArguments($reflection, $arguments);
547
-        } catch (\Throwable) {
613
+        }
614
+        catch (\Throwable)
615
+        {
548 616
             return false;
549 617
         }
550 618
 
Please login to merge, or discard this patch.
src/Core/src/Internal/Resolver.php 1 patch
Braces   +68 added lines, -32 removed lines patch added patch discarded remove patch
@@ -52,7 +52,8 @@  discard block
 block discarded – undo
52 52
     ): array {
53 53
         $state = new ResolvingState($reflection, $parameters);
54 54
 
55
-        foreach ($reflection->getParameters() as $parameter) {
55
+        foreach ($reflection->getParameters() as $parameter)
56
+        {
56 57
             $this->resolveParameter($parameter, $state, $validate)
57 58
             or
58 59
             throw new ArgumentResolvingException($reflection, $parameter->getName());
@@ -66,47 +67,59 @@  discard block
 block discarded – undo
66 67
         $positional = true;
67 68
         $variadic = false;
68 69
         $parameters = $reflection->getParameters();
69
-        if (\count($parameters) === 0) {
70
+        if (\count($parameters) === 0)
71
+        {
70 72
             return;
71 73
         }
72 74
 
73 75
         $parameter = null;
74
-        while (\count($parameters) > 0 || \count($arguments) > 0) {
76
+        while (\count($parameters) > 0 || \count($arguments) > 0)
77
+        {
75 78
             // get related argument value
76 79
             $key = \key($arguments);
77 80
 
78 81
             // For a variadic parameter it's no sense - named or positional argument will be sent
79 82
             // But you can't send positional argument after named in any case
80
-            if (\is_int($key) && !$positional) {
83
+            if (\is_int($key) && !$positional)
84
+            {
81 85
                 throw new PositionalArgumentException($reflection, $key);
82 86
             }
83 87
 
84 88
             $positional = $positional && \is_int($key);
85 89
 
86
-            if (!$variadic) {
90
+            if (!$variadic)
91
+            {
87 92
                 $parameter = \array_shift($parameters);
88 93
                 $variadic = $parameter?->isVariadic() ?? false;
89 94
             }
90 95
 
91
-            if ($parameter === null) {
96
+            if ($parameter === null)
97
+            {
92 98
                 throw new UnknownParameterException($reflection, $key);
93 99
             }
94 100
             $name = $parameter->getName();
95 101
 
96
-            if (($positional || $variadic) && $key !== null) {
102
+            if (($positional || $variadic) && $key !== null)
103
+            {
97 104
                 /** @psalm-suppress ReferenceReusedFromConfusingScope */
98 105
                 $value = \array_shift($arguments);
99
-            } elseif ($key === null || !\array_key_exists($name, $arguments)) {
100
-                if ($parameter->isOptional()) {
106
+            }
107
+            elseif ($key === null || !\array_key_exists($name, $arguments))
108
+            {
109
+                if ($parameter->isOptional())
110
+                {
101 111
                     continue;
102 112
                 }
103 113
                 throw new MissingRequiredArgumentException($reflection, $name);
104
-            } else {
114
+            }
115
+            else
116
+            {
105 117
                 $value = &$arguments[$name];
106 118
                 unset($arguments[$name]);
107 119
             }
108 120
 
109
-            if (!$this->validateValueToParameter($parameter, $value)) {
121
+            if (!$this->validateValueToParameter($parameter, $value))
122
+            {
110 123
                 throw new InvalidArgumentException($reflection, $name);
111 124
             }
112 125
         }
@@ -114,7 +127,8 @@  discard block
 block discarded – undo
114 127
 
115 128
     private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool
116 129
     {
117
-        if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) {
130
+        if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null))
131
+        {
118 132
             return true;
119 133
         }
120 134
         $type = $parameter->getType();
@@ -125,17 +139,21 @@  discard block
 block discarded – undo
125 139
             $type instanceof ReflectionIntersectionType => [false, $type->getTypes()],
126 140
         };
127 141
 
128
-        foreach ($types as $t) {
142
+        foreach ($types as $t)
143
+        {
129 144
             \assert($t instanceof ReflectionNamedType);
130
-            if (!$this->validateValueNamedType($t, $value)) {
145
+            if (!$this->validateValueNamedType($t, $value))
146
+            {
131 147
                 // If it is TypeIntersection
132
-                if ($or) {
148
+                if ($or)
149
+                {
133 150
                     continue;
134 151
                 }
135 152
                 return false;
136 153
             }
137 154
             // If it is not type intersection then we can skip that value after first successful check
138
-            if ($or) {
155
+            if ($or)
156
+            {
139 157
                 return true;
140 158
             }
141 159
         }
@@ -150,7 +168,8 @@  discard block
 block discarded – undo
150 168
     {
151 169
         $name = $type->getName();
152 170
 
153
-        if ($type->isBuiltin()) {
171
+        if ($type->isBuiltin())
172
+        {
154 173
             return match ($name) {
155 174
                 'mixed' => true,
156 175
                 'string' => \is_string($value),
@@ -183,13 +202,18 @@  discard block
 block discarded – undo
183 202
 
184 203
         // Try to resolve parameter by name
185 204
         $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic);
186
-        if ($res !== [] || $isVariadic) {
205
+        if ($res !== [] || $isVariadic)
206
+        {
187 207
             // validate
188
-            if ($isVariadic) {
189
-                foreach ($res as $k => &$v) {
208
+            if ($isVariadic)
209
+            {
210
+                foreach ($res as $k => &$v)
211
+                {
190 212
                     $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k);
191 213
                 }
192
-            } else {
214
+            }
215
+            else
216
+            {
193 217
                 $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null);
194 218
             }
195 219
 
@@ -197,39 +221,49 @@  discard block
 block discarded – undo
197 221
         }
198 222
 
199 223
         $error = null;
200
-        if ($hasType) {
224
+        if ($hasType)
225
+        {
201 226
             /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */
202 227
             $reflectionType = $parameter->getType();
203 228
 
204
-            if ($reflectionType instanceof ReflectionIntersectionType) {
229
+            if ($reflectionType instanceof ReflectionIntersectionType)
230
+            {
205 231
                 throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName());
206 232
             }
207 233
 
208 234
             $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes();
209
-            foreach ($types as $namedType) {
210
-                try {
211
-                    if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) {
235
+            foreach ($types as $namedType)
236
+            {
237
+                try
238
+                {
239
+                    if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate))
240
+                    {
212 241
                         return true;
213 242
                     }
214
-                } catch (Throwable $e) {
243
+                }
244
+                catch (Throwable $e)
245
+                {
215 246
                     $error = $e;
216 247
                 }
217 248
             }
218 249
         }
219 250
 
220
-        if ($parameter->isDefaultValueAvailable()) {
251
+        if ($parameter->isDefaultValueAvailable())
252
+        {
221 253
             $argument = $parameter->getDefaultValue();
222 254
             $this->processArgument($state, $argument);
223 255
             return true;
224 256
         }
225 257
 
226
-        if ($hasType && $parameter->allowsNull()) {
258
+        if ($hasType && $parameter->allowsNull())
259
+        {
227 260
             $argument = null;
228 261
             $this->processArgument($state, $argument);
229 262
             return true;
230 263
         }
231 264
 
232
-        if ($error === null) {
265
+        if ($error === null)
266
+        {
233 267
             return false;
234 268
         }
235 269
 
@@ -270,12 +304,14 @@  discard block
 block discarded – undo
270 304
         int|string $key = null
271 305
     ): void {
272 306
         // Resolve Autowire objects
273
-        if ($value instanceof Autowire) {
307
+        if ($value instanceof Autowire)
308
+        {
274 309
             $value = $value->resolve($this->factory);
275 310
         }
276 311
 
277 312
         // Validation
278
-        if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) {
313
+        if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value))
314
+        {
279 315
             throw new InvalidArgumentException(
280 316
                 $validateWith->getDeclaringFunction(),
281 317
                 $validateWith->getName()
Please login to merge, or discard this patch.