Passed
Pull Request — master (#817)
by Maxim
07:12
created
src/Core/tests/Internal/Resolver/CommonCasesTest.php 1 patch
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -63,7 +63,9 @@  discard block
 block discarded – undo
63 63
         $this->expectExceptionMessage('Enum `Spiral\Tests\Core\Stub\EnumObject` can not be constructed.');
64 64
 
65 65
         $this->resolveClosure(
66
-            static function (EnumObject $enum) {},
66
+            static function (EnumObject $enum)
67
+            {
68
+},
67 69
         );
68 70
     }
69 71
 
@@ -75,7 +77,9 @@  discard block
 block discarded – undo
75 77
         );
76 78
 
77 79
         $this->resolveClosure(
78
-            static function (TestTrait $enum) {},
80
+            static function (TestTrait $enum)
81
+            {
82
+},
79 83
         );
80 84
     }
81 85
 }
Please login to merge, or discard this patch.
src/Core/src/Internal/Trace.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,8 @@
 block discarded – undo
24 24
     public function __toString(): string
25 25
     {
26 26
         $info = [$this->alias];
27
-        foreach ($this->info as $key => $item) {
27
+        foreach ($this->info as $key => $item)
28
+        {
28 29
             $info[] = "$key: {$this->stringifyValue($item)}";
29 30
         }
30 31
         return \implode("\n", $info);
Please login to merge, or discard this patch.
src/Core/src/Internal/Tracer.php 1 patch
Braces   +15 added lines, -7 removed lines patch added patch discarded remove patch
@@ -34,26 +34,32 @@  discard block
 block discarded – undo
34 34
     public function push(string $alias, bool $nextLevel = false, mixed ...$details): void
35 35
     {
36 36
         $trace = new Trace($alias, $details);
37
-        if ($nextLevel || $this->traces === []) {
37
+        if ($nextLevel || $this->traces === [])
38
+        {
38 39
             $this->traces[] = [$trace];
39
-        } else {
40
+        }
41
+        else
42
+        {
40 43
             $this->traces[\array_key_last($this->traces)][] = $trace;
41 44
         }
42 45
     }
43 46
 
44 47
     public function pop(bool $previousLevel = false): void
45 48
     {
46
-        if ($this->traces === []) {
49
+        if ($this->traces === [])
50
+        {
47 51
             return;
48 52
         }
49
-        if ($previousLevel) {
53
+        if ($previousLevel)
54
+        {
50 55
             \array_pop($this->traces);
51 56
             return;
52 57
         }
53 58
         $key = \array_key_last($this->traces);
54 59
         $list = &$this->traces[$key];
55 60
         \array_pop($list);
56
-        if ($list === []) {
61
+        if ($list === [])
62
+        {
57 63
             unset($this->traces[$key]);
58 64
         }
59 65
     }
@@ -70,7 +76,8 @@  discard block
 block discarded – undo
70 76
     {
71 77
         $result = [];
72 78
         $i = 0;
73
-        foreach ($blocks as $block) {
79
+        foreach ($blocks as $block)
80
+        {
74 81
             \array_push($result, ...$this->blockToStringList($block, $i++));
75 82
         }
76 83
         return \implode("\n", $result);
@@ -90,7 +97,8 @@  discard block
 block discarded – undo
90 97
         // Separator
91 98
         $s = "\n";
92 99
         $nexPrefix = "$s$padding  ";
93
-        foreach ($items as $item) {
100
+        foreach ($items as $item)
101
+        {
94 102
             $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item);
95 103
         }
96 104
         return $result;
Please login to merge, or discard this patch.
src/Core/tests/ExceptionsTest.php 1 patch
Braces   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -143,9 +143,12 @@
 block discarded – undo
143 143
     {
144 144
         $container = new Container();
145 145
 
146
-        try {
146
+        try
147
+        {
147 148
             $container->get('invalid');
148
-        } catch (ContainerException $e) {
149
+        }
150
+        catch (ContainerException $e)
151
+        {
149 152
             $this->assertSame(
150 153
                 $e->getMessage(),
151 154
                 <<<MARKDOWN
Please login to merge, or discard this patch.
src/Core/src/Internal/Factory.php 1 patch
Braces   +86 added lines, -37 removed lines patch added patch discarded remove patch
@@ -54,34 +54,47 @@  discard block
 block discarded – undo
54 54
      */
55 55
     public function make(string $alias, array $parameters = [], string $context = null): mixed
56 56
     {
57
-        if (!isset($this->state->bindings[$alias])) {
57
+        if (!isset($this->state->bindings[$alias]))
58
+        {
58 59
             $this->tracer->push($alias, false, source: 'autowiring', context: $context);
59
-            try {
60
+            try
61
+            {
60 62
                 //No direct instructions how to construct class, make is automatically
61 63
                 return $this->autowire($alias, $parameters, $context);
62
-            } finally {
64
+            }
65
+            finally
66
+            {
63 67
                 $this->tracer->pop(false);
64 68
             }
65 69
         }
66 70
 
67 71
         $binding = $this->state->bindings[$alias];
68
-        try {
72
+        try
73
+        {
69 74
             $this->tracer->push($alias, false, source: 'binding', binding: $binding, context: $context);
70 75
 
71
-            if (\is_object($binding)) {
72
-                if ($binding::class === WeakReference::class) {
73
-                    if ($binding->get() === null && \class_exists($alias)) {
74
-                        try {
76
+            if (\is_object($binding))
77
+            {
78
+                if ($binding::class === WeakReference::class)
79
+                {
80
+                    if ($binding->get() === null && \class_exists($alias))
81
+                    {
82
+                        try
83
+                        {
75 84
                             $this->tracer->push($alias, false, source: WeakReference::class, context: $context);
76 85
                             $object = $this->createInstance($alias, $parameters, $context);
77 86
                             $binding = $this->state->bindings[$alias] = WeakReference::create($object);
78
-                        } catch (\Throwable) {
87
+                        }
88
+                        catch (\Throwable)
89
+                        {
79 90
                             throw new ContainerException($this->tracer->combineTraceMessage(\sprintf(
80 91
                                 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.',
81 92
                                 $this->tracer->getRootAlias(),
82 93
                                 $alias,
83 94
                             )));
84
-                        } finally {
95
+                        }
96
+                        finally
97
+                        {
85 98
                             $this->tracer->pop();
86 99
                         }
87 100
                     }
@@ -91,25 +104,32 @@  discard block
 block discarded – undo
91 104
                 return $binding;
92 105
             }
93 106
 
94
-            if (\is_string($binding)) {
107
+            if (\is_string($binding))
108
+            {
95 109
                 //Binding is pointing to something else
96 110
                 return $this->make($binding, $parameters, $context);
97 111
             }
98 112
 
99 113
             unset($this->state->bindings[$alias]);
100
-            try {
114
+            try
115
+            {
101 116
                 $instance = $binding[0] === $alias
102 117
                     ? $this->autowire($alias, $parameters, $context)
103 118
                     : $this->evaluateBinding($alias, $binding[0], $parameters, $context);
104
-            } finally {
119
+            }
120
+            finally
121
+            {
105 122
                 /** @psalm-var class-string $alias */
106 123
                 $this->state->bindings[$alias] = $binding;
107 124
             }
108
-        } finally {
125
+        }
126
+        finally
127
+        {
109 128
             $this->tracer->pop(false);
110 129
         }
111 130
 
112
-        if ($binding[1]) {
131
+        if ($binding[1])
132
+        {
113 133
             // Indicates singleton
114 134
             /** @psalm-var class-string $alias */
115 135
             $this->state->bindings[$alias] = $instance;
@@ -128,7 +148,8 @@  discard block
 block discarded – undo
128 148
      */
129 149
     private function autowire(string $class, array $parameters, string $context = null): object
130 150
     {
131
-        if (!\class_exists($class) && !isset($this->state->injectors[$class])) {
151
+        if (!\class_exists($class) && !isset($this->state->injectors[$class]))
152
+        {
132 153
             throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf(
133 154
                 'Can\'t resolve `%s`: undefined class or binding `%s`.',
134 155
                 $this->tracer->getRootAlias(),
@@ -155,18 +176,23 @@  discard block
 block discarded – undo
155 176
         array $parameters,
156 177
         string $context = null
157 178
     ): mixed {
158
-        if (\is_string($target)) {
179
+        if (\is_string($target))
180
+        {
159 181
             // Reference
160 182
             return $this->make($target, $parameters, $context);
161 183
         }
162 184
 
163
-        if ($target instanceof Autowire) {
185
+        if ($target instanceof Autowire)
186
+        {
164 187
             return $target->resolve($this, $parameters);
165 188
         }
166 189
 
167
-        try {
190
+        try
191
+        {
168 192
             return $this->invoker->invoke($target, $parameters);
169
-        } catch (NotCallableException $e) {
193
+        }
194
+        catch (NotCallableException $e)
195
+        {
170 196
             throw new ContainerException(
171 197
                 $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)),
172 198
                 $e->getCode(),
@@ -190,20 +216,26 @@  discard block
 block discarded – undo
190 216
      */
191 217
     private function createInstance(string $class, array $parameters, string $context = null): object
192 218
     {
193
-        try {
219
+        try
220
+        {
194 221
             $reflection = new \ReflectionClass($class);
195
-        } catch (\ReflectionException $e) {
222
+        }
223
+        catch (\ReflectionException $e)
224
+        {
196 225
             throw new ContainerException($e->getMessage(), $e->getCode(), $e);
197 226
         }
198 227
 
199 228
         //We have to construct class using external injector when we know exact context
200
-        if ($parameters === [] && $this->binder->hasInjector($class)) {
229
+        if ($parameters === [] && $this->binder->hasInjector($class))
230
+        {
201 231
             $injector = $this->state->injectors[$reflection->getName()];
202 232
 
203
-            try {
233
+            try
234
+            {
204 235
                 $injectorInstance = $this->container->get($injector);
205 236
 
206
-                if (!$injectorInstance instanceof InjectorInterface) {
237
+                if (!$injectorInstance instanceof InjectorInterface)
238
+                {
207 239
                     throw new InjectionException(
208 240
                         \sprintf(
209 241
                             "Class '%s' must be an instance of InjectorInterface for '%s'.",
@@ -215,7 +247,8 @@  discard block
 block discarded – undo
215 247
 
216 248
                 /** @var InjectorInterface<TObject> $injectorInstance */
217 249
                 $instance = $injectorInstance->createInjection($reflection, $context);
218
-                if (!$reflection->isInstance($instance)) {
250
+                if (!$reflection->isInstance($instance))
251
+                {
219 252
                     throw new InjectionException(
220 253
                         \sprintf(
221 254
                             "Invalid injection response for '%s'.",
@@ -225,12 +258,15 @@  discard block
 block discarded – undo
225 258
                 }
226 259
 
227 260
                 return $instance;
228
-            } finally {
261
+            }
262
+            finally
263
+            {
229 264
                 $this->state->injectors[$reflection->getName()] = $injector;
230 265
             }
231 266
         }
232 267
 
233
-        if (!$reflection->isInstantiable()) {
268
+        if (!$reflection->isInstantiable())
269
+        {
234 270
             $itIs = match (true) {
235 271
                 $reflection->isEnum() => 'Enum',
236 272
                 $reflection->isAbstract() => 'Abstract class',
@@ -243,10 +279,14 @@  discard block
 block discarded – undo
243 279
 
244 280
         $constructor = $reflection->getConstructor();
245 281
 
246
-        if ($constructor !== null) {
247
-            try {
282
+        if ($constructor !== null)
283
+        {
284
+            try
285
+            {
248 286
                 $arguments = $this->resolver->resolveArguments($constructor, $parameters);
249
-            } catch (ValidationException $e) {
287
+            }
288
+            catch (ValidationException $e)
289
+            {
250 290
                 throw new ContainerException(
251 291
                     $this->tracer->combineTraceMessage(
252 292
                         \sprintf(
@@ -257,16 +297,23 @@  discard block
 block discarded – undo
257 297
                     ),
258 298
                 );
259 299
             }
260
-            try {
300
+            try
301
+            {
261 302
                 // Using constructor with resolved arguments
262 303
                 $this->tracer->push($class, true, ...['constructor', ...$arguments]);
263 304
                 $instance = new $class(...$arguments);
264
-            } catch (\TypeError $e) {
305
+            }
306
+            catch (\TypeError $e)
307
+            {
265 308
                 throw new WrongTypeException($constructor, $e);
266
-            } finally {
309
+            }
310
+            finally
311
+            {
267 312
                 $this->tracer->pop(true);
268 313
             }
269
-        } else {
314
+        }
315
+        else
316
+        {
270 317
             // No constructor specified
271 318
             $instance = $reflection->newInstance();
272 319
         }
@@ -284,9 +331,11 @@  discard block
 block discarded – undo
284 331
     private function registerInstance(object $instance, array $parameters): object
285 332
     {
286 333
         //Declarative singletons (only when class received via direct get)
287
-        if ($parameters === [] && $instance instanceof SingletonInterface) {
334
+        if ($parameters === [] && $instance instanceof SingletonInterface)
335
+        {
288 336
             $alias = $instance::class;
289
-            if (!isset($this->state->bindings[$alias])) {
337
+            if (!isset($this->state->bindings[$alias]))
338
+            {
290 339
                 $this->state->bindings[$alias] = $instance;
291 340
             }
292 341
         }
Please login to merge, or discard this patch.