Passed
Pull Request — master (#1199)
by butschster
10:48
created
src/Core/src/Container.php 2 patches
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -48,11 +48,11 @@  discard block
 block discarded – undo
48 48
     public const DEFAULT_ROOT_SCOPE_NAME = 'root';
49 49
 
50 50
     private Internal\State $state;
51
-    private ResolverInterface|Internal\Resolver $resolver;
52
-    private FactoryInterface|Internal\Factory $factory;
53
-    private ContainerInterface|Internal\Container $container;
54
-    private BinderInterface|Internal\Binder $binder;
55
-    private InvokerInterface|Internal\Invoker $invoker;
51
+    private ResolverInterface | Internal\Resolver $resolver;
52
+    private FactoryInterface | Internal\Factory $factory;
53
+    private ContainerInterface | Internal\Container $container;
54
+    private BinderInterface | Internal\Binder $binder;
55
+    private InvokerInterface | Internal\Invoker $invoker;
56 56
     private Internal\Scope $scope;
57 57
 
58 58
     /**
@@ -60,11 +60,11 @@  discard block
 block discarded – undo
60 60
      */
61 61
     public function __construct(
62 62
         private Config $config = new Config(),
63
-        string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME,
63
+        string | \BackedEnum | null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME,
64 64
         private Options $options = new Options(),
65
-    ) {
66
-        if (\is_object($scopeName)) {
67
-            $scopeName = (string) $scopeName->value;
65
+    ){
66
+        if (\is_object($scopeName)){
67
+            $scopeName = (string)$scopeName->value;
68 68
         }
69 69
 
70 70
         $this->initServices($this, $scopeName);
@@ -118,11 +118,11 @@  discard block
 block discarded – undo
118 118
      * @throws \Throwable
119 119
      * @psalm-suppress TooManyArguments
120 120
      */
121
-    public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed
121
+    public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed
122 122
     {
123 123
         return ContainerScope::getContainer() === $this
124 124
             ? $this->factory->make($alias, $parameters, $context)
125
-            : ContainerScope::runScope($this, fn (): mixed => $this->factory->make($alias, $parameters, $context));
125
+            : ContainerScope::runScope($this, fn () : mixed => $this->factory->make($alias, $parameters, $context));
126 126
     }
127 127
 
128 128
     /**
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
      * @throws \Throwable
145 145
      * @psalm-suppress TooManyArguments
146 146
      */
147
-    public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed
147
+    public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed
148 148
     {
149 149
         return ContainerScope::getContainer() === $this
150 150
             ? $this->container->get($id, $context)
@@ -164,9 +164,9 @@  discard block
 block discarded – undo
164 164
      *        If {@see string}, the default binder for the given scope will be returned. Default bindings won't affect
165 165
      *        already created Container instances except the case with the root one.
166 166
      */
167
-    public function getBinder(string|\BackedEnum|null $scope = null): BinderInterface
167
+    public function getBinder(string | \BackedEnum | null $scope = null): BinderInterface
168 168
     {
169
-        $scope = \is_object($scope) ? (string) $scope->value : $scope;
169
+        $scope = \is_object($scope) ? (string)$scope->value : $scope;
170 170
 
171 171
         return $scope === null
172 172
             ? $this->binder
@@ -176,25 +176,25 @@  discard block
 block discarded – undo
176 176
     /**
177 177
      * @throws \Throwable
178 178
      */
179
-    public function runScope(Scope|array $bindings, callable $scope): mixed
179
+    public function runScope(Scope | array $bindings, callable $scope): mixed
180 180
     {
181
-        if (!\is_array($bindings)) {
181
+        if (!\is_array($bindings)){
182 182
             return $this->runIsolatedScope($bindings, $scope);
183 183
         }
184 184
 
185 185
         $binds = &$this->state->bindings;
186 186
         $singletons = &$this->state->singletons;
187 187
         $cleanup = $previous = $prevSin = [];
188
-        foreach ($bindings as $alias => $resolver) {
188
+        foreach ($bindings as $alias => $resolver){
189 189
             // Store previous bindings
190
-            if (isset($binds[$alias])) {
190
+            if (isset($binds[$alias])){
191 191
                 $previous[$alias] = $binds[$alias];
192
-            } else {
192
+            }else{
193 193
                 // Store bindings to be removed
194 194
                 $cleanup[] = $alias;
195 195
             }
196 196
             // Store previous singletons
197
-            if (isset($singletons[$alias])) {
197
+            if (isset($singletons[$alias])){
198 198
                 $prevSin[$alias] = $singletons[$alias];
199 199
                 unset($singletons[$alias]);
200 200
             }
@@ -202,21 +202,21 @@  discard block
 block discarded – undo
202 202
             $this->binder->bind($alias, $resolver);
203 203
         }
204 204
 
205
-        try {
205
+        try{
206 206
             return ContainerScope::getContainer() !== $this
207 207
                 ? ContainerScope::runScope($this, $scope)
208 208
                 : $scope($this);
209
-        } finally {
209
+        }finally{
210 210
             // Remove new bindings
211
-            foreach ($cleanup as $alias) {
211
+            foreach ($cleanup as $alias){
212 212
                 unset($binds[$alias], $singletons[$alias]);
213 213
             }
214 214
             // Restore previous bindings
215
-            foreach ($previous as $alias => $resolver) {
215
+            foreach ($previous as $alias => $resolver){
216 216
                 $binds[$alias] = $resolver;
217 217
             }
218 218
             // Restore singletons
219
-            foreach ($prevSin as $alias => $instance) {
219
+            foreach ($prevSin as $alias => $instance){
220 220
                 $singletons[$alias] = $instance;
221 221
             }
222 222
         }
@@ -264,9 +264,9 @@  discard block
 block discarded – undo
264 264
      * @throws SingletonOverloadException
265 265
      * @throws \Throwable
266 266
      */
267
-    public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void
267
+    public function bindSingleton(string $alias, string | array | callable | object $resolver, ?bool $force = null): void
268 268
     {
269
-        if ($force ?? $this->options->allowSingletonsRebind) {
269
+        if ($force ?? $this->options->allowSingletonsRebind){
270 270
             $this->binder->removeBinding($alias);
271 271
         }
272 272
 
@@ -293,7 +293,7 @@  discard block
 block discarded – undo
293 293
     {
294 294
         return ContainerScope::getContainer() === $this
295 295
             ? $this->invoker->invoke($target, $parameters)
296
-            : ContainerScope::runScope($this, fn (): mixed => $this->invoker->invoke($target, $parameters));
296
+            : ContainerScope::runScope($this, fn () : mixed => $this->invoker->invoke($target, $parameters));
297 297
     }
298 298
 
299 299
     /**
@@ -337,8 +337,8 @@  discard block
 block discarded – undo
337 337
         ], $this->options);
338 338
 
339 339
         // Create container services
340
-        foreach ($container->config as $property => $class) {
341
-            if (\property_exists($container, $property)) {
340
+        foreach ($container->config as $property => $class){
341
+            if (\property_exists($container, $property)){
342 342
                 $container->$property = $constructor->get($property, $class);
343 343
             }
344 344
         }
@@ -352,7 +352,7 @@  discard block
 block discarded – undo
352 352
     private function closeScope(): void
353 353
     {
354 354
         /** @psalm-suppress RedundantPropertyInitializationCheck */
355
-        if (!isset($this->scope)) {
355
+        if (!isset($this->scope)){
356 356
             $this->destruct();
357 357
             return;
358 358
         }
@@ -361,10 +361,10 @@  discard block
 block discarded – undo
361 361
 
362 362
         // Run finalizers
363 363
         $errors = [];
364
-        foreach ($this->state->finalizers as $finalizer) {
365
-            try {
364
+        foreach ($this->state->finalizers as $finalizer){
365
+            try{
366 366
                 $this->invoker->invoke($finalizer);
367
-            } catch (\Throwable $e) {
367
+            }catch (\Throwable $e){
368 368
                 $errors[] = $e;
369 369
             }
370 370
         }
@@ -373,7 +373,7 @@  discard block
 block discarded – undo
373 373
         $this->destruct();
374 374
 
375 375
         // Throw collected errors
376
-        if ($errors !== []) {
376
+        if ($errors !== []){
377 377
             throw new FinalizersException($scopeName, $errors);
378 378
         }
379 379
     }
@@ -395,18 +395,18 @@  discard block
 block discarded – undo
395 395
         $container->scope->setParent($this, $this->scope, $this->factory);
396 396
 
397 397
         // Add specific bindings
398
-        foreach ($config->bindings as $alias => $resolver) {
398
+        foreach ($config->bindings as $alias => $resolver){
399 399
             $container->binder->bind($alias, $resolver);
400 400
         }
401 401
 
402 402
         return ContainerScope::runScope(
403 403
             $container,
404 404
             static function (self $container) use ($config, $closure): mixed {
405
-                try {
405
+                try{
406 406
                     return $config->autowire
407 407
                         ? $container->invoke($closure)
408 408
                         : $closure($container);
409
-                } finally {
409
+                }finally{
410 410
                     $container->closeScope();
411 411
                 }
412 412
             }
Please login to merge, or discard this patch.
Braces   +48 added lines, -22 removed lines patch added patch discarded remove patch
@@ -63,7 +63,8 @@  discard block
 block discarded – undo
63 63
         string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME,
64 64
         private Options $options = new Options(),
65 65
     ) {
66
-        if (\is_object($scopeName)) {
66
+        if (\is_object($scopeName))
67
+        {
67 68
             $scopeName = (string) $scopeName->value;
68 69
         }
69 70
 
@@ -178,23 +179,29 @@  discard block
 block discarded – undo
178 179
      */
179 180
     public function runScope(Scope|array $bindings, callable $scope): mixed
180 181
     {
181
-        if (!\is_array($bindings)) {
182
+        if (!\is_array($bindings))
183
+        {
182 184
             return $this->runIsolatedScope($bindings, $scope);
183 185
         }
184 186
 
185 187
         $binds = &$this->state->bindings;
186 188
         $singletons = &$this->state->singletons;
187 189
         $cleanup = $previous = $prevSin = [];
188
-        foreach ($bindings as $alias => $resolver) {
190
+        foreach ($bindings as $alias => $resolver)
191
+        {
189 192
             // Store previous bindings
190
-            if (isset($binds[$alias])) {
193
+            if (isset($binds[$alias]))
194
+            {
191 195
                 $previous[$alias] = $binds[$alias];
192
-            } else {
196
+            }
197
+            else
198
+            {
193 199
                 // Store bindings to be removed
194 200
                 $cleanup[] = $alias;
195 201
             }
196 202
             // Store previous singletons
197
-            if (isset($singletons[$alias])) {
203
+            if (isset($singletons[$alias]))
204
+            {
198 205
                 $prevSin[$alias] = $singletons[$alias];
199 206
                 unset($singletons[$alias]);
200 207
             }
@@ -202,21 +209,27 @@  discard block
 block discarded – undo
202 209
             $this->binder->bind($alias, $resolver);
203 210
         }
204 211
 
205
-        try {
212
+        try
213
+        {
206 214
             return ContainerScope::getContainer() !== $this
207 215
                 ? ContainerScope::runScope($this, $scope)
208 216
                 : $scope($this);
209
-        } finally {
217
+        }
218
+        finally
219
+        {
210 220
             // Remove new bindings
211
-            foreach ($cleanup as $alias) {
221
+            foreach ($cleanup as $alias)
222
+            {
212 223
                 unset($binds[$alias], $singletons[$alias]);
213 224
             }
214 225
             // Restore previous bindings
215
-            foreach ($previous as $alias => $resolver) {
226
+            foreach ($previous as $alias => $resolver)
227
+            {
216 228
                 $binds[$alias] = $resolver;
217 229
             }
218 230
             // Restore singletons
219
-            foreach ($prevSin as $alias => $instance) {
231
+            foreach ($prevSin as $alias => $instance)
232
+            {
220 233
                 $singletons[$alias] = $instance;
221 234
             }
222 235
         }
@@ -266,7 +279,8 @@  discard block
 block discarded – undo
266 279
      */
267 280
     public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void
268 281
     {
269
-        if ($force ?? $this->options->allowSingletonsRebind) {
282
+        if ($force ?? $this->options->allowSingletonsRebind)
283
+        {
270 284
             $this->binder->removeBinding($alias);
271 285
         }
272 286
 
@@ -337,8 +351,10 @@  discard block
 block discarded – undo
337 351
         ], $this->options);
338 352
 
339 353
         // Create container services
340
-        foreach ($container->config as $property => $class) {
341
-            if (\property_exists($container, $property)) {
354
+        foreach ($container->config as $property => $class)
355
+        {
356
+            if (\property_exists($container, $property))
357
+            {
342 358
                 $container->$property = $constructor->get($property, $class);
343 359
             }
344 360
         }
@@ -352,7 +368,8 @@  discard block
 block discarded – undo
352 368
     private function closeScope(): void
353 369
     {
354 370
         /** @psalm-suppress RedundantPropertyInitializationCheck */
355
-        if (!isset($this->scope)) {
371
+        if (!isset($this->scope))
372
+        {
356 373
             $this->destruct();
357 374
             return;
358 375
         }
@@ -361,10 +378,14 @@  discard block
 block discarded – undo
361 378
 
362 379
         // Run finalizers
363 380
         $errors = [];
364
-        foreach ($this->state->finalizers as $finalizer) {
365
-            try {
381
+        foreach ($this->state->finalizers as $finalizer)
382
+        {
383
+            try
384
+            {
366 385
                 $this->invoker->invoke($finalizer);
367
-            } catch (\Throwable $e) {
386
+            }
387
+            catch (\Throwable $e)
388
+            {
368 389
                 $errors[] = $e;
369 390
             }
370 391
         }
@@ -373,7 +394,8 @@  discard block
 block discarded – undo
373 394
         $this->destruct();
374 395
 
375 396
         // Throw collected errors
376
-        if ($errors !== []) {
397
+        if ($errors !== [])
398
+        {
377 399
             throw new FinalizersException($scopeName, $errors);
378 400
         }
379 401
     }
@@ -395,18 +417,22 @@  discard block
 block discarded – undo
395 417
         $container->scope->setParent($this, $this->scope, $this->factory);
396 418
 
397 419
         // Add specific bindings
398
-        foreach ($config->bindings as $alias => $resolver) {
420
+        foreach ($config->bindings as $alias => $resolver)
421
+        {
399 422
             $container->binder->bind($alias, $resolver);
400 423
         }
401 424
 
402 425
         return ContainerScope::runScope(
403 426
             $container,
404 427
             static function (self $container) use ($config, $closure): mixed {
405
-                try {
428
+                try
429
+                {
406 430
                     return $config->autowire
407 431
                         ? $container->invoke($closure)
408 432
                         : $closure($container);
409
-                } finally {
433
+                }
434
+                finally
435
+                {
410 436
                     $container->closeScope();
411 437
                 }
412 438
             }
Please login to merge, or discard this patch.