Passed
Push — main ( fd247c...13b9f6 )
by Jeroen
16:48 queued 15:14
created

Route::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 8
nop 9
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 1
b 0
f 0

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
namespace Jerowork\RouteAttributeProvider\Api;
6
7
use Attribute;
8
use JsonSerializable;
9
10
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
11 1
final class Route implements JsonSerializable
12
{
13
    private string $pattern;
14
15
    /**
16
     * @var string[]
17
     */
18
    private array $methods;
19
20
    private ?string $name;
21
22
    /**
23
     * Set of middleware FQCNs.
24
     *
25
     * @var string[]
26
     */
27
    private array $middleware;
28
29
    private ?string $host;
30
31
    /**
32
     * @var string[]
33
     */
34
    private array $schemes;
35
36
    private ?int $httpPort;
37
    private ?int $httpsPort;
38
39
    /**
40
     * @var array<string, mixed>
41
     */
42
    private array $options;
43
44
    /**
45
     * @param string|string[] $method
46
     * @param string|string[] $middleware
47
     * @param string|string[] $schemes
48
     * @param array<string, mixed> $options
49
     */
50 16
    public function __construct(
51
        string $pattern,
52
        string | array $method = [RequestMethod::GET],
53
        string $name = null,
54
        string | array $middleware = [],
55
        string $host = null,
56
        string | array $schemes = [],
57
        int $httpPort = null,
58
        int $httpsPort = null,
59
        array $options = []
60
    ) {
61 16
        $this->pattern    = $pattern;
62 16
        $this->methods    = is_string($method) === true ? [$method] : $method;
0 ignored issues
show
introduced by
The condition is_string($method) === true is always false.
Loading history...
63 16
        $this->name       = $name;
64 16
        $this->middleware = is_string($middleware) === true ? [$middleware] : $middleware;
0 ignored issues
show
introduced by
The condition is_string($middleware) === true is always false.
Loading history...
65 16
        $this->host       = $host;
66 16
        $this->schemes    = is_string($schemes) === true ? [$schemes] : $schemes;
0 ignored issues
show
introduced by
The condition is_string($schemes) === true is always false.
Loading history...
67 16
        $this->httpPort   = $httpPort;
68 16
        $this->httpsPort  = $httpsPort;
69 16
        $this->options    = $options;
70 16
    }
71
72
    /**
73
     * @param array<string,mixed> $payload
74
     */
75 4
    public static function fromPayload(array $payload): self
76
    {
77 4
        return new self(
78 4
            $payload['pattern'],
79 4
            $payload['methods'],
80 4
            $payload['name'],
81 4
            $payload['middleware'],
82 4
            $payload['host'],
83 4
            $payload['schemes'],
84 4
            $payload['httpPort'],
85 4
            $payload['httpsPort'],
86 4
            $payload['options']
87
        );
88
    }
89
90 14
    public function getPattern(): string
91
    {
92 14
        return $this->pattern;
93
    }
94
95
    /**
96
     * @return string[]
97
     */
98 7
    public function getMethods(): array
99
    {
100 7
        return $this->methods;
101
    }
102
103 7
    public function getName(): ?string
104
    {
105 7
        return $this->name;
106
    }
107
108
    /**
109
     * @return string[]
110
     */
111 7
    public function getMiddleware(): array
112
    {
113 7
        return $this->middleware;
114
    }
115
116 1
    public function getHost(): ?string
117
    {
118 1
        return $this->host;
119
    }
120
121
    /**
122
     * @return string[]
123
     */
124 1
    public function getSchemes(): array
125
    {
126 1
        return $this->schemes;
127
    }
128
129 1
    public function getHttpPort(): ?int
130
    {
131 1
        return $this->httpPort;
132
    }
133
134 1
    public function getHttpsPort(): ?int
135
    {
136 1
        return $this->httpsPort;
137
    }
138
139
    /**
140
     * @return array<string, mixed>
141
     */
142 1
    public function getOptions(): array
143
    {
144 1
        return $this->options;
145
    }
146
147
    /**
148
     * @return array<string,mixed>
149
     */
150 6
    public function jsonSerialize(): array
151
    {
152
        return [
153 6
            'pattern'    => $this->getPattern(),
154 6
            'methods'    => $this->methods,
155 6
            'name'       => $this->name,
156 6
            'middleware' => $this->middleware,
157 6
            'host'       => $this->host,
158 6
            'schemes'    => $this->schemes,
159 6
            'httpPort'   => $this->httpPort,
160 6
            'httpsPort'  => $this->httpsPort,
161 6
            'options'    => $this->options,
162
        ];
163
    }
164
}
165