Passed
Pull Request — master (#831)
by Maxim
11:02 queued 04:50
created
src/Router/src/UriHandler.php 1 patch
Braces   +46 added lines, -22 removed lines patch added patch discarded remove patch
@@ -90,7 +90,8 @@  discard block
 block discarded – undo
90 90
 
91 91
     public function withBasePath(string $basePath): self
92 92
     {
93
-        if (!\str_ends_with($basePath, '/')) {
93
+        if (!\str_ends_with($basePath, '/'))
94
+        {
94 95
             $basePath .= '/';
95 96
         }
96 97
 
@@ -127,12 +128,14 @@  discard block
 block discarded – undo
127 128
      */
128 129
     public function match(UriInterface $uri, array $defaults): ?array
129 130
     {
130
-        if (!$this->isCompiled()) {
131
+        if (!$this->isCompiled())
132
+        {
131 133
             $this->compile();
132 134
         }
133 135
 
134 136
         $matches = [];
135
-        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) {
137
+        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches))
138
+        {
136 139
             return null;
137 140
         }
138 141
 
@@ -149,7 +152,8 @@  discard block
 block discarded – undo
149 152
      */
150 153
     public function uri(iterable $parameters = [], array $defaults = []): UriInterface
151 154
     {
152
-        if (!$this->isCompiled()) {
155
+        if (!$this->isCompiled())
156
+        {
153 157
             $this->compile();
154 158
         }
155 159
 
@@ -159,8 +163,10 @@  discard block
 block discarded – undo
159 163
             $this->fetchOptions($parameters, $query)
160 164
         );
161 165
 
162
-        foreach ($this->constrains as $key => $_) {
163
-            if (empty($parameters[$key])) {
166
+        foreach ($this->constrains as $key => $_)
167
+        {
168
+            if (empty($parameters[$key]))
169
+            {
164 170
                 throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key));
165 171
             }
166 172
         }
@@ -184,18 +190,23 @@  discard block
 block discarded – undo
184 190
         $allowed = \array_keys($this->options);
185 191
 
186 192
         $result = [];
187
-        foreach ($parameters as $key => $parameter) {
188
-            if (\is_int($key) && isset($allowed[$key])) {
193
+        foreach ($parameters as $key => $parameter)
194
+        {
195
+            if (\is_int($key) && isset($allowed[$key]))
196
+            {
189 197
                 // this segment fetched keys from given parameters either by name or by position
190 198
                 $key = $allowed[$key];
191
-            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) {
199
+            }
200
+            elseif (!\array_key_exists($key, $this->options) && \is_array($parameters))
201
+            {
192 202
                 // all additional parameters given in array form can be glued to query string
193 203
                 $query[$key] = $parameter;
194 204
                 continue;
195 205
             }
196 206
 
197 207
             //String must be normalized here
198
-            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
208
+            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter))
209
+            {
199 210
                 $result[$key] = $this->slugify->slugify($parameter);
200 211
                 continue;
201 212
             }
@@ -213,15 +224,20 @@  discard block
 block discarded – undo
213 224
     {
214 225
         $path = $uri->getPath();
215 226
 
216
-        if (empty($path) || $path[0] !== '/') {
227
+        if (empty($path) || $path[0] !== '/')
228
+        {
217 229
             $path = '/' . $path;
218 230
         }
219 231
 
220
-        if ($this->matchHost) {
232
+        if ($this->matchHost)
233
+        {
221 234
             $uriString = $uri->getHost() . $path;
222
-        } else {
235
+        }
236
+        else
237
+        {
223 238
             $uriString = \substr($path, \strlen($this->basePath));
224
-            if ($uriString === false) {
239
+            if ($uriString === false)
240
+            {
225 241
                 $uriString = '';
226 242
             }
227 243
         }
@@ -234,7 +250,8 @@  discard block
 block discarded – undo
234 250
      */
235 251
     private function compile(): void
236 252
     {
237
-        if ($this->pattern === null) {
253
+        if ($this->pattern === null)
254
+        {
238 255
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
239 256
         }
240 257
 
@@ -243,14 +260,17 @@  discard block
 block discarded – undo
243 260
         $pattern = \rtrim(\ltrim($this->getPrefix() . '/' . $this->pattern, ':/'), '/');
244 261
 
245 262
         // correct [/ first occurrence]
246
-        if (\str_starts_with($pattern, '[/')) {
263
+        if (\str_starts_with($pattern, '[/'))
264
+        {
247 265
             $pattern = '[' . \substr($pattern, 2);
248 266
         }
249 267
 
250
-        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
268
+        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches))
269
+        {
251 270
             $variables = \array_combine($matches[1], $matches[2]);
252 271
 
253
-            foreach ($variables as $key => $segment) {
272
+            foreach ($variables as $key => $segment)
273
+            {
254 274
                 $segment = $this->prepareSegment($key, $segment);
255 275
                 $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment);
256 276
                 $options[] = $key;
@@ -260,13 +280,16 @@  discard block
 block discarded – undo
260 280
         $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern);
261 281
         $options = \array_fill_keys($options, null);
262 282
 
263
-        foreach ($this->constrains as $key => $value) {
264
-            if ($value instanceof Autofill) {
283
+        foreach ($this->constrains as $key => $value)
284
+        {
285
+            if ($value instanceof Autofill)
286
+            {
265 287
                 // only forces value replacement, not required to be presented as parameter
266 288
                 continue;
267 289
             }
268 290
 
269
-            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) {
291
+            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key]))
292
+            {
270 293
                 throw new ConstrainException(
271 294
                     \sprintf(
272 295
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -288,7 +311,8 @@  discard block
 block discarded – undo
288 311
     private function interpolate(string $string, array $values): string
289 312
     {
290 313
         $replaces = [];
291
-        foreach ($values as $key => $value) {
314
+        foreach ($values as $key => $value)
315
+        {
292 316
             $replaces[\sprintf('<%s>', $key)] = match (true) {
293 317
                 $value instanceof \Stringable || \is_scalar($value) => (string) $value,
294 318
                 default => '',
Please login to merge, or discard this patch.
src/Router/src/Router.php 1 patch
Braces   +51 added lines, -23 removed lines patch added patch discarded remove patch
@@ -63,14 +63,19 @@  discard block
 block discarded – undo
63 63
 
64 64
         return $this->tracer->trace(
65 65
             name: 'Routing',
66
-            callback: function (SpanInterface $span) use ($request) {
67
-                try {
66
+            callback: function (SpanInterface $span) use ($request)
67
+            {
68
+                try
69
+                {
68 70
                     $route = $this->matchRoute($request, $routeName);
69
-                } catch (RouteException $e) {
71
+                }
72
+                catch (RouteException $e)
73
+                {
70 74
                     throw new RouterException('Invalid route definition', $e->getCode(), $e);
71 75
                 }
72 76
 
73
-                if ($route === null) {
77
+                if ($route === null)
78
+                {
74 79
                     $this->eventDispatcher?->dispatch(new RouteNotFound($request));
75 80
                     throw new RouteNotFoundException($request->getUri());
76 81
                 }
@@ -105,7 +110,8 @@  discard block
 block discarded – undo
105 110
 
106 111
     public function getRoute(string $name): RouteInterface
107 112
     {
108
-        if (isset($this->routes[$name])) {
113
+        if (isset($this->routes[$name]))
114
+        {
109 115
             return $this->routes[$name];
110 116
         }
111 117
 
@@ -114,7 +120,8 @@  discard block
 block discarded – undo
114 120
 
115 121
     public function getRoutes(): array
116 122
     {
117
-        if (!empty($this->default)) {
123
+        if (!empty($this->default))
124
+        {
118 125
             return $this->routes + [null => $this->default];
119 126
         }
120 127
 
@@ -123,9 +130,12 @@  discard block
 block discarded – undo
123 130
 
124 131
     public function uri(string $route, iterable $parameters = []): UriInterface
125 132
     {
126
-        try {
133
+        try
134
+        {
127 135
             return $this->getRoute($route)->uri($parameters);
128
-        } catch (UndefinedRouteException) {
136
+        }
137
+        catch (UndefinedRouteException)
138
+        {
129 139
             //In some cases route name can be provided as controller:action pair, we can try to
130 140
             //generate such route automatically based on our default/fallback route
131 141
             return $this->castRoute($route)->uri($parameters);
@@ -137,29 +147,35 @@  discard block
 block discarded – undo
137 147
         /** @var GroupRegistry $groups */
138 148
         $groups = $this->container->get(GroupRegistry::class);
139 149
 
140
-        foreach ($routes->getCollection() as $name => $configurator) {
150
+        foreach ($routes->getCollection() as $name => $configurator)
151
+        {
141 152
             $target = $configurator->target;
142
-            if ($configurator->core !== null && $target instanceof AbstractTarget) {
153
+            if ($configurator->core !== null && $target instanceof AbstractTarget)
154
+            {
143 155
                 $target = $target->withCore($configurator->core);
144 156
             }
145 157
 
146 158
             $route = new Route(\ltrim($configurator->pattern, '/'), $target, $configurator->defaults);
147 159
 
148
-            if ($configurator->middleware !== null) {
160
+            if ($configurator->middleware !== null)
161
+            {
149 162
                 $route = $route->withMiddleware(...$configurator->middleware);
150 163
             }
151 164
 
152
-            if ($configurator->methods !== null) {
165
+            if ($configurator->methods !== null)
166
+            {
153 167
                 $route = $route->withVerbs(...$configurator->methods);
154 168
             }
155 169
 
156
-            if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) {
170
+            if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME)
171
+            {
157 172
                 $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup());
158 173
                 $group->setPrefix($configurator->prefix);
159 174
                 $group->addRoute($name, $route);
160 175
             }
161 176
 
162
-            if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) {
177
+            if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME)
178
+            {
163 179
                 $this->setDefault($route);
164 180
             }
165 181
         }
@@ -170,17 +186,20 @@  discard block
 block discarded – undo
170 186
      */
171 187
     protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface
172 188
     {
173
-        foreach ($this->routes as $name => $route) {
189
+        foreach ($this->routes as $name => $route)
190
+        {
174 191
             // Matched route will return new route instance with matched parameters
175 192
             $matched = $route->match($request);
176 193
 
177
-            if ($matched !== null) {
194
+            if ($matched !== null)
195
+            {
178 196
                 $routeName = $name;
179 197
                 return $matched;
180 198
             }
181 199
         }
182 200
 
183
-        if ($this->default !== null) {
201
+        if ($this->default !== null)
202
+        {
184 203
             return $this->default->match($request);
185 204
         }
186 205
 
@@ -193,14 +212,18 @@  discard block
 block discarded – undo
193 212
      */
194 213
     protected function configure(RouteInterface $route): RouteInterface
195 214
     {
196
-        if ($route instanceof ContainerizedInterface && !$route->hasContainer()) {
215
+        if ($route instanceof ContainerizedInterface && !$route->hasContainer())
216
+        {
197 217
             // isolating route in a given container
198 218
             $route = $route->withContainer($this->container);
199 219
         }
200 220
 
201
-        try {
221
+        try
222
+        {
202 223
             $uriHandler = $route->getUriHandler();
203
-        } catch (\Throwable) {
224
+        }
225
+        catch (\Throwable)
226
+        {
204 227
             $uriHandler = $this->uriHandler;
205 228
         }
206 229
 
@@ -229,11 +252,16 @@  discard block
 block discarded – undo
229 252
             );
230 253
         }
231 254
 
232
-        if (!empty($matches['name'])) {
255
+        if (!empty($matches['name']))
256
+        {
233 257
             $routeObject = $this->getRoute($matches['name']);
234
-        } elseif ($this->default !== null) {
258
+        }
259
+        elseif ($this->default !== null)
260
+        {
235 261
             $routeObject = $this->default;
236
-        } else {
262
+        }
263
+        else
264
+        {
237 265
             throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route));
238 266
         }
239 267
 
Please login to merge, or discard this patch.
src/Router/src/Loader/Configurator/ImportConfigurator.php 1 patch
Braces   +12 added lines, -6 removed lines patch added patch discarded remove patch
@@ -33,7 +33,8 @@  discard block
 block discarded – undo
33 33
 
34 34
     public function defaults(array $defaults): self
35 35
     {
36
-        foreach ($this->routes->all() as $configurator) {
36
+        foreach ($this->routes->all() as $configurator)
37
+        {
37 38
             $configurator->defaults($defaults);
38 39
         }
39 40
 
@@ -45,7 +46,8 @@  discard block
 block discarded – undo
45 46
      */
46 47
     public function group(string $group): self
47 48
     {
48
-        foreach ($this->routes->all() as $configurator) {
49
+        foreach ($this->routes->all() as $configurator)
50
+        {
49 51
             $configurator->group($group);
50 52
         }
51 53
 
@@ -57,7 +59,8 @@  discard block
 block discarded – undo
57 59
      */
58 60
     public function prefix(string $prefix): self
59 61
     {
60
-        foreach ($this->routes->all() as $configurator) {
62
+        foreach ($this->routes->all() as $configurator)
63
+        {
61 64
             $configurator->prefix($prefix);
62 65
         }
63 66
 
@@ -69,7 +72,8 @@  discard block
 block discarded – undo
69 72
      */
70 73
     public function namePrefix(string $prefix): self
71 74
     {
72
-        foreach ($this->routes->all() as $name => $configurator) {
75
+        foreach ($this->routes->all() as $name => $configurator)
76
+        {
73 77
             $this->routes->add($prefix . $name, $configurator);
74 78
             $this->routes->remove($name);
75 79
         }
@@ -79,7 +83,8 @@  discard block
 block discarded – undo
79 83
 
80 84
     public function core(CoreInterface $core): self
81 85
     {
82
-        foreach ($this->routes->all() as $configurator) {
86
+        foreach ($this->routes->all() as $configurator)
87
+        {
83 88
             $configurator->core($core);
84 89
         }
85 90
 
@@ -88,7 +93,8 @@  discard block
 block discarded – undo
88 93
 
89 94
     public function middleware(MiddlewareInterface|string|array $middleware): self
90 95
     {
91
-        foreach ($this->routes->all() as $configurator) {
96
+        foreach ($this->routes->all() as $configurator)
97
+        {
92 98
             $configurator->middleware($middleware);
93 99
         }
94 100
 
Please login to merge, or discard this patch.
src/AnnotatedRoutes/src/RouteLocatorListener.php 1 patch
Braces   +8 added lines, -4 removed lines patch added patch discarded remove patch
@@ -24,10 +24,12 @@  discard block
 block discarded – undo
24 24
 
25 25
     public function listen(ReflectionClass $class): void
26 26
     {
27
-        foreach ($class->getMethods() as $method) {
27
+        foreach ($class->getMethods() as $method)
28
+        {
28 29
             $route = $this->reader->firstFunctionMetadata($method, Route::class);
29 30
 
30
-            if ($route === null) {
31
+            if ($route === null)
32
+            {
31 33
                 continue;
32 34
             }
33 35
 
@@ -40,7 +42,8 @@  discard block
 block discarded – undo
40 42
         $defaultGroup = $this->groups->getDefaultGroup();
41 43
 
42 44
         $routes = [];
43
-        foreach ($this->attributes as $classes) {
45
+        foreach ($this->attributes as $classes)
46
+        {
44 47
             [$method, $route] = $classes;
45 48
             $class = $method->getDeclaringClass();
46 49
 
@@ -64,7 +67,8 @@  discard block
 block discarded – undo
64 67
 
65 68
     private function configureRoutes(array $routes): void
66 69
     {
67
-        foreach ($routes as $name => $schema) {
70
+        foreach ($routes as $name => $schema)
71
+        {
68 72
             $route = new \Spiral\Router\Route(
69 73
                 $schema['pattern'],
70 74
                 new Action($schema['controller'], $schema['action']),
Please login to merge, or discard this patch.
src/AnnotatedRoutes/tests/RouteLocatorListenerTest.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -53,7 +53,8 @@
 block discarded – undo
53 53
         $this->container = new Container();
54 54
 
55 55
         $this->container->bindSingleton(UriFactoryInterface::class, new Psr17Factory());
56
-        $this->container->bindSingleton(RouterInterface::class, static function (UriHandler $handler, Container $container) {
56
+        $this->container->bindSingleton(RouterInterface::class, static function (UriHandler $handler, Container $container)
57
+        {
57 58
             return new Router('/', $handler, $container);
58 59
         });
59 60
         $this->container->bindSingleton(GroupRegistry::class, new GroupRegistry($this->container));
Please login to merge, or discard this patch.
src/Router/src/RouteGroup.php 1 patch
Braces   +17 added lines, -8 removed lines patch added patch discarded remove patch
@@ -72,7 +72,8 @@  discard block
 block discarded – undo
72 72
 
73 73
     public function setCore(Autowire|CoreInterface|string $core): self
74 74
     {
75
-        if (!$core instanceof CoreInterface) {
75
+        if (!$core instanceof CoreInterface)
76
+        {
76 77
             $core = $this->container->get($core);
77 78
         }
78 79
         $this->core = $core;
@@ -90,7 +91,8 @@  discard block
 block discarded – undo
90 91
     {
91 92
         $this->middleware[] = $middleware;
92 93
 
93
-        if ($this->container instanceof BinderInterface && $this->name !== null) {
94
+        if ($this->container instanceof BinderInterface && $this->name !== null)
95
+        {
94 96
             $this->container->bind(
95 97
                 'middleware:' . $this->name,
96 98
                 function (PipelineFactory $factory): Pipeline {
@@ -112,7 +114,8 @@  discard block
 block discarded – undo
112 114
      */
113 115
     public function flushRoutes(): void
114 116
     {
115
-        foreach ($this->routes as $name) {
117
+        foreach ($this->routes as $name)
118
+        {
116 119
             $this->router->setRoute($name, $this->applyGroupParams($this->router->getRoute($name)));
117 120
         }
118 121
     }
@@ -122,7 +125,8 @@  discard block
 block discarded – undo
122 125
      */
123 126
     public function addRoute(string $name, Route $route): self
124 127
     {
125
-        if ($this->name !== null && $this->middleware !== []) {
128
+        if ($this->name !== null && $this->middleware !== [])
129
+        {
126 130
             $route = $route->withMiddleware('middleware:' . $this->name);
127 131
         }
128 132
 
@@ -135,17 +139,22 @@  discard block
 block discarded – undo
135 139
 
136 140
     private function applyGroupParams(Route $route): Route
137 141
     {
138
-        if ($this->core !== null) {
142
+        if ($this->core !== null)
143
+        {
139 144
             $target = $route->getTarget();
140 145
 
141
-            if ($target instanceof AbstractTarget) {
146
+            if ($target instanceof AbstractTarget)
147
+            {
142 148
                 $route = $route->withTarget($target->withCore($this->core));
143 149
             }
144 150
         }
145 151
 
146
-        try {
152
+        try
153
+        {
147 154
             $uriHandler = $route->getUriHandler();
148
-        } catch (\Throwable) {
155
+        }
156
+        catch (\Throwable)
157
+        {
149 158
             $uriHandler = $this->handler;
150 159
         }
151 160
 
Please login to merge, or discard this patch.
src/Router/src/GroupRegistry.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -23,7 +23,8 @@
 block discarded – undo
23 23
 
24 24
     public function getGroup(string $name): RouteGroup
25 25
     {
26
-        if (!isset($this->groups[$name])) {
26
+        if (!isset($this->groups[$name]))
27
+        {
27 28
             $this->groups[$name] = $this->factory->make(RouteGroup::class, ['name' => $name]);
28 29
         }
29 30
 
Please login to merge, or discard this patch.
src/Framework/Bootloader/Http/RoutesBootloader.php 1 patch
Braces   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -14,7 +14,8 @@  discard block
 block discarded – undo
14 14
 {
15 15
     public function init(HttpBootloader $http): void
16 16
     {
17
-        foreach ($this->globalMiddleware() as $middleware) {
17
+        foreach ($this->globalMiddleware() as $middleware)
18
+        {
18 19
             $http->addMiddleware($middleware);
19 20
         }
20 21
     }
@@ -53,8 +54,10 @@  discard block
 block discarded – undo
53 54
 
54 55
     private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void
55 56
     {
56
-        foreach ($groups as $group => $middlewares) {
57
-            foreach ($middlewares as $middleware) {
57
+        foreach ($groups as $group => $middlewares)
58
+        {
59
+            foreach ($middlewares as $middleware)
60
+            {
58 61
                 $registry->getGroup($group)->addMiddleware($middleware);
59 62
             }
60 63
         }
Please login to merge, or discard this patch.