Completed
Branch master (ee91f8)
by
unknown
08:26
created

ErrorCollection   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 443
Duplicated Lines 32.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 29
c 2
b 0
f 0
lcom 1
cbo 1
dl 142
loc 443
ccs 79
cts 79
cp 1
rs 10

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getArrayCopy() 0 4 1
A add() 0 6 1
A addDataError() 13 13 1
A addDataTypeError() 13 13 1
A addDataIdError() 13 13 1
A addDataAttributeError() 14 14 1
A addRelationshipsError() 13 13 1
A addRelationshipError() 14 14 1
A addRelationshipTypeError() 14 14 1
A addRelationshipIdError() 14 14 1
A addQueryParameterError() 17 17 1
A addResourceError() 17 17 1
A getPathToData() 0 4 1
A getPathToType() 0 4 1
A getPathToId() 0 4 1
A getPathToAttribute() 0 4 1
A getPathToRelationships() 0 4 1
A getPathToRelationship() 0 4 1
A getPathToRelationshipType() 0 5 1
A getPathToRelationshipId() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Neomerx\JsonApi\Exceptions;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Countable;
20
use \ArrayAccess;
21
use \ArrayObject;
22
use \Serializable;
23
use \IteratorAggregate;
24
use \Neomerx\JsonApi\Document\Error;
25
use \Neomerx\JsonApi\Contracts\Document\LinkInterface;
26
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;
27
28
/**
29
 * @package Neomerx\JsonApi
30
 */
