Passed
Push — master ( 71600f...612a33 )
by Melech
14:37
created

Route::__construct()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 43
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
c 1
b 0
f 0
nc 33
nop 9
dl 0
loc 43
rs 8.4444

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\Routing\Attributes;
15
16
use Attribute;
17
use Valkyrja\Routing\Exceptions\InvalidRoutePath;
18
use Valkyrja\Routing\Message;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Routing\Attributes\Message. 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...
19
use Valkyrja\Routing\Models\Parameter;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Routing\Attributes\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...
20
use Valkyrja\Routing\Models\Route as Model;
21
22
/**
23
 * Attribute Route.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
28
class Route extends Model
29
{
30
    /**
31
     * @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...
32
     * @param Parameter[]|null             $parameters The parameters
33
     * @param class-string<Message>[]|null $messages   The messages
34
     *
35
     * @throws InvalidRoutePath
36
     */
37
    public function __construct(
38
        string $path,
39
        string|null $name = null,
40
        array|null $methods = null,
41
        array|null $parameters = null,
42
        array|null $middleware = null,
43
        array|null $messages = null,
44
        bool|null $secure = null,
45
        string|null $to = null,
46
        int|null $code = null,
47
    ) {
48
        $this->path = $path;
49
50
        /** @psalm-suppress TypeDoesNotContainType Not everyone will use Psalm :) */
51
        if ($path === '') {
52
            throw new InvalidRoutePath("Path must be a non-empty-string.");
53
        }
54
55
        if ($name !== null && $name !== '') {
56
            $this->name = $name;
57
        } else {
58
            $this->name = $path;
59
        }
60
61
        if ($methods !== null) {
62
            $this->methods = $methods;
63
        }
64
65
        if ($parameters !== null) {
66
            $this->setParameters($parameters);
67
        }
68
69
        if ($secure !== null) {
70
            $this->secure = $secure;
71
        }
72
73
        if ($to !== '') {
74
            $this->setTo($to);
75
        }
76
77
        $this->setMiddleware($middleware);
78
        $this->setCode($code);
79
        $this->setMessages($messages);
80
    }
81
}
82