Route::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 15
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 13

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Routing\Data;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Dispatch\Data\Contract\MethodDispatchContract;
18
use Valkyrja\Http\Message\Enum\RequestMethod;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Enum\RequestMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Valkyrja\Http\Middleware\Contract\RouteDispatchedMiddlewareContract;
20
use Valkyrja\Http\Middleware\Contract\RouteMatchedMiddlewareContract;
21
use Valkyrja\Http\Middleware\Contract\SendingResponseMiddlewareContract;
22
use Valkyrja\Http\Middleware\Contract\TerminatedMiddlewareContract;
23
use Valkyrja\Http\Middleware\Contract\ThrowableCaughtMiddlewareContract;
24
use Valkyrja\Http\Routing\Data\Contract\ParameterContract;
25
use Valkyrja\Http\Routing\Data\Contract\RouteContract;
26
use Valkyrja\Http\Struct\Request\Contract\RequestStructContract;
27
use Valkyrja\Http\Struct\Response\Contract\ResponseStructContract;
28
29
use function in_array;
30
31
class Route implements RouteContract
32
{
33
    /**
34
     * @param non-empty-string                                  $path                      The path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
35
     * @param non-empty-string                                  $name                      The name
36
     * @param non-empty-string|null                             $regex                     The regex
37
     * @param RequestMethod[]                                   $requestMethods            The request methods
38
     * @param ParameterContract[]                               $parameters                The parameters
39
     * @param class-string<RouteMatchedMiddlewareContract>[]    $routeMatchedMiddleware    The route matched middleware
40
     * @param class-string<RouteDispatchedMiddlewareContract>[] $routeDispatchedMiddleware The route dispatched middleware
41
     * @param class-string<ThrowableCaughtMiddlewareContract>[] $throwableCaughtMiddleware The throwable caught middleware
42
     * @param class-string<SendingResponseMiddlewareContract>[] $sendingResponseMiddleware The sending response middleware
43
     * @param class-string<TerminatedMiddlewareContract>[]      $terminatedMiddleware      The terminated middleware
44
     * @param class-string<RequestStructContract>|null          $requestStruct             The request struct
45
     * @param class-string<ResponseStructContract>|null         $responseStruct            The response struct
46
     */
47
    public function __construct(
48
        protected string $path,
49
        protected string $name,
50
        protected MethodDispatchContract $dispatch,
51
        protected array $requestMethods = [RequestMethod::HEAD, RequestMethod::GET],
52
        protected string|null $regex = null,
53
        protected array $parameters = [],
54
        protected array $routeMatchedMiddleware = [],
55
        protected array $routeDispatchedMiddleware = [],
56
        protected array $throwableCaughtMiddleware = [],
57
        protected array $sendingResponseMiddleware = [],
58
        protected array $terminatedMiddleware = [],
59
        protected string|null $requestStruct = null,
60
        protected string|null $responseStruct = null,
61
    ) {
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    #[Override]
68
    public function getPath(): string
69
    {
70
        return $this->path;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    #[Override]
77
    public function withPath(string $path): static
78
    {
79
        $new = clone $this;
80
81
        $new->path = $this->getFilteredPath($path);
82
83
        return $new;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    #[Override]
90
    public function withAddedPath(string $path): static
91
    {
92
        $new = clone $this;
93
94
        $filteredExistingPath = $this->getFilteredPath($this->path);
95
        $filteredPath         = $this->getFilteredPath($path);
96
97
        $new->path = $this->getFilteredPath($filteredExistingPath . $filteredPath);
98
99
        return $new;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    #[Override]
106
    public function getName(): string
107
    {
108
        return $this->name;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    #[Override]
115
    public function withName(string $name): static
116
    {
117
        $new = clone $this;
118
119
        $new->name = $name;
120
121
        return $new;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    #[Override]
128
    public function withAddedName(string $name): static
129
    {
130
        $new = clone $this;
131
132
        $new->name = $this->name . $name;
133
134
        return $new;
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    #[Override]
141
    public function getDispatch(): MethodDispatchContract
142
    {
143
        return $this->dispatch;
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    #[Override]
150
    public function withDispatch(MethodDispatchContract $dispatch): static
151
    {
152
        $new = clone $this;
153
154
        $new->dispatch = $dispatch;
155
156
        return $new;
157
    }
158
159
    /**
160
     * @inheritDoc
161
     *
162
     * @return RequestMethod[]
163
     */
164
    #[Override]
165
    public function getRequestMethods(): array
166
    {
167
        return $this->requestMethods;
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    #[Override]
174
    public function hasRequestMethod(RequestMethod $requestMethod): bool
175
    {
176
        return in_array($requestMethod, $this->requestMethods, true);
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    #[Override]
183
    public function withRequestMethod(RequestMethod $requestMethod): static
184
    {
185
        $new = clone $this;
186
187
        $new->requestMethods = [$requestMethod];
188
189
        return $new;
190
    }
191
192
    /**
193
     * @inheritDoc
194
     */
195
    #[Override]
196
    public function withRequestMethods(RequestMethod ...$requestMethods): static
197
    {
198
        $new = clone $this;
199
200
        $new->requestMethods = $requestMethods;
201
202
        return $new;
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    #[Override]
209
    public function withAddedRequestMethod(RequestMethod $requestMethod): static
210
    {
211
        $new = clone $this;
212
213
        if (! in_array($requestMethod, $this->requestMethods, true)) {
214
            $new->requestMethods[] = $requestMethod;
215
        }
216
217
        return $new;
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    #[Override]
224
    public function withAddedRequestMethods(RequestMethod ...$requestMethods): static
225
    {
226
        $new = clone $this;
227
228
        foreach ($requestMethods as $requestMethod) {
229
            if (! in_array($requestMethod, $this->requestMethods, true)) {
230
                $new->requestMethods[] = $requestMethod;
231
            }
232
        }
233
234
        return $new;
235
    }
236
237
    /**
238
     * @inheritDoc
239
     */
240
    #[Override]
241
    public function getRegex(): string|null
242
    {
243
        return $this->regex;
244
    }
245
246
    /**
247
     * @inheritDoc
248
     */
249
    #[Override]
250
    public function withRegex(string|null $regex = null): static
251
    {
252
        $new = clone $this;
253
254
        $new->regex = $regex;
255
256
        return $new;
257
    }
258
259
    /**
260
     * @inheritDoc
261
     *
262
     * @return array<array-key, ParameterContract>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, ParameterContract> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, ParameterContract>.
Loading history...
263
     */
264
    #[Override]
265
    public function getParameters(): array
266
    {
267
        return $this->parameters;
268
    }
269
270
    /**
271
     * @inheritDoc
272
     */
273
    #[Override]
274
    public function withParameter(ParameterContract $parameter): static
275
    {
276
        $new = clone $this;
277
278
        $new->parameters = [$parameter];
279
280
        return $new;
281
    }
282
283
    /**
284
     * @inheritDoc
285
     */
286
    #[Override]
287
    public function withParameters(ParameterContract ...$parameters): static
288
    {
289
        $new = clone $this;
290
291
        $new->parameters = $parameters;
292
293
        return $new;
294
    }
295
296
    /**
297
     * @inheritDoc
298
     */
299
    #[Override]
300
    public function withAddedParameter(ParameterContract $parameter): static
301
    {
302
        $new = clone $this;
303
304
        $new->parameters[] = $parameter;
305
306
        return $new;
307
    }
308
309
    /**
310
     * @inheritDoc
311
     */
312
    #[Override]
313
    public function withAddedParameters(ParameterContract ...$parameters): static
314
    {
315
        $new = clone $this;
316
317
        $new->parameters = array_merge($this->parameters, $parameters);
318
319
        return $new;
320
    }
321
322
    /**
323
     * @inheritDoc
324
     *
325
     * @return class-string<RouteMatchedMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteMatchedMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteMatchedMiddlewareContract>[].
Loading history...
326
     */
327
    #[Override]
328
    public function getRouteMatchedMiddleware(): array
329
    {
330
        return $this->routeMatchedMiddleware;
331
    }
332
333
    /**
334
     * @inheritDoc
335
     *
336
     * @param class-string<RouteMatchedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteMatchedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteMatchedMiddlewareContract>.
Loading history...
337
     */
338
    #[Override]
339
    public function withRouteMatchedMiddleware(string ...$middleware): static
340
    {
341
        $new = clone $this;
342
343
        $new->routeMatchedMiddleware = $middleware;
344
345
        return $new;
346
    }
347
348
    /**
349
     * @inheritDoc
350
     *
351
     * @param class-string<RouteMatchedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteMatchedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteMatchedMiddlewareContract>.
Loading history...
352
     */
353
    #[Override]
354
    public function withAddedRouteMatchedMiddleware(string ...$middleware): static
355
    {
356
        $new = clone $this;
357
358
        $new->routeMatchedMiddleware = array_merge($this->routeMatchedMiddleware, $middleware);
359
360
        return $new;
361
    }
362
363
    /**
364
     * @inheritDoc
365
     *
366
     * @return class-string<RouteDispatchedMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteDispatchedMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteDispatchedMiddlewareContract>[].
Loading history...
367
     */
368
    #[Override]
369
    public function getRouteDispatchedMiddleware(): array
370
    {
371
        return $this->routeDispatchedMiddleware;
372
    }
373
374
    /**
375
     * @inheritDoc
376
     *
377
     * @param class-string<RouteDispatchedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteDispatchedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteDispatchedMiddlewareContract>.
Loading history...
378
     */
379
    #[Override]
380
    public function withRouteDispatchedMiddleware(string ...$middleware): static
381
    {
382
        $new = clone $this;
383
384
        $new->routeDispatchedMiddleware = $middleware;
385
386
        return $new;
387
    }
388
389
    /**
390
     * @inheritDoc
391
     *
392
     * @param class-string<RouteDispatchedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<RouteDispatchedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<RouteDispatchedMiddlewareContract>.
Loading history...
393
     */
394
    #[Override]
395
    public function withAddedRouteDispatchedMiddleware(string ...$middleware): static
396
    {
397
        $new = clone $this;
398
399
        $new->routeDispatchedMiddleware = array_merge($this->routeDispatchedMiddleware, $middleware);
400
401
        return $new;
402
    }
403
404
    /**
405
     * @inheritDoc
406
     *
407
     * @return class-string<ThrowableCaughtMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddlewareContract>[].
Loading history...
408
     */
409
    #[Override]
410
    public function getThrowableCaughtMiddleware(): array
411
    {
412
        return $this->throwableCaughtMiddleware;
413
    }
414
415
    /**
416
     * @inheritDoc
417
     *
418
     * @param class-string<ThrowableCaughtMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddlewareContract>.
Loading history...
419
     */
420
    #[Override]
421
    public function withThrowableCaughtMiddleware(string ...$middleware): static
422
    {
423
        $new = clone $this;
424
425
        $new->throwableCaughtMiddleware = $middleware;
426
427
        return $new;
428
    }
429
430
    /**
431
     * @inheritDoc
432
     *
433
     * @param class-string<ThrowableCaughtMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddlewareContract>.
Loading history...
434
     */
435
    #[Override]
436
    public function withAddedThrowableCaughtMiddleware(string ...$middleware): static
437
    {
438
        $new = clone $this;
439
440
        $new->throwableCaughtMiddleware = array_merge($this->throwableCaughtMiddleware, $middleware);
441
442
        return $new;
443
    }
444
445
    /**
446
     * @inheritDoc
447
     *
448
     * @return class-string<SendingResponseMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<SendingResponseMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<SendingResponseMiddlewareContract>[].
Loading history...
449
     */
450
    #[Override]
451
    public function getSendingResponseMiddleware(): array
452
    {
453
        return $this->sendingResponseMiddleware;
454
    }
455
456
    /**
457
     * @inheritDoc
458
     *
459
     * @param class-string<SendingResponseMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<SendingResponseMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<SendingResponseMiddlewareContract>.
Loading history...
460
     */
461
    #[Override]
462
    public function withSendingResponseMiddleware(string ...$middleware): static
463
    {
464
        $new = clone $this;
465
466
        $new->sendingResponseMiddleware = $middleware;
467
468
        return $new;
469
    }
470
471
    /**
472
     * @inheritDoc
473
     *
474
     * @param class-string<SendingResponseMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<SendingResponseMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<SendingResponseMiddlewareContract>.
Loading history...
475
     */
476
    #[Override]
477
    public function withAddedSendingResponseMiddleware(string ...$middleware): static
478
    {
479
        $new = clone $this;
480
481
        $new->sendingResponseMiddleware = array_merge($this->sendingResponseMiddleware, $middleware);
482
483
        return $new;
484
    }
485
486
    /**
487
     * @inheritDoc
488
     *
489
     * @return class-string<TerminatedMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TerminatedMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TerminatedMiddlewareContract>[].
Loading history...
490
     */
491
    #[Override]
492
    public function getTerminatedMiddleware(): array
493
    {
494
        return $this->terminatedMiddleware;
495
    }
496
497
    /**
498
     * @inheritDoc
499
     *
500
     * @param class-string<TerminatedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TerminatedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TerminatedMiddlewareContract>.
Loading history...
501
     */
502
    #[Override]
503
    public function withTerminatedMiddleware(string ...$middleware): static
504
    {
505
        $new = clone $this;
506
507
        $new->terminatedMiddleware = $middleware;
508
509
        return $new;
510
    }
511
512
    /**
513
     * @inheritDoc
514
     *
515
     * @param class-string<TerminatedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TerminatedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TerminatedMiddlewareContract>.
Loading history...
516
     */
517
    #[Override]
518
    public function withAddedTerminatedMiddleware(string ...$middleware): static
519
    {
520
        $new = clone $this;
521
522
        $new->terminatedMiddleware = array_merge($this->terminatedMiddleware, $middleware);
523
524
        return $new;
525
    }
526
527
    /**
528
     * @inheritDoc
529
     */
530
    #[Override]
531
    public function getRequestStruct(): string|null
532
    {
533
        return $this->requestStruct;
534
    }
535
536
    /**
537
     * @inheritDoc
538
     */
539
    #[Override]
540
    public function withRequestStruct(string|null $requestStruct = null): static
541
    {
542
        $new = clone $this;
543
544
        $new->requestStruct = $requestStruct;
545
546
        return $new;
547
    }
548
549
    /**
550
     * @inheritDoc
551
     */
552
    #[Override]
553
    public function getResponseStruct(): string|null
554
    {
555
        return $this->responseStruct;
556
    }
557
558
    /**
559
     * @inheritDoc
560
     */
561
    #[Override]
562
    public function withResponseStruct(string|null $responseStruct = null): static
563
    {
564
        $new = clone $this;
565
566
        $new->responseStruct = $responseStruct;
567
568
        return $new;
569
    }
570
571
    /**
572
     * Validate a path.
573
     *
574
     * @param non-empty-string $path The path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
575
     *
576
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
577
     */
578
    protected function getFilteredPath(string $path): string
579
    {
580
        // Trim slashes from the beginning and end of the path
581
        if (! $path = trim($path, '/')) {
582
            // If the path only had a slash return as just slash
583
            return '/';
584
        }
585
586
        // Prepend a slash
587
        return '/' . $path;
588
    }
589
}
590