31
class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable
32
{
33
    /**
34
     * @var ArrayObject
35
     */
36
    private $items;
37
38
    /**
39
     * ErrorCollection constructor.
40
     */
41 39
    public function __construct()
42
    {
43 39
        $this->items = new ArrayObject();
44 39
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 10
    public function getIterator()
50
    {
51 10
        return $this->items->getIterator();
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 26
    public function count()
58
    {
59 26
        return $this->items->count();
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function serialize()
66
    {
67 1
        return $this->items->serialize();
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 1
    public function unserialize($serialized)
74
    {
75 1
        $this->items->unserialize($serialized);
76 1
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 1
    public function offsetExists($offset)
82
    {
83 1
        return $this->items->offsetExists($offset);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     *
89
     * @return Error
90
     */
91 15
    public function offsetGet($offset)
92
    {
93 15
        return $this->items->offsetGet($offset);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 11
    public function offsetSet($offset, $value)
100
    {
101 11
        $this->items->offsetSet($offset, $value);
102 11
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 1
    public function offsetUnset($offset)
108
    {
109 1
        $this->items->offsetUnset($offset);
110 1
    }
111
112
    /**
113
     * @return Error[]
114
     */
115 4
    public function getArrayCopy()
116
    {
117 4
        return $this->items->getArrayCopy();
118
    }
119
120
    /**
121
     * @param Error $error
122
     *
123
     * @return $this
124
     */
125 20
    public function add(Error $error)
126
    {
127 20
        $this->items->append($error);
128
129 20
        return $this;
130
    }
131
132
    /**
133
     * @param string             $title
134
     * @param string|null        $detail
135
     * @param int|string|null    $status
136
     * @param int|string|null    $idx
137
     * @param LinkInterface|null $aboutLink
138
     * @param int|string|null    $code
139
     * @param mixed|null         $meta
140
     *
141
     * @return $this
142
     */
143 2 View Code Duplication
    public function addDataError(
144
        $title,
145
        $detail = null,
146
        $status = null,
147
        $idx = null,
148
        LinkInterface $aboutLink = null,
149
        $code = null,
150
        $meta = null
151
    ) {
152 2
        $pointer = $this->getPathToData();
153
154 2
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
155
    }
156
157
    /**
158
     * @param string             $title
159
     * @param string|null        $detail
160
     * @param int|string|null    $status
161
     * @param int|string|null    $idx
162
     * @param LinkInterface|null $aboutLink
163
     * @param int|string|null    $code
164
     * @param mixed|null         $meta
165
     *
166
     * @return $this
167
     */
168 1 View Code Duplication
    public function addDataTypeError(
169
        $title,
170
        $detail = null,
171
        $status = null,
172
        $idx = null,
173
        LinkInterface $aboutLink = null,
174
        $code = null,
175
        $meta = null
176
    ) {
177 1
        $pointer = $this->getPathToType();
178
179 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
180
    }
181
182
    /**
183
     * @param string             $title
184
     * @param string|null        $detail
185
     * @param int|string|null    $status
186
     * @param int|string|null    $idx
187
     * @param LinkInterface|null $aboutLink
188
     * @param int|string|null    $code
189
     * @param mixed|null         $meta
190
     *
191
     * @return $this
192
     */
193 1 View Code Duplication
    public function addDataIdError(
194
        $title,
195
        $detail = null,
196
        $status = null,
197
        $idx = null,
198
        LinkInterface $aboutLink = null,
199
        $code = null,
200
        $meta = null
201
    ) {
202 1
        $pointer = $this->getPathToId();
203
204 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
205
    }
206
207
    /**
208
     * @param string             $name
209
     * @param string             $title
210
     * @param string|null        $detail
211
     * @param int|string|null    $status
212
     * @param int|string|null    $idx
213
     * @param LinkInterface|null $aboutLink
214
     * @param int|string|null    $code
215
     * @param mixed|null         $meta
216
     *
217
     * @return $this
218
     */
219 1 View Code Duplication
    public function addDataAttributeError(
220
        $name,
221
        $title,
222
        $detail = null,
223
        $status = null,
224
        $idx = null,
225
        LinkInterface $aboutLink = null,
226
        $code = null,
227
        $meta = null
228
    ) {
229 1
        $pointer = $this->getPathToAttribute($name);
230
231 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
232
    }
233
234
    /**
235
     * @param string             $title
236
     * @param string|null        $detail
237
     * @param int|string|null    $status
238
     * @param int|string|null    $idx
239
     * @param LinkInterface|null $aboutLink
240
     * @param int|string|null    $code
241
     * @param mixed|null         $meta
242
     *
243
     * @return $this
244
     */
245 1 View Code Duplication
    public function addRelationshipsError(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
        $title,
247
        $detail = null,
248
        $status = null,
249
        $idx = null,
250
        LinkInterface $aboutLink = null,
251
        $code = null,
252
        $meta = null
253
    ) {
254 1
        $pointer = $this->getPathToRelationships();
255
256 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
257
    }
258
259
    /**
260
     * @param string             $name
261
     * @param string             $title
262
     * @param string|null        $detail
263
     * @param int|string|null    $status
264
     * @param int|string|null    $idx
265
     * @param LinkInterface|null $aboutLink
266
     * @param int|string|null    $code
267
     * @param mixed|null         $meta
268
     *
269
     * @return $this
270
     */
271 1 View Code Duplication
    public function addRelationshipError(
272
        $name,
273
        $title,
274
        $detail = null,
275
        $status = null,
276
        $idx = null,
277
        LinkInterface $aboutLink = null,
278
        $code = null,
279
        $meta = null
280
    ) {
281 1
        $pointer = $this->getPathToRelationship($name);
282
283 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
284
    }
285
286
    /**
287
     * @param string             $name
288
     * @param string             $title
289
     * @param string|null        $detail
290
     * @param int|string|null    $status
291
     * @param int|string|null    $idx
292
     * @param LinkInterface|null $aboutLink
293
     * @param int|string|null    $code
294
     * @param mixed|null         $meta
295
     *
296
     * @return $this
297
     */
298 1 View Code Duplication
    public function addRelationshipTypeError(
299
        $name,
300
        $title,
301
        $detail = null,
302
        $status = null,
303
        $idx = null,
304
        LinkInterface $aboutLink = null,
305
        $code = null,
306
        $meta = null
307
    ) {
308 1
        $pointer = $this->getPathToRelationshipType($name);
309
310 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
311
    }
312
313
    /**
314
     * @param string             $name
315
     * @param string             $title
316
     * @param string|null        $detail
317
     * @param int|string|null    $status
318
     * @param int|string|null    $idx
319
     * @param LinkInterface|null $aboutLink
320
     * @param int|string|null    $code
321
     * @param mixed|null         $meta
322
     *
323
     * @return $this
324
     */
325 1 View Code Duplication
    public function addRelationshipIdError(
326
        $name,
327
        $title,
328
        $detail = null,
329
        $status = null,
330
        $idx = null,
331
        LinkInterface $aboutLink = null,
332
        $code = null,
333
        $meta = null
334
    ) {
335 1
        $pointer = $this->getPathToRelationshipId($name);
336
337 1
        return $this->addResourceError($title, $pointer, $detail, $status, $idx, $aboutLink, $code, $meta);
338
    }
339
340
    /**
341
     * @param string             $name
342
     * @param string             $title
343
     * @param string|null        $detail
344
     * @param int|string|null    $status
345
     * @param int|string|null    $idx
346
     * @param LinkInterface|null $aboutLink
347
     * @param int|string|null    $code
348
     * @param mixed|null         $meta
349
     *
350
     * @return $this
351
     */
352 8 View Code Duplication
    public function addQueryParameterError(
353
        $name,
354
        $title,
355
        $detail = null,
356
        $status = null,
357
        $idx = null,
358
        LinkInterface $aboutLink = null,
359
        $code = null,
360
        $meta = null
361
    ) {
362 8
        $source = [Error::SOURCE_PARAMETER => $name];
363 8
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
364
365 8
        $this->add($error);
366
367 8
        return $this;
368
    }
369
370
    /** @noinspection PhpTooManyParametersInspection
371
     * @param string             $title
372
     * @param string             $pointer
373
     * @param string|null        $detail
374
     * @param int|string|null    $status
375
     * @param int|string|null    $idx
376
     * @param LinkInterface|null $aboutLink
377
     * @param int|string|null    $code
378
     * @param mixed|null         $meta
379
     *
380
     * @return $this
381
     */
382 9 View Code Duplication
    protected function addResourceError(
383
        $title,
384
        $pointer,
385
        $detail = null,
386
        $status = null,
387
        $idx = null,
388
        LinkInterface $aboutLink = null,
389
        $code = null,
390
        $meta = null
391
    ) {
392 9
        $source = [Error::SOURCE_POINTER => $pointer];
393 9
        $error  = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
394
395 9
        $this->add($error);
396
397 9
        return $this;
398
    }
399
400
    /**
401
     * @return string
402
     */
403 9
    protected function getPathToData()
404
    {
405 9
        return '/' . DocumentInterface::KEYWORD_DATA;
406
    }
407
408
    /**
409
     * @return string
410
     */
411 1
    protected function getPathToType()
412
    {
413 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_TYPE;
414
    }
415
416
    /**
417
     * @return string
418
     */
419 1
    protected function getPathToId()
420
    {
421 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ID;
422
    }
423
424
    /**
425
     * @param string $name
426
     *
427
     * @return string
428
     */
429 1
    protected function getPathToAttribute($name)
430
    {
431 1
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_ATTRIBUTES . '/' . $name;
432
    }
433
434
    /**
435
     * @return string
436
     */
437 4
    protected function getPathToRelationships()
438
    {
439 4
        return $this->getPathToData() . '/' . DocumentInterface::KEYWORD_RELATIONSHIPS;
440
    }
441
442
    /**
443
     * @param string $name
444
     *
445
     * @return string
446
     */
447 3
    protected function getPathToRelationship($name)
448
    {
449 3
        return $this->getPathToRelationships() . '/' . $name;
450
    }
451
452
    /**
453
     * @param string $name
454
     *
455
     * @return string
456
     */
457 1
    protected function getPathToRelationshipType($name)
458
    {
459 1
        return $this->getPathToRelationship($name) . '/' .
460 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_TYPE;
461
    }
462
463
    /**
464
     * @param string $name
465
     *
466
     * @return string
467
     */
468 1
    protected function getPathToRelationshipId($name)
469
    {
470 1
        return $this->getPathToRelationship($name) . '/' .
471 1
            DocumentInterface::KEYWORD_DATA . '/' . DocumentInterface::KEYWORD_ID;
472
    }
473
}
474