Passed
Pull Request — master (#33)
by Melech
03:22
created

Route::__construct()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 47
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
c 1
b 0
f 0
nc 512
nop 9
dl 0
loc 47
rs 4.1777

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\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...
18
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...
19
use Valkyrja\Routing\Models\Route as Model;
20
21
/**
22
 * Attribute Route.
23
 *
24
 * @author Melech Mizrachi
25
 */
26
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
27
class Route extends Model
28
{
29
    /**
30
     * @param Parameter[]|null $parameters The parameters
31
     * @param class-string<Message>[]|null $messages   The messages
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Message>[]|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Message>[]|null.
Loading history...
32
     */
33
    public function __construct(
34
        string $path,
35
        string $name = null,
36
        array $methods = null,
37
        array $parameters = null,
38
        array $middleware = null,
39
        array $messages = null,
40
        bool $secure = null,
41
        string $to = null,
42
        int $code = null,
43
    ) {
44
        $this->path = $path;
45
46
        if ($path) {
47
            $this->name = $path;
48
        }
49
50
        if ($name) {
51
            $this->name = $name;
52
        }
53
54
        if ($methods) {
55
            $this->methods = $methods;
56
        }
57
58
        if ($parameters) {
59
            $this->setParameters($parameters);
60
        }
61
62
        if ($middleware) {
63
            $this->setMiddleware($middleware);
64
        }
65
66
        if ($secure) {
67
            $this->secure = $secure;
68
        }
69
70
        if ($to) {
71
            $this->setTo($to);
72
        }
73
74
        if ($code) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $code of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
75
            $this->code = $code;
76
        }
77
78
        if ($messages) {
79
            $this->setMessages($messages);
80
        }
81
    }
82
}
83