Passed
Pull Request — master (#1192)
by butschster
14:46 queued 03:21
created
src/Router/src/UriHandler.php 1 patch
Braces   +62 added lines, -30 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
 
@@ -213,19 +217,23 @@  discard block
 block discarded – undo
213 217
         );
214 218
 
215 219
         $required = \array_keys($this->constrains);
216
-        if ($this->strict) {
220
+        if ($this->strict)
221
+        {
217 222
             $required = \array_unique([...$this->requiredOptions, ...$required]);
218 223
         }
219 224
 
220 225
         $missingParameters = [];
221 226
 
222
-        foreach ($required as $key) {
223
-            if (empty($parameters[$key])) {
227
+        foreach ($required as $key)
228
+        {
229
+            if (empty($parameters[$key]))
230
+            {
224 231
                 $missingParameters[] = $key;
225 232
             }
226 233
         }
227 234
 
228
-        if ($missingParameters !== []) {
235
+        if ($missingParameters !== [])
236
+        {
229 237
             throw new UriHandlerException(
230 238
                 \sprintf(
231 239
                     \count($missingParameters) === 1
@@ -255,18 +263,23 @@  discard block
 block discarded – undo
255 263
         $allowed = \array_keys($this->options);
256 264
 
257 265
         $result = [];
258
-        foreach ($parameters as $key => $parameter) {
259
-            if (\is_int($key) && isset($allowed[$key])) {
266
+        foreach ($parameters as $key => $parameter)
267
+        {
268
+            if (\is_int($key) && isset($allowed[$key]))
269
+            {
260 270
                 // this segment fetched keys from given parameters either by name or by position
261 271
                 $key = $allowed[$key];
262
-            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) {
272
+            }
273
+            elseif (!\array_key_exists($key, $this->options) && \is_array($parameters))
274
+            {
263 275
                 // all additional parameters given in array form can be glued to query string
264 276
                 $query[$key] = $parameter;
265 277
                 continue;
266 278
             }
267 279
 
268 280
             // String must be normalized here
269
-            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
281
+            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter))
282
+            {
270 283
                 $result[$key] = ($this->pathSegmentEncoder)($parameter);
271 284
                 continue;
272 285
             }
@@ -284,13 +297,17 @@  discard block
 block discarded – undo
284 297
     {
285 298
         $path = $uri->getPath();
286 299
 
287
-        if (empty($path) || $path[0] !== '/') {
300
+        if (empty($path) || $path[0] !== '/')
301
+        {
288 302
             $path = '/' . $path;
289 303
         }
290 304
 
291
-        if ($this->matchHost) {
305
+        if ($this->matchHost)
306
+        {
292 307
             $uriString = $uri->getHost() . $path;
293
-        } else {
308
+        }
309
+        else
310
+        {
294 311
             $uriString = \substr($path, \strlen($this->basePath)) ?: '';
295 312
         }
296 313
 
@@ -305,7 +322,8 @@  discard block
 block discarded – undo
305 322
      */
306 323
     private function compile(): void
307 324
     {
308
-        if ($this->pattern === null) {
325
+        if ($this->pattern === null)
326
+        {
309 327
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
310 328
         }
311 329
 
@@ -319,15 +337,18 @@  discard block
 block discarded – undo
319 337
         $pattern = \rtrim(\ltrim($pattern, ':/'), '/');
320 338
 
321 339
         // correct [/ first occurrence]
322
-        if (\str_starts_with($pattern, '[/')) {
340
+        if (\str_starts_with($pattern, '[/'))
341
+        {
323 342
             $pattern = '[' . \substr($pattern, 2);
324 343
         }
325 344
 
326 345
         // 2) Extract variables from the pattern
327
-        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
346
+        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches))
347
+        {
328 348
             $variables = \array_combine($matches[1], $matches[2]);
329 349
 
330
-            foreach ($variables as $key => $segment) {
350
+            foreach ($variables as $key => $segment)
351
+            {
331 352
                 $segment = $this->prepareSegment($key, $segment);
332 353
                 $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment);
333 354
                 $options[] = $key;
@@ -339,14 +360,17 @@  discard block
 block discarded – undo
339 360
         $options = \array_fill_keys($options, null);
340 361
 
341 362
         // 3) Validate constraints
342
-        foreach ($this->constrains as $key => $value) {
343
-            if ($value instanceof Autofill) {
363
+        foreach ($this->constrains as $key => $value)
364
+        {
365
+            if ($value instanceof Autofill)
366
+            {
344 367
                 // only forces value replacement, not required to be presented as parameter
345 368
                 continue;
346 369
             }
347 370
 
348 371
             // If a constraint references a param that doesn't appear in the pattern or defaults
349
-            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) {
372
+            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key]))
373
+            {
350 374
                 throw new ConstrainException(
351 375
                     \sprintf(
352 376
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -363,7 +387,8 @@  discard block
 block discarded – undo
363 387
         $this->options = $options;
364 388
 
365 389
         // 5) Mark which parameters are required vs. optional
366
-        if ($this->strict) {
390
+        if ($this->strict)
391
+        {
367 392
             $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options));
368 393
         }
369 394
     }
@@ -385,20 +410,25 @@  discard block
 block discarded – undo
385 410
         $pos = 0;
386 411
         $length = \strlen($pattern);
387 412
 
388
-        while ($pos < $length) {
413
+        while ($pos < $length)
414
+        {
389 415
             $char = $pattern[$pos];
390 416
 
391 417
             // We enter an optional segment
392
-            if ($char === '[') {
418
+            if ($char === '[')
419
+            {
393 420
                 \array_push($stack, '[');
394 421
             } // We exit an optional segment
395
-            elseif ($char === ']') {
422
+            elseif ($char === ']')
423
+            {
396 424
                 \array_pop($stack);
397 425
             } // We see a parameter like <id> or <action:\d+>
398
-            elseif ($char === '<') {
426
+            elseif ($char === '<')
427
+            {
399 428
                 // Find the closing '>'
400 429
                 $endPos = \strpos($pattern, '>', $pos);
401
-                if ($endPos === false) {
430
+                if ($endPos === false)
431
+                {
402 432
                     break;
403 433
                 }
404 434
 
@@ -409,7 +439,8 @@  discard block
 block discarded – undo
409 439
                 $varName = \explode(':', $varPart)[0];
410 440
 
411 441
                 // If we are inside a bracket, that var is optional
412
-                if ($stack !== []) {
442
+                if ($stack !== [])
443
+                {
413 444
                     $optionalVars[] = $varName;
414 445
                 }
415 446
 
@@ -434,7 +465,8 @@  discard block
 block discarded – undo
434 465
     private function interpolate(string $string, array $values): string
435 466
     {
436 467
         $replaces = [];
437
-        foreach ($values as $key => $value) {
468
+        foreach ($values as $key => $value)
469
+        {
438 470
             $replaces[\sprintf('<%s>', $key)] = match (true) {
439 471
                 $value instanceof \Stringable || \is_scalar($value) => (string) $value,
440 472
                 default => '',
Please login to merge, or discard this patch.