Test Failed
Pull Request — master (#1192)
by butschster
10:57
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   +38 added lines, -38 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
 
@@ -214,13 +214,13 @@  discard block
 block discarded – undo
214 214
 
215 215
         $required = \array_keys($this->constrains);
216 216
         $parametersToCheck = $parameters;
217
-        if ($this->strict) {
217
+        if ($this->strict){
218 218
             $required = \array_unique([...$this->requiredOptions, ...$required]);
219 219
             $parametersToCheck = \array_filter($parametersToCheck);
220 220
         }
221 221
 
222 222
         $missingParameters = \array_diff($required, \array_keys($parametersToCheck));
223
-        if ($missingParameters !== []) {
223
+        if ($missingParameters !== []){
224 224
             throw new UriHandlerException(
225 225
                 \sprintf(
226 226
                     \count($missingParameters) === 1
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
         $path = $this->interpolate($this->template, $parameters);
236 236
 
237 237
         //Uri with added base path and prefix
238
-        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/'));
238
+        $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/'));
239 239
 
240 240
         return empty($query) ? $uri : $uri->withQuery(\http_build_query($query));
241 241
     }
@@ -250,23 +250,23 @@  discard block
 block discarded – undo
250 250
         $allowed = \array_keys($this->options);
251 251
 
252 252
         $result = [];
253
-        foreach ($parameters as $key => $parameter) {
254
-            if (\is_int($key) && isset($allowed[$key])) {
253
+        foreach ($parameters as $key => $parameter){
254
+            if (\is_int($key) && isset($allowed[$key])){
255 255
                 // this segment fetched keys from given parameters either by name or by position
256 256
                 $key = $allowed[$key];
257
-            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) {
257
+            } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){
258 258
                 // all additional parameters given in array form can be glued to query string
259 259
                 $query[$key] = $parameter;
260 260
                 continue;
261 261
             }
262 262
 
263 263
             // String must be normalized here
264
-            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) {
264
+            if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){
265 265
                 $result[$key] = ($this->pathSegmentEncoder)($parameter);
266 266
                 continue;
267 267
             }
268 268
 
269
-            $result[$key] = (string) $parameter;
269
+            $result[$key] = (string)$parameter;
270 270
         }
271 271
 
272 272
         return $result;
@@ -279,13 +279,13 @@  discard block
 block discarded – undo
279 279
     {
280 280
         $path = $uri->getPath();
281 281
 
282
-        if (empty($path) || $path[0] !== '/') {
283
-            $path = '/' . $path;
282
+        if (empty($path) || $path[0] !== '/'){
283
+            $path = '/'.$path;
284 284
         }
285 285
 
286
-        if ($this->matchHost) {
287
-            $uriString = $uri->getHost() . $path;
288
-        } else {
286
+        if ($this->matchHost){
287
+            $uriString = $uri->getHost().$path;
288
+        }else{
289 289
             $uriString = \substr($path, \strlen($this->basePath)) ?: '';
290 290
         }
291 291
 
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
      */
301 301
     private function compile(): void
302 302
     {
303
-        if ($this->pattern === null) {
303
+        if ($this->pattern === null){
304 304
             throw new UriHandlerException('Unable to compile UriHandler, pattern is not set');
305 305
         }
306 306
 
@@ -310,19 +310,19 @@  discard block
 block discarded – undo
310 310
         // 1) Build full pattern
311 311
         $prefix = \rtrim($this->getPrefix(), '/ ');
312 312
         $pattern = \ltrim($this->pattern, '/ ');
313
-        $pattern = $prefix . '/' . $pattern;
313
+        $pattern = $prefix.'/'.$pattern;
314 314
         $pattern = \rtrim(\ltrim($pattern, ':/'), '/');
315 315
 
316 316
         // correct [/ first occurrence]
317
-        if (\str_starts_with($pattern, '[/')) {
318
-            $pattern = '[' . \substr($pattern, 2);
317
+        if (\str_starts_with($pattern, '[/')){
318
+            $pattern = '['.\substr($pattern, 2);
319 319
         }
320 320
 
321 321
         // 2) Extract variables from the pattern
322
-        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) {
322
+        if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){
323 323
             $variables = \array_combine($matches[1], $matches[2]);
324 324
 
325
-            foreach ($variables as $key => $segment) {
325
+            foreach ($variables as $key => $segment){
326 326
                 $segment = $this->prepareSegment($key, $segment);
327 327
                 $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment);
328 328
                 $options[] = $key;
@@ -334,14 +334,14 @@  discard block
 block discarded – undo
334 334
         $options = \array_fill_keys($options, null);
335 335
 
336 336
         // 3) Validate constraints
337
-        foreach ($this->constrains as $key => $value) {
338
-            if ($value instanceof Autofill) {
337
+        foreach ($this->constrains as $key => $value){
338
+            if ($value instanceof Autofill){
339 339
                 // only forces value replacement, not required to be presented as parameter
340 340
                 continue;
341 341
             }
342 342
 
343 343
             // 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])) {
344
+            if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){
345 345
                 throw new ConstrainException(
346 346
                     \sprintf(
347 347
                         'Route `%s` does not define routing parameter `<%s>`.',
@@ -353,12 +353,12 @@  discard block
 block discarded – undo
353 353
         }
354 354
 
355 355
         // 4) Compile your final regex pattern
356
-        $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu';
356
+        $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu';
357 357
         $this->template = \stripslashes(\str_replace('?', '', $template));
358 358
         $this->options = $options;
359 359
 
360 360
         // 5) Mark which parameters are required vs. optional
361
-        if ($this->strict) {
361
+        if ($this->strict){
362 362
             $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options));
363 363
         }
364 364
     }
@@ -380,20 +380,20 @@  discard block
 block discarded – undo
380 380
         $pos = 0;
381 381
         $length = \strlen($pattern);
382 382
 
383
-        while ($pos < $length) {
383
+        while ($pos < $length){
384 384
             $char = $pattern[$pos];
385 385
 
386 386
             // We enter an optional segment
387
-            if ($char === '[') {
387
+            if ($char === '['){
388 388
                 \array_push($stack, '[');
389 389
             } // We exit an optional segment
390
-            elseif ($char === ']') {
390
+            elseif ($char === ']'){
391 391
                 \array_pop($stack);
392 392
             } // We see a parameter like <id> or <action:\d+>
393
-            elseif ($char === '<') {
393
+            elseif ($char === '<'){
394 394
                 // Find the closing '>'
395 395
                 $endPos = \strpos($pattern, '>', $pos);
396
-                if ($endPos === false) {
396
+                if ($endPos === false){
397 397
                     break;
398 398
                 }
399 399
 
@@ -404,7 +404,7 @@  discard block
 block discarded – undo
404 404
                 $varName = \explode(':', $varPart)[0];
405 405
 
406 406
                 // If we are inside a bracket, that var is optional
407
-                if ($stack !== []) {
407
+                if ($stack !== []){
408 408
                     $optionalVars[] = $varName;
409 409
                 }
410 410
 
@@ -429,9 +429,9 @@  discard block
 block discarded – undo
429 429
     private function interpolate(string $string, array $values): string
430 430
     {
431 431
         $replaces = [];
432
-        foreach ($values as $key => $value) {
432
+        foreach ($values as $key => $value){
433 433
             $replaces[\sprintf('<%s>', $key)] = match (true) {
434
-                $value instanceof \Stringable || \is_scalar($value) => (string) $value,
434
+                $value instanceof \Stringable || \is_scalar($value) => (string)$value,
435 435
                 default => '',
436 436
             };
437 437
         }
@@ -451,7 +451,7 @@  discard block
 block discarded – undo
451 451
                 '|',
452 452
                 \array_map(fn(string $segment): string => $this->filterSegment($segment), $this->constrains[$name]),
453 453
             ),
454
-            default => $this->filterSegment((string) $this->constrains[$name])
454
+            default => $this->filterSegment((string)$this->constrains[$name])
455 455
         };
456 456
     }
457 457
 
Please login to merge, or discard this 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.