Route::withRouteMatchedMiddleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Cli\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\Cli\Interaction\Message\Contract\MessageContract;
18
use Valkyrja\Cli\Middleware\Contract\ExitedMiddlewareContract;
19
use Valkyrja\Cli\Middleware\Contract\RouteDispatchedMiddlewareContract;
20
use Valkyrja\Cli\Middleware\Contract\RouteMatchedMiddlewareContract;
21
use Valkyrja\Cli\Middleware\Contract\ThrowableCaughtMiddlewareContract;
22
use Valkyrja\Cli\Routing\Data\Contract\ArgumentParameterContract;
23
use Valkyrja\Cli\Routing\Data\Contract\OptionParameterContract;
24
use Valkyrja\Cli\Routing\Data\Contract\ParameterContract;
25
use Valkyrja\Cli\Routing\Data\Contract\RouteContract;
26
use Valkyrja\Dispatch\Data\Contract\MethodDispatchContract;
27
28
class Route implements RouteContract
29
{
30
    /** @var ArgumentParameterContract[] */
31
    protected array $arguments = [];
32
33
    /** @var OptionParameterContract[] */
34
    protected array $options = [];
35
36
    /**
37
     * @param non-empty-string                                  $name                      The name
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...
38
     * @param non-empty-string                                  $description               The description
39
     * @param MessageContract                                   $helpText                  The help text
40
     * @param class-string<RouteMatchedMiddlewareContract>[]    $routeMatchedMiddleware    The command matched middleware
41
     * @param class-string<RouteDispatchedMiddlewareContract>[] $routeDispatchedMiddleware The command dispatched middleware
42
     * @param class-string<ThrowableCaughtMiddlewareContract>[] $throwableCaughtMiddleware The throwable caught middleware
43
     * @param class-string<ExitedMiddlewareContract>[]          $exitedMiddleware          The exited middleware
44
     * @param ParameterContract[]                               $parameters                The parameters
45
     */
46
    public function __construct(
47
        protected string $name,
48
        protected string $description,
49
        protected MessageContract $helpText,
50
        protected MethodDispatchContract $dispatch,
51
        protected array $routeMatchedMiddleware = [],
52
        protected array $routeDispatchedMiddleware = [],
53
        protected array $throwableCaughtMiddleware = [],
54
        protected array $exitedMiddleware = [],
55
        array $parameters = [],
56
    ) {
57
        foreach ($parameters as $parameter) {
58
            if ($parameter instanceof ArgumentParameterContract) {
59
                $this->arguments[] = $parameter;
60
            } elseif ($parameter instanceof OptionParameterContract) {
61
                $this->options[] = $parameter;
62
            }
63
        }
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    #[Override]
70
    public function getName(): string
71
    {
72
        return $this->name;
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    #[Override]
79
    public function withName(string $name): static
80
    {
81
        $new = clone $this;
82
83
        $new->name = $name;
84
85
        return $new;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    #[Override]
92
    public function getDescription(): string
93
    {
94
        return $this->description;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    #[Override]
101
    public function withDescription(string $description): static
102
    {
103
        $new = clone $this;
104
105
        $new->description = $description;
106
107
        return $new;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    #[Override]
114
    public function getHelpText(): MessageContract
115
    {
116
        return $this->helpText;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    #[Override]
123
    public function withHelpText(MessageContract $helpText): static
124
    {
125
        $new = clone $this;
126
127
        $new->helpText = $helpText;
128
129
        return $new;
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    #[Override]
136
    public function hasArguments(): bool
137
    {
138
        return $this->arguments !== [];
139
    }
140
141
    /**
142
     * @inheritDoc
143
     */
144
    #[Override]
145
    public function getArguments(): array
146
    {
147
        return $this->arguments;
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    #[Override]
154
    public function getArgument(string $name): ArgumentParameterContract|null
155
    {
156
        $arguments = array_filter($this->arguments, static fn (ArgumentParameterContract $argument) => $argument->getName() === $name);
157
158
        return reset($arguments) ?: null;
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    #[Override]
165
    public function withArguments(ArgumentParameterContract ...$arguments): static
166
    {
167
        $new = clone $this;
168
169
        $new->arguments = $arguments;
170
171
        return $new;
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    #[Override]
178
    public function withAddedArguments(ArgumentParameterContract ...$arguments): static
179
    {
180
        $new = clone $this;
181
182
        $new->arguments = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->arguments, $arguments) of type array<integer,array|arra...mentParameterContract>> is incompatible with the declared type Valkyrja\Cli\Routing\Dat...mentParameterContract[] of property $arguments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
            ...$this->arguments,
184
            ...$arguments,
185
        ];
186
187
        return $new;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    #[Override]
194
    public function hasOptions(): bool
195
    {
196
        return $this->options !== [];
197
    }
198
199
    /**
200
     * @inheritDoc
201
     */
202
    #[Override]
203
    public function getOptions(): array
204
    {
205
        return $this->options;
206
    }
207
208
    /**
209
     * @inheritDoc
210
     */
211
    #[Override]
212
    public function getOption(string $name): OptionParameterContract|null
213
    {
214
        $options = array_filter($this->options, static fn (OptionParameterContract $option) => $option->getName() === $name);
215
216
        return reset($options) ?: null;
217
    }
218
219
    /**
220
     * @inheritDoc
221
     */
222
    #[Override]
223
    public function withOptions(OptionParameterContract ...$options): static
224
    {
225
        $new = clone $this;
226
227
        $new->options = $options;
228
229
        return $new;
230
    }
231
232
    /**
233
     * @inheritDoc
234
     */
235
    #[Override]
236
    public function withAddedOptions(OptionParameterContract ...$options): static
237
    {
238
        $new = clone $this;
239
240
        $new->options = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->options, $options) of type array<integer,array|arra...tionParameterContract>> is incompatible with the declared type Valkyrja\Cli\Routing\Dat...tionParameterContract[] of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
241
            ...$this->options,
242
            ...$options,
243
        ];
244
245
        return $new;
246
    }
247
248
    /**
249
     * Get the command matched middleware.
250
     *
251
     * @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...
252
     */
253
    #[Override]
254
    public function getRouteMatchedMiddleware(): array
255
    {
256
        return $this->routeMatchedMiddleware;
257
    }
258
259
    /**
260
     * Create a new command with the specified command matched middleware.
261
     *
262
     * @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...
263
     */
264
    #[Override]
265
    public function withRouteMatchedMiddleware(string ...$middleware): static
266
    {
267
        $new = clone $this;
268
269
        $new->routeMatchedMiddleware = $middleware;
270
271
        return $new;
272
    }
273
274
    /**
275
     * Create a new command with added command matched middleware.
276
     *
277
     * @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...
278
     */
279
    #[Override]
280
    public function withAddedRouteMatchedMiddleware(string ...$middleware): static
281
    {
282
        $new = clone $this;
283
284
        $new->routeMatchedMiddleware = array_merge($this->routeMatchedMiddleware, $middleware);
285
286
        return $new;
287
    }
288
289
    /**
290
     * Get the command dispatched middleware.
291
     *
292
     * @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...
293
     */
294
    #[Override]
295
    public function getRouteDispatchedMiddleware(): array
296
    {
297
        return $this->routeDispatchedMiddleware;
298
    }
299
300
    /**
301
     * Create a new command with the specified command dispatched middleware.
302
     *
303
     * @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...
304
     */
305
    #[Override]
306
    public function withRouteDispatchedMiddleware(string ...$middleware): static
307
    {
308
        $new = clone $this;
309
310
        $new->routeDispatchedMiddleware = $middleware;
311
312
        return $new;
313
    }
314
315
    /**
316
     * Create a new command with added command dispatched middleware.
317
     *
318
     * @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...
319
     */
320
    #[Override]
321
    public function withAddedRouteDispatchedMiddleware(string ...$middleware): static
322
    {
323
        $new = clone $this;
324
325
        $new->routeDispatchedMiddleware = array_merge($this->routeDispatchedMiddleware, $middleware);
326
327
        return $new;
328
    }
329
330
    /**
331
     * Get the throwable caught middleware.
332
     *
333
     * @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...
334
     */
335
    #[Override]
336
    public function getThrowableCaughtMiddleware(): array
337
    {
338
        return $this->throwableCaughtMiddleware;
339
    }
340
341
    /**
342
     * Create a new command with the specified throwable caught middleware.
343
     *
344
     * @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...
345
     */
346
    #[Override]
347
    public function withThrowableCaughtMiddleware(string ...$middleware): static
348
    {
349
        $new = clone $this;
350
351
        $new->throwableCaughtMiddleware = $middleware;
352
353
        return $new;
354
    }
355
356
    /**
357
     * Create a new command with added throwable caught middleware.
358
     *
359
     * @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...
360
     */
361
    #[Override]
362
    public function withAddedThrowableCaughtMiddleware(string ...$middleware): static
363
    {
364
        $new = clone $this;
365
366
        $new->throwableCaughtMiddleware = array_merge($this->throwableCaughtMiddleware, $middleware);
367
368
        return $new;
369
    }
370
371
    /**
372
     * Get the exited middleware.
373
     *
374
     * @return class-string<ExitedMiddlewareContract>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddlewareContract>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddlewareContract>[].
Loading history...
375
     */
376
    #[Override]
377
    public function getExitedMiddleware(): array
378
    {
379
        return $this->exitedMiddleware;
380
    }
381
382
    /**
383
     * Create a new command with the specified exited middleware.
384
     *
385
     * @param class-string<ExitedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddlewareContract>.
Loading history...
386
     */
387
    #[Override]
388
    public function withExitedMiddleware(string ...$middleware): static
389
    {
390
        $new = clone $this;
391
392
        $new->exitedMiddleware = $middleware;
393
394
        return $new;
395
    }
396
397
    /**
398
     * Create a new command with added exited middleware.
399
     *
400
     * @param class-string<ExitedMiddlewareContract> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddlewareContract> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddlewareContract>.
Loading history...
401
     */
402
    #[Override]
403
    public function withAddedExitedMiddleware(string ...$middleware): static
404
    {
405
        $new = clone $this;
406
407
        $new->exitedMiddleware = array_merge($this->exitedMiddleware, $middleware);
408
409
        return $new;
410
    }
411
412
    /**
413
     * @inheritDoc
414
     */
415
    #[Override]
416
    public function getDispatch(): MethodDispatchContract
417
    {
418
        return $this->dispatch;
419
    }
420
421
    /**
422
     * @inheritDoc
423
     */
424
    #[Override]
425
    public function withDispatch(MethodDispatchContract $dispatch): static
426
    {
427
        $new = clone $this;
428
429
        $new->dispatch = $dispatch;
430
431
        return $new;
432
    }
433
}
434