Test Failed
Pull Request — master (#1192)
by butschster
10:57
created
src/Router/src/UriHandler.php 1 patch
Braces   +58 added lines, -28 removed lines patch added patch discarded remove patch
@@ -130,7 +130,8 @@  discard block
 block discarded – undo
130 130
      */
131 131
     public function withBasePath(string $basePath): self
132 132
     {
133
-        if (!\str_ends_with($basePath, '/')) {
133
+        if (!\str_ends_with($basePath, '/'))
134
+        {
134 135
             $basePath .= '/';
135 136
         }
136 137
 
@@ -180,12 +181,14 @@  discard block
 block discarded – undo
180 181
      */
181 182
     public function match(UriInterface $uri, array $defaults): ?array
182 183
     {
183
-        if (!$this->isCompiled()) {
184
+        if (!$this->isCompiled())
185
+        {
184 186
             $this->compile();
185 187
         }
186 188
 
187 189
         $matches = [];
188
-        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) {
190
+        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches))
191
+        {
189 192
             return null;
190 193
         }
191 194
 
@@ -202,7 +205,8 @@  discard block
 block discarded – undo
202 205
      */
203 206
     public function uri(iterable $parameters = [], array $defaults = []): UriInterface
204 207
     {
205
-        if (!$this->isCompiled()) {
208
+        if (!$this->isCompiled())
209
+        {
206 210
             $this->compile();
207 211
         }
208 212
 
@@ -214,13 +218,15 @@  discard block
 block discarded – undo
214 218
 
215 219
         $required = \array_keys($this->constrains);
216 220
         $parametersToCheck = $parameters;
217
-        if ($this->strict) {
221
+        if ($this->strict)
222
+        {
218 223
             $required = \array_unique([...$this->requiredOptions, ...$required]);
219 224
             $parametersToCheck = \array_filter($parametersToCheck);
220 225
         }
221 226
 
222 227
         $missingParameters = \array_diff($required, \array_keys($parametersToCheck));
223
-        if ($missingParameters !== []) {
228
+        if ($missingParameters !== [])
229
+        {
224 230
             throw new UriHandlerException(
225 231
                 \sprintf(
226 232
                     \count($missingParameters) === 1
@@ -250,18 +256,23 @@  discard block
 block discarded – undo
250 256
         $allowed = \array_keys($this->options);
251 257
 
252 258
         $result = [];
253
-        foreach ($parameters as $key => $parameter) {
254
-            if (\is_int($key) && isset($allowed[$key])) {
259
+        foreach ($parameters as $key => $parameter)
260
+        {
261
+            if (\is_int($key) && isset($allowed[$key]))
262
+            {
255 263
                 // this segment fetched keys from given parameters either by name or by position
256 264
                 $key = $allowed[$key];
257
-            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) {
265
+            }
266
+            elseif (!\array_key_exists($key, $this->options) && \is_array($parameters))
267
+            {
258 268
                 // all additional parameters given in array form can be glued to query string
259 269
                 $query[$key] = $parameter;
260 270
                 continue;
261 271
             }
262 272
 
263 273
             // String must be normalized here
264
-            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
274
+            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter))
275
+            {
265 276
                 $result[$key] = ($this->pathSegmentEncoder)($parameter);
266 277
                 continue;
267 278
             }
@@ -279,13 +290,17 @@  discard block
 block discarded – undo
279 290
     {
280 291
         $path = $uri->getPath();
281 292
 
282
-        if (empty($path) || $path[0] !== '/') {
293
+        if (empty($path) || $path[0] !== '/')
294
+        {
283 295
             $path = '/' . $path;
284 296
         }
285 297
 
286
-        if ($this->matchHost) {
298
+        if ($this->matchHost)
299
+        {
287 300
             $uriString = $uri->getHost() . $path;
288
-        } else {
301
+        }
302
+        else
303
+        {
289 304
             $uriString = \substr($path, \strlen($this->basePath)) ?: '';
290 305
         }
291 306
 
@@ -300,7 +315,8 @@  discard block
 block discarded – undo
300 315
      */
301 316
     private function compile(): void
302 317
     {
303
-        if ($this->pattern === null) {
318
+        if ($this->pattern === null)
319
+        {
304 320
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
305 321
         }
306 322
 
@@ -314,15 +330,18 @@  discard block
 block discarded – undo
314 330
         $pattern = \rtrim(\ltrim($pattern, ':/'), '/');
315 331
 
316 332
         // correct [/ first occurrence]
317
-        if (\str_starts_with($pattern, '[/')) {
333
+        if (\str_starts_with($pattern, '[/'))
334
+        {
318 335
             $pattern = '[' . \substr($pattern, 2);
319 336
         }
320 337
 
321 338
         // 2) Extract variables from the pattern
322
-        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
339
+        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches))
340
+        {
323 341
             $variables = \array_combine($matches[1], $matches[2]);
324 342
 
325
-            foreach ($variables as $key => $segment) {
343
+            foreach ($variables as $key => $segment)
344
+            {
326 345
                 $segment = $this->prepareSegment($key, $segment);
327 346
                 $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment);
328 347
                 $options[] = $key;
@@ -334,14 +353,17 @@  discard block
 block discarded – undo
334 353
         $options = \array_fill_keys($options, null);
335 354
 
336 355
         // 3) Validate constraints
337
-        foreach ($this->constrains as $key => $value) {
338
-            if ($value instanceof Autofill) {
356
+        foreach ($this->constrains as $key => $value)
357
+        {
358
+            if ($value instanceof Autofill)
359
+            {
339 360
                 // only forces value replacement, not required to be presented as parameter
340 361
                 continue;
341 362
             }
342 363
 
343 364
             // If a constraint references a param that doesn't appear in the pattern or defaults
344
-            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) {
365
+            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key]))
366
+            {
345 367
                 throw new ConstrainException(
346 368
                     \sprintf(
347 369
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -358,7 +380,8 @@  discard block
 block discarded – undo
358 380
         $this->options = $options;
359 381
 
360 382
         // 5) Mark which parameters are required vs. optional
361
-        if ($this->strict) {
383
+        if ($this->strict)
384
+        {
362 385
             $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options));
363 386
         }
364 387
     }
@@ -380,20 +403,25 @@  discard block
 block discarded – undo
380 403
         $pos = 0;
381 404
         $length = \strlen($pattern);
382 405
 
383
-        while ($pos < $length) {
406
+        while ($pos < $length)
407
+        {
384 408
             $char = $pattern[$pos];
385 409
 
386 410
             // We enter an optional segment
387
-            if ($char === '[') {
411
+            if ($char === '[')
412
+            {
388 413
                 \array_push($stack, '[');
389 414
             } // We exit an optional segment
390
-            elseif ($char === ']') {
415
+            elseif ($char === ']')
416
+            {
391 417
                 \array_pop($stack);
392 418
             } // We see a parameter like <id> or <action:\d+>
393
-            elseif ($char === '<') {
419
+            elseif ($char === '<')
420
+            {
394 421
                 // Find the closing '>'
395 422
                 $endPos = \strpos($pattern, '>', $pos);
396
-                if ($endPos === false) {
423
+                if ($endPos === false)
424
+                {
397 425
                     break;
398 426
                 }
399 427
 
@@ -404,7 +432,8 @@  discard block
 block discarded – undo
404 432
                 $varName = \explode(':', $varPart)[0];
405 433
 
406 434
                 // If we are inside a bracket, that var is optional
407
-                if ($stack !== []) {
435
+                if ($stack !== [])
436
+                {
408 437
                     $optionalVars[] = $varName;
409 438
                 }
410 439
 
@@ -429,7 +458,8 @@  discard block
 block discarded – undo
429 458
     private function interpolate(string $string, array $values): string
430 459
     {
431 460
         $replaces = [];
432
-        foreach ($values as $key => $value) {
461
+        foreach ($values as $key => $value)
462
+        {
433 463
             $replaces[\sprintf('<%s>', $key)] = match (true) {
434 464
                 $value instanceof \Stringable || \is_scalar($value) => (string) $value,
435 465
                 default => '',
Please login to merge, or discard this patch.