Error::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 10
dl 0
loc 26
ccs 12
cts 12
cp 1
crap 2
rs 9.504
c 0
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 declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
22
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface;
23
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
24
25
/**
26
 * @package Neomerx\JsonApi
27
 *
28
 * @SuppressWarnings(PHPMD.StaticAccess)
29
 */
30
class Error implements ErrorInterface
31
{
32
    /**
33
     * @var int|string|null
34
     */
35
    private $index;
36
37
    /**
38
     * @var null|iterable
39
     */
40
    private $links;
41
42
    /**
43
     * @var null|iterable
44
     */
45
    private $typeLinks;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $status;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $code;
56
57
    /**
58
     * @var string|null
59
     */
60
    private $title;
61
62
    /**
63
     * @var string|null
64
     */
65
    private $detail;
66
67
    /**
68
     * @var array|null
69
     */
70
    private $source;
71
72
    /**
73
     * @var bool
74
     */
75
    private $hasMeta;
76
77
    /**
78
     * @var mixed
79
     */
80
    private $meta;
81
82
    /**
83
     * @param int|string|null    $idx
84
     * @param LinkInterface|null $aboutLink
85
     * @param iterable|null      $typeLinks
86
     * @param string|null        $status
87
     * @param string|null        $code
88
     * @param string|null        $title
89
     * @param string|null        $detail
90
     * @param array|null         $source
91
     * @param bool               $hasMeta
92
     * @param mixed              $meta
93
     *
94
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
95
     * @SuppressWarnings(PHPMD.IfStatementAssignment)
96
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
97
     */
98 30
    public function __construct(
99
        $idx = null,
100
        LinkInterface $aboutLink = null,
101
        ?iterable $typeLinks = null,
102
        string $status = null,
103
        string $code = null,
104
        string $title = null,
105
        string $detail = null,
106
        array $source = null,
107
        bool $hasMeta = false,
108
        $meta = null
109
    ) {
110
        $this
111 30
            ->setId($idx)
112 30
            ->setLink(DocumentInterface::KEYWORD_ERRORS_ABOUT, $aboutLink)
113 30
            ->setTypeLinks($typeLinks)
114 30
            ->setStatus($status)
115 30
            ->setCode($code)
116 30
            ->setTitle($title)
117 30
            ->setDetail($detail)
118 30
            ->setSource($source);
119
120 30
        if (($this->hasMeta = $hasMeta) === true) {
121 4
            $this->setMeta($meta);
122
        }
123 30
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 6
    public function getId()
129
    {
130 6
        return $this->index;
131
    }
132
133
    /**
134
     * @param string|int|null $index
135
     *
136
     * @return self
137
     */
138 30
    public function setId($index): self
139
    {
140 30
        \assert($index === null || \is_int($index) === true || \is_string($index) === true);
141
142 30
        $this->index = $index;
143
144 30
        return $this;
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150 6
    public function getLinks(): ?iterable
151
    {
152 6
        return $this->links;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->links; of type null|Neomerx\JsonApi\Schema\iterable adds the type Neomerx\JsonApi\Schema\iterable to the return on line 152 which is incompatible with the return type declared by the interface Neomerx\JsonApi\Contract...rrorInterface::getLinks of type null|Neomerx\JsonApi\Contracts\Schema\iterable.
Loading history...
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158 6
    public function getTypeLinks(): ?iterable
159
    {
160 6
        return $this->typeLinks;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->typeLinks; of type null|Neomerx\JsonApi\Schema\iterable adds the type Neomerx\JsonApi\Schema\iterable to the return on line 160 which is incompatible with the return type declared by the interface Neomerx\JsonApi\Contract...Interface::getTypeLinks of type null|Neomerx\JsonApi\Contracts\Schema\iterable.
Loading history...
161
    }
162
163
    /**
164
     * @param string             $name
165
     * @param LinkInterface|null $link
166
     *
167
     * @return self
168
     *
169
     * @SuppressWarnings(PHPMD.ElseExpression)
170
     */
171 30
    public function setLink(string $name, ?LinkInterface $link): self
172
    {
173 30
        if ($link !== null) {
174 4
            $this->links[$name] = $link;
175
        } else {
176 26
            unset($this->links[$name]);
177
        }
178
179 30
        return $this;
180
    }
181
182
    /**
183
     * @param iterable|null $typeLinks
184
     *
185
     * @return self
186
     */
187 30
    public function setTypeLinks(?iterable $typeLinks): self
188
    {
189 30
        $this->typeLinks = $typeLinks;
190
191 30
        return $this;
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197 6
    public function getStatus(): ?string
198
    {
199 6
        return $this->status;
200
    }
201
202
    /**
203
     * @param string|null $status
204
     *
205
     * @return self
206
     */
207 30
    public function setStatus(?string $status): self
208
    {
209 30
        $this->status = $status;
210
211 30
        return $this;
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217 6
    public function getCode(): ?string
218
    {
219 6
        return $this->code;
220
    }
221
222
    /**
223
     * @param string|null $code
224
     *
225
     * @return self
226
     */
227 30
    public function setCode(?string $code): self
228
    {
229 30
        $this->code = $code;
230
231 30
        return $this;
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237 7
    public function getTitle(): ?string
238
    {
239 7
        return $this->title;
240
    }
241
242
    /**
243
     * @param null|string $title
244
     *
245
     * @return self
246
     */
247 30
    public function setTitle(?string $title): self
248
    {
249 30
        $this->title = $title;
250
251 30
        return $this;
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257 6
    public function getDetail(): ?string
258
    {
259 6
        return $this->detail;
260
    }
261
262
    /**
263
     * @param null|string $detail
264
     *
265
     * @return self
266
     */
267 30
    public function setDetail(?string $detail): self
268
    {
269 30
        $this->detail = $detail;
270
271 30
        return $this;
272
    }
273
274
    /**
275
     * @inheritdoc
276
     */
277 16
    public function getSource(): ?array
278
    {
279 16
        return $this->source;
280
    }
281
282
    /**
283
     * @param array|null $source
284
     *
285
     * @return self
286
     */
287 30
    public function setSource(?array $source): self
288
    {
289 30
        $this->source = $source;
290
291 30
        return $this;
292
    }
293
294
    /**
295
     * @inheritdoc
296
     */
297 6
    public function hasMeta(): bool
298
    {
299 6
        return $this->hasMeta;
300
    }
301
302
    /**
303
     * @inheritdoc
304
     */
305 4
    public function getMeta()
306
    {
307 4
        return $this->meta;
308
    }
309
310
    /**
311
     * @param mixed|null $meta
312
     *
313
     * @return self
314
     */
315 4
    public function setMeta($meta): self
316
    {
317 4
        $this->hasMeta = true;
318 4
        $this->meta    = $meta;
319
320 4
        return $this;
321
    }
322
}
323