Route::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 16
c 0
b 0
f 0
rs 10
cc 4
nc 4
nop 9

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\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\Message;
18
use Valkyrja\Cli\Middleware\Contract\CommandDispatchedMiddleware;
19
use Valkyrja\Cli\Middleware\Contract\CommandMatchedMiddleware;
20
use Valkyrja\Cli\Middleware\Contract\ExitedMiddleware;
21
use Valkyrja\Cli\Middleware\Contract\ThrowableCaughtMiddleware;
22
use Valkyrja\Cli\Routing\Data\Contract\ArgumentParameter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Cli\Routing\Data\ArgumentParameter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use Valkyrja\Cli\Routing\Data\Contract\OptionParameter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Cli\Routing\Data\OptionParameter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
24
use Valkyrja\Cli\Routing\Data\Contract\Parameter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Cli\Routing\Data\Parameter. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
25
use Valkyrja\Cli\Routing\Data\Contract\Route as Contract;
26
use Valkyrja\Dispatcher\Data\Contract\MethodDispatch;
27
28
/**
29
 * Class Route.
30
 *
31
 * @author Melech Mizrachi
32
 */
33
class Route implements Contract
34
{
35
    /** @var ArgumentParameter[] */
36
    protected array $arguments = [];
37
38
    /** @var OptionParameter[] */
39
    protected array $options = [];
40
41
    /**
42
     * @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...
43
     * @param non-empty-string                            $description                 The description
44
     * @param Message                                     $helpText                    The help text
45
     * @param class-string<CommandMatchedMiddleware>[]    $commandMatchedMiddleware    The command matched middleware
46
     * @param class-string<CommandDispatchedMiddleware>[] $commandDispatchedMiddleware The command dispatched middleware
47
     * @param class-string<ThrowableCaughtMiddleware>[]   $throwableCaughtMiddleware   The throwable caught middleware
48
     * @param class-string<ExitedMiddleware>[]            $exitedMiddleware            The exited middleware
49
     * @param Parameter[]                                 $parameters                  The parameters
50
     */
51
    public function __construct(
52
        protected string $name,
53
        protected string $description,
54
        protected Message $helpText,
55
        protected MethodDispatch $dispatch,
56
        protected array $commandMatchedMiddleware = [],
57
        protected array $commandDispatchedMiddleware = [],
58
        protected array $throwableCaughtMiddleware = [],
59
        protected array $exitedMiddleware = [],
60
        array $parameters = [],
61
    ) {
62
        foreach ($parameters as $parameter) {
63
            if ($parameter instanceof ArgumentParameter) {
64
                $this->arguments[] = $parameter;
65
            } elseif ($parameter instanceof OptionParameter) {
66
                $this->options[] = $parameter;
67
            }
68
        }
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    #[Override]
75
    public function getName(): string
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    #[Override]
84
    public function withName(string $name): static
85
    {
86
        $new = clone $this;
87
88
        $new->name = $name;
89
90
        return $new;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    #[Override]
97
    public function getDescription(): string
98
    {
99
        return $this->description;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    #[Override]
106
    public function withDescription(string $description): static
107
    {
108
        $new = clone $this;
109
110
        $new->description = $description;
111
112
        return $new;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    #[Override]
119
    public function getHelpText(): Message
120
    {
121
        return $this->helpText;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    #[Override]
128
    public function withHelpText(Message $helpText): static
129
    {
130
        $new = clone $this;
131
132
        $new->helpText = $helpText;
133
134
        return $new;
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    #[Override]
141
    public function hasArguments(): bool
142
    {
143
        return $this->arguments !== [];
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    #[Override]
150
    public function getArguments(): array
151
    {
152
        return $this->arguments;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    #[Override]
159
    public function getArgument(string $name): ArgumentParameter|null
160
    {
161
        $arguments = array_filter($this->arguments, static fn (ArgumentParameter $argument) => $argument->getName() === $name);
162
163
        return reset($arguments) ?: null;
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    #[Override]
170
    public function withArguments(ArgumentParameter ...$arguments): static
171
    {
172
        $new = clone $this;
173
174
        $new->arguments = $arguments;
175
176
        return $new;
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    #[Override]
183
    public function withAddedArguments(ArgumentParameter ...$arguments): static
184
    {
185
        $new = clone $this;
186
187
        $new->arguments = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->arguments, $arguments) of type array<integer,array|arra...act\ArgumentParameter>> is incompatible with the declared type Valkyrja\Cli\Routing\Dat...act\ArgumentParameter[] 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...
188
            ...$this->arguments,
189
            ...$arguments,
190
        ];
191
192
        return $new;
193
    }
194
195
    /**
196
     * @inheritDoc
197
     */
198
    #[Override]
199
    public function hasOptions(): bool
200
    {
201
        return $this->options !== [];
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    #[Override]
208
    public function getOptions(): array
209
    {
210
        return $this->options;
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216
    #[Override]
217
    public function getOption(string $name): OptionParameter|null
218
    {
219
        $options = array_filter($this->options, static fn (OptionParameter $option) => $option->getName() === $name);
220
221
        return reset($options) ?: null;
222
    }
223
224
    /**
225
     * @inheritDoc
226
     */
227
    #[Override]
228
    public function withOptions(OptionParameter ...$options): static
229
    {
230
        $new = clone $this;
231
232
        $new->options = $options;
233
234
        return $new;
235
    }
236
237
    /**
238
     * @inheritDoc
239
     */
240
    #[Override]
241
    public function withAddedOptions(OptionParameter ...$options): static
242
    {
243
        $new = clone $this;
244
245
        $new->options = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->options, $options) of type array<integer,array|arra...tract\OptionParameter>> is incompatible with the declared type Valkyrja\Cli\Routing\Dat...tract\OptionParameter[] 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...
246
            ...$this->options,
247
            ...$options,
248
        ];
249
250
        return $new;
251
    }
252
253
    /**
254
     * Get the command matched middleware.
255
     *
256
     * @return class-string<CommandMatchedMiddleware>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandMatchedMiddleware>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandMatchedMiddleware>[].
Loading history...
257
     */
258
    #[Override]
259
    public function getCommandMatchedMiddleware(): array
260
    {
261
        return $this->commandMatchedMiddleware;
262
    }
263
264
    /**
265
     * Create a new command with the specified command matched middleware.
266
     *
267
     * @param class-string<CommandMatchedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandMatchedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandMatchedMiddleware>.
Loading history...
268
     *
269
     * @return static
270
     */
271
    #[Override]
272
    public function withCommandMatchedMiddleware(string ...$middleware): static
273
    {
274
        $new = clone $this;
275
276
        $new->commandMatchedMiddleware = $middleware;
277
278
        return $new;
279
    }
280
281
    /**
282
     * Create a new command with added command matched middleware.
283
     *
284
     * @param class-string<CommandMatchedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandMatchedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandMatchedMiddleware>.
Loading history...
285
     *
286
     * @return static
287
     */
288
    #[Override]
289
    public function withAddedCommandMatchedMiddleware(string ...$middleware): static
290
    {
291
        $new = clone $this;
292
293
        $new->commandMatchedMiddleware = array_merge($this->commandMatchedMiddleware, $middleware);
294
295
        return $new;
296
    }
297
298
    /**
299
     * Get the command dispatched middleware.
300
     *
301
     * @return class-string<CommandDispatchedMiddleware>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandDispatchedMiddleware>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandDispatchedMiddleware>[].
Loading history...
302
     */
303
    #[Override]
304
    public function getCommandDispatchedMiddleware(): array
305
    {
306
        return $this->commandDispatchedMiddleware;
307
    }
308
309
    /**
310
     * Create a new command with the specified command dispatched middleware.
311
     *
312
     * @param class-string<CommandDispatchedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandDispatchedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandDispatchedMiddleware>.
Loading history...
313
     *
314
     * @return static
315
     */
316
    #[Override]
317
    public function withCommandDispatchedMiddleware(string ...$middleware): static
318
    {
319
        $new = clone $this;
320
321
        $new->commandDispatchedMiddleware = $middleware;
322
323
        return $new;
324
    }
325
326
    /**
327
     * Create a new command with added command dispatched middleware.
328
     *
329
     * @param class-string<CommandDispatchedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CommandDispatchedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CommandDispatchedMiddleware>.
Loading history...
330
     *
331
     * @return static
332
     */
333
    #[Override]
334
    public function withAddedCommandDispatchedMiddleware(string ...$middleware): static
335
    {
336
        $new = clone $this;
337
338
        $new->commandDispatchedMiddleware = array_merge($this->commandDispatchedMiddleware, $middleware);
339
340
        return $new;
341
    }
342
343
    /**
344
     * Get the throwable caught middleware.
345
     *
346
     * @return class-string<ThrowableCaughtMiddleware>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddleware>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddleware>[].
Loading history...
347
     */
348
    #[Override]
349
    public function getThrowableCaughtMiddleware(): array
350
    {
351
        return $this->throwableCaughtMiddleware;
352
    }
353
354
    /**
355
     * Create a new command with the specified throwable caught middleware.
356
     *
357
     * @param class-string<ThrowableCaughtMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddleware>.
Loading history...
358
     *
359
     * @return static
360
     */
361
    #[Override]
362
    public function withThrowableCaughtMiddleware(string ...$middleware): static
363
    {
364
        $new = clone $this;
365
366
        $new->throwableCaughtMiddleware = $middleware;
367
368
        return $new;
369
    }
370
371
    /**
372
     * Create a new command with added throwable caught middleware.
373
     *
374
     * @param class-string<ThrowableCaughtMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ThrowableCaughtMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ThrowableCaughtMiddleware>.
Loading history...
375
     *
376
     * @return static
377
     */
378
    #[Override]
379
    public function withAddedThrowableCaughtMiddleware(string ...$middleware): static
380
    {
381
        $new = clone $this;
382
383
        $new->throwableCaughtMiddleware = array_merge($this->throwableCaughtMiddleware, $middleware);
384
385
        return $new;
386
    }
387
388
    /**
389
     * Get the exited middleware.
390
     *
391
     * @return class-string<ExitedMiddleware>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddleware>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddleware>[].
Loading history...
392
     */
393
    #[Override]
394
    public function getExitedMiddleware(): array
395
    {
396
        return $this->exitedMiddleware;
397
    }
398
399
    /**
400
     * Create a new command with the specified exited middleware.
401
     *
402
     * @param class-string<ExitedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddleware>.
Loading history...
403
     *
404
     * @return static
405
     */
406
    #[Override]
407
    public function withExitedMiddleware(string ...$middleware): static
408
    {
409
        $new = clone $this;
410
411
        $new->exitedMiddleware = $middleware;
412
413
        return $new;
414
    }
415
416
    /**
417
     * Create a new command with added exited middleware.
418
     *
419
     * @param class-string<ExitedMiddleware> ...$middleware The middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ExitedMiddleware> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ExitedMiddleware>.
Loading history...
420
     *
421
     * @return static
422
     */
423
    #[Override]
424
    public function withAddedExitedMiddleware(string ...$middleware): static
425
    {
426
        $new = clone $this;
427
428
        $new->exitedMiddleware = array_merge($this->exitedMiddleware, $middleware);
429
430
        return $new;
431
    }
432
433
    /**
434
     * @inheritDoc
435
     */
436
    #[Override]
437
    public function getDispatch(): MethodDispatch
438
    {
439
        return $this->dispatch;
440
    }
441
442
    /**
443
     * @inheritDoc
444
     */
445
    #[Override]
446
    public function withDispatch(MethodDispatch $dispatch): static
447
    {
448
        $new = clone $this;
449
450
        $new->dispatch = $dispatch;
451
452
        return $new;
453
    }
454
}
455