Passed
Pull Request — master (#1192)
by butschster
12:18
created
src/Router/tests/UriTest.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 
107 107
         $uri = $router->uri('test:id', [
108 108
             'id' => 100,
109
-            'title' => new class implements \Stringable {
109
+            'title' => new class implements \Stringable{
110 110
                 public function __toString()
111 111
                 {
112 112
                     return 'hello-world';
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
         $uriHandler->setStrict(true);
202 202
         $route = $route->withUriHandler($uriHandler);
203 203
 
204
-        self::assertSame($expected, (string) $route->uri($params));
204
+        self::assertSame($expected, (string)$route->uri($params));
205 205
     }
206 206
 
207 207
     #[DataProvider('provideSegmentInDifferentLanguages')]
Please login to merge, or discard this patch.
src/Router/src/UriHandler.php 2 patches
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
         private readonly UriFactoryInterface $uriFactory,
61 61
         ?SlugifyInterface $slugify = null,
62 62
         ?RoutePatternRegistryInterface $patternRegistry = null,
63
-    ) {
63
+    ){
64 64
         $this->patternRegistry = $patternRegistry ?? new DefaultPatternRegistry();
65 65
 
66 66
         $slugify ??= new Slugify();
@@ -130,7 +130,7 @@  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
             $basePath .= '/';
135 135
         }
136 136
 
@@ -180,12 +180,12 @@  discard block
 block discarded – undo
180 180
      */
181 181
     public function match(UriInterface $uri, array $defaults): ?array
182 182
     {
183
-        if (!$this->isCompiled()) {
183
+        if (!$this->isCompiled()){
184 184
             $this->compile();
185 185
         }
186 186
 
187 187
         $matches = [];
188
-        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) {
188
+        if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){
189 189
             return null;
190 190
         }
191 191
 
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
      */
203 203
     public function uri(iterable $parameters = [], array $defaults = []): UriInterface
204 204
     {
205
-        if (!$this->isCompiled()) {
205
+        if (!$this->isCompiled()){
206 206
             $this->compile();
207 207
         }
208 208
 
@@ -213,19 +213,19 @@  discard block
 block discarded – undo
213 213
         );
214 214
 
215 215
         $required = \array_keys($this->constrains);
216
-        if ($this->strict) {
216
+        if ($this->strict){
217 217
             $required = \array_unique([...$this->requiredOptions, ...$required]);
218 218
         }
219 219
 
220 220
         $missingParameters = [];
221 221
 
222
-        foreach ($required as $key) {
223
-            if (empty($parameters[$key])) {
222
+        foreach ($required as $key){
223
+            if (empty($parameters[$key])){
224 224
                 $missingParameters[] = $key;
225 225
             }
226 226
         }
227 227
 
228
-        if ($missingParameters !== []) {
228
+        if ($missingParameters !== []){
229 229
             throw new UriHandlerException(
230 230
                 \sprintf(
231 231
                     \count($missingParameters) === 1
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
         $path = $this->interpolate($this->template, $parameters);
241 241
 
242 242
         //Uri with added base path and prefix
243
-        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/'));
243
+        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/'));
244 244
 
245 245
         return empty($query) ? $uri : $uri->withQuery(\http_build_query($query));
246 246
     }
@@ -255,23 +255,23 @@  discard block
 block discarded – undo
255 255
         $allowed = \array_keys($this->options);
256 256
 
257 257
         $result = [];
258
-        foreach ($parameters as $key => $parameter) {
259
-            if (\is_int($key) && isset($allowed[$key])) {
258
+        foreach ($parameters as $key => $parameter){
259
+            if (\is_int($key) && isset($allowed[$key])){
260 260
                 // this segment fetched keys from given parameters either by name or by position
261 261
                 $key = $allowed[$key];
262
-            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) {
262
+            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){
263 263
                 // all additional parameters given in array form can be glued to query string
264 264
                 $query[$key] = $parameter;
265 265
                 continue;
266 266
             }
267 267
 
268 268
             // String must be normalized here
269
-            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
269
+            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){
270 270
                 $result[$key] = ($this->pathSegmentEncoder)($parameter);
271 271
                 continue;
272 272
             }
273 273
 
274
-            $result[$key] = (string) $parameter;
274
+            $result[$key] = (string)$parameter;
275 275
         }
276 276
 
277 277
         return $result;
@@ -284,13 +284,13 @@  discard block
 block discarded – undo
284 284
     {
285 285
         $path = $uri->getPath();
286 286
 
287
-        if (empty($path) || $path[0] !== '/') {
288
-            $path = '/' . $path;
287
+        if (empty($path) || $path[0] !== '/'){
288
+            $path = '/'.$path;
289 289
         }
290 290
 
291
-        if ($this->matchHost) {
292
-            $uriString = $uri->getHost() . $path;
293
-        } else {
291
+        if ($this->matchHost){
292
+            $uriString = $uri->getHost().$path;
293
+        }else{
294 294
             $uriString = \substr($path, \strlen($this->basePath)) ?: '';
295 295
         }
296 296
 
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
      */
306 306
     private function compile(): void
307 307
     {
308
-        if ($this->pattern === null) {
308
+        if ($this->pattern === null){
309 309
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
310 310
         }
311 311
 
@@ -315,19 +315,19 @@  discard block
 block discarded – undo
315 315
         // 1) Build full pattern
316 316
         $prefix = \rtrim($this->getPrefix(), '/ ');
317 317
         $pattern = \ltrim($this->pattern, '/ ');
318
-        $pattern = $prefix . '/' . $pattern;
318
+        $pattern = $prefix.'/'.$pattern;
319 319
         $pattern = \rtrim(\ltrim($pattern, ':/'), '/');
320 320
 
321 321
         // correct [/ first occurrence]
322
-        if (\str_starts_with($pattern, '[/')) {
323
-            $pattern = '[' . \substr($pattern, 2);
322
+        if (\str_starts_with($pattern, '[/')){
323
+            $pattern = '['.\substr($pattern, 2);
324 324
         }
325 325
 
326 326
         // 2) Extract variables from the pattern
327
-        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
327
+        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){
328 328
             $variables = \array_combine($matches[1], $matches[2]);
329 329
 
330
-            foreach ($variables as $key => $segment) {
330
+            foreach ($variables as $key => $segment){
331 331
                 $segment = $this->prepareSegment($key, $segment);
332 332
                 $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment);
333 333
                 $options[] = $key;
@@ -339,14 +339,14 @@  discard block
 block discarded – undo
339 339
         $options = \array_fill_keys($options, null);
340 340
 
341 341
         // 3) Validate constraints
342
-        foreach ($this->constrains as $key => $value) {
343
-            if ($value instanceof Autofill) {
342
+        foreach ($this->constrains as $key => $value){
343
+            if ($value instanceof Autofill){
344 344
                 // only forces value replacement, not required to be presented as parameter
345 345
                 continue;
346 346
             }
347 347
 
348 348
             // 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])) {
349
+            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){
350 350
                 throw new ConstrainException(
351 351
                     \sprintf(
352 352
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -358,12 +358,12 @@  discard block
 block discarded – undo
358 358
         }
359 359
 
360 360
         // 4) Compile your final regex pattern
361
-        $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu';
361
+        $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu';
362 362
         $this->template = \stripslashes(\str_replace('?', '', $template));
363 363
         $this->options = $options;
364 364
 
365 365
         // 5) Mark which parameters are required vs. optional
366
-        if ($this->strict) {
366
+        if ($this->strict){
367 367
             $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options));
368 368
         }
369 369
     }
@@ -385,20 +385,20 @@  discard block
 block discarded – undo
385 385
         $pos = 0;
386 386
         $length = \strlen($pattern);
387 387
 
388
-        while ($pos < $length) {
388
+        while ($pos < $length){
389 389
             $char = $pattern[$pos];
390 390
 
391 391
             // We enter an optional segment
392
-            if ($char === '[') {
392
+            if ($char === '['){
393 393
                 \array_push($stack, '[');
394 394
             } // We exit an optional segment
395
-            elseif ($char === ']') {
395
+            elseif ($char === ']'){
396 396
                 \array_pop($stack);
397 397
             } // We see a parameter like <id> or <action:\d+>
398
-            elseif ($char === '<') {
398
+            elseif ($char === '<'){
399 399
                 // Find the closing '>'
400 400
                 $endPos = \strpos($pattern, '>', $pos);
401
-                if ($endPos === false) {
401
+                if ($endPos === false){
402 402
                     break;
403 403
                 }
404 404
 
@@ -409,7 +409,7 @@  discard block
 block discarded – undo
409 409
                 $varName = \explode(':', $varPart)[0];
410 410
 
411 411
                 // If we are inside a bracket, that var is optional
412
-                if ($stack !== []) {
412
+                if ($stack !== []){
413 413
                     $optionalVars[] = $varName;
414 414
                 }
415 415
 
@@ -434,9 +434,9 @@  discard block
 block discarded – undo
434 434
     private function interpolate(string $string, array $values): string
435 435
     {
436 436
         $replaces = [];
437
-        foreach ($values as $key => $value) {
437
+        foreach ($values as $key => $value){
438 438
             $replaces[\sprintf('<%s>', $key)] = match (true) {
439
-                $value instanceof \Stringable || \is_scalar($value) => (string) $value,
439
+                $value instanceof \Stringable || \is_scalar($value) => (string)$value,
440 440
                 default => '',
441 441
             };
442 442
         }
@@ -456,7 +456,7 @@  discard block
 block discarded – undo
456 456
                 '|',
457 457
                 \array_map(fn(string $segment): string => $this->filterSegment($segment), $this->constrains[$name]),
458 458
             ),
459
-            default => $this->filterSegment((string) $this->constrains[$name])
459
+            default => $this->filterSegment((string)$this->constrains[$name])
460 460
         };
461 461
     }
462 462
 
Please login to merge, or discard this 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.