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; |
||
34 | class ErrorCollection implements IteratorAggregate, ArrayAccess, Serializable, Countable |
||
35 | { |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $items = []; |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 10 | public function getIterator() |
|
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | 27 | public function count() |
|
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | 1 | public function serialize() |
|
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | */ |
||
68 | 1 | public function unserialize($serialized) |
|
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | 1 | public function offsetExists($offset) |
|
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | * |
||
84 | * @return ErrorInterface |
||
85 | */ |
||
86 | 16 | public function offsetGet($offset) |
|
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 11 | public function offsetSet($offset, $value) |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 1 | public function offsetUnset($offset) |
|
106 | |||
107 | /** |
||
108 | * @return ErrorInterface[] |
||
109 | */ |
||
110 | 4 | public function getArrayCopy() |
|
114 | |||
115 | /** |
||
116 | * @param ErrorInterface $error |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | 23 | public function add(ErrorInterface $error) |
|
126 | |||
127 | /** |
||
128 | * @param string $title |
||
129 | * @param string|null $detail |
||
130 | * @param int|string|null $status |
||
131 | * @param int|string|null $idx |
||
132 | * @param LinkInterface|null $aboutLink |
||
133 | * @param int|string|null $code |
||
134 | * @param mixed|null $meta |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | 2 | View Code Duplication | public function addDataError( |
151 | |||
152 | /** |
||
153 | * @param string $title |
||
154 | * @param string|null $detail |
||
155 | * @param int|string|null $status |
||
156 | * @param int|string|null $idx |
||
157 | * @param LinkInterface|null $aboutLink |
||
158 | * @param int|string|null $code |
||
159 | * @param mixed|null $meta |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | 1 | View Code Duplication | public function addDataTypeError( |
176 | |||
177 | /** |
||
178 | * @param string $title |
||
179 | * @param string|null $detail |
||
180 | * @param int|string|null $status |
||
181 | * @param int|string|null $idx |
||
182 | * @param LinkInterface|null $aboutLink |
||
183 | * @param int|string|null $code |
||
184 | * @param mixed|null $meta |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | 1 | View Code Duplication | public function addDataIdError( |
201 | |||
202 | /** |
||
203 | * @param string $title |
||
204 | * @param string|null $detail |
||
205 | * @param int|string|null $status |
||
206 | * @param int|string|null $idx |
||
207 | * @param LinkInterface|null $aboutLink |
||
208 | * @param int|string|null $code |
||
209 | * @param mixed|null $meta |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | 1 | View Code Duplication | public function addAttributesError( |
226 | |||
227 | /** |
||
228 | * @param string $name |
||
229 | * @param string $title |
||
230 | * @param string|null $detail |
||
231 | * @param int|string|null $status |
||
232 | * @param int|string|null $idx |
||
233 | * @param LinkInterface|null $aboutLink |
||
234 | * @param int|string|null $code |
||
235 | * @param mixed|null $meta |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | 1 | View Code Duplication | public function addDataAttributeError( |
253 | |||
254 | /** |
||
255 | * @param string $title |
||
256 | * @param string|null $detail |
||
257 | * @param int|string|null $status |
||
258 | * @param int|string|null $idx |
||
259 | * @param LinkInterface|null $aboutLink |
||
260 | * @param int|string|null $code |
||
261 | * @param mixed|null $meta |
||
262 | * |
||
263 | * @return $this |
||
264 | */ |
||
265 | 1 | View Code Duplication | public function addRelationshipsError( |
278 | |||
279 | /** |
||
280 | * @param string $name |
||
281 | * @param string $title |
||
282 | * @param string|null $detail |
||
283 | * @param int|string|null $status |
||
284 | * @param int|string|null $idx |
||
285 | * @param LinkInterface|null $aboutLink |
||
286 | * @param int|string|null $code |
||
287 | * @param mixed|null $meta |
||
288 | * |
||
289 | * @return $this |
||
290 | */ |
||
291 | 1 | View Code Duplication | public function addRelationshipError( |
305 | |||
306 | /** |
||
307 | * @param string $name |
||
308 | * @param string $title |
||
309 | * @param string|null $detail |
||
310 | * @param int|string|null $status |
||
311 | * @param int|string|null $idx |
||
312 | * @param LinkInterface|null $aboutLink |
||
313 | * @param int|string|null $code |
||
314 | * @param mixed|null $meta |
||
315 | * |
||
316 | * @return $this |
||
317 | */ |
||
318 | 1 | View Code Duplication | public function addRelationshipTypeError( |
332 | |||
333 | /** |
||
334 | * @param string $name |
||
335 | * @param string $title |
||
336 | * @param string|null $detail |
||
337 | * @param int|string|null $status |
||
338 | * @param int|string|null $idx |
||
339 | * @param LinkInterface|null $aboutLink |
||
340 | * @param int|string|null $code |
||
341 | * @param mixed|null $meta |
||
342 | * |
||
343 | * @return $this |
||
344 | */ |
||
345 | 1 | View Code Duplication | public function addRelationshipIdError( |
359 | |||
360 | /** |
||
361 | * @param string $name |
||
362 | * @param string $title |
||
363 | * @param string|null $detail |
||
364 | * @param int|string|null $status |
||
365 | * @param int|string|null $idx |
||
366 | * @param LinkInterface|null $aboutLink |
||
367 | * @param int|string|null $code |
||
368 | * @param mixed|null $meta |
||
369 | * |
||
370 | * @return $this |
||
371 | */ |
||
372 | 8 | View Code Duplication | public function addQueryParameterError( |
389 | |||
390 | /** @noinspection PhpTooManyParametersInspection |
||
391 | * @param string $title |
||
392 | * @param string $pointer |
||
393 | * @param string|null $detail |
||
394 | * @param int|string|null $status |
||
395 | * @param int|string|null $idx |
||
396 | * @param LinkInterface|null $aboutLink |
||
397 | * @param int|string|null $code |
||
398 | * @param mixed|null $meta |
||
399 | * |
||
400 | * @return $this |
||
401 | */ |
||
402 | 10 | View Code Duplication | protected function addResourceError( |
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | 10 | protected function getPathToData() |
|
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | 1 | protected function getPathToType() |
|
435 | |||
436 | /** |
||
437 | * @return string |
||
438 | */ |
||
439 | 1 | protected function getPathToId() |
|
443 | |||
444 | /** |
||
445 | * @return string |
||
446 | */ |
||
447 | 1 | protected function getPathToAttributes() |
|
451 | |||
452 | /** |
||
453 | * @param string $name |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 1 | protected function getPathToAttribute($name) |
|
461 | |||
462 | /** |
||
463 | * @return string |
||
464 | */ |
||
465 | 4 | protected function getPathToRelationships() |
|
469 | |||
470 | /** |
||
471 | * @param string $name |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | 3 | protected function getPathToRelationship($name) |
|
479 | |||
480 | /** |
||
481 | * @param string $name |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | 1 | protected function getPathToRelationshipType($name) |
|
490 | |||
491 | /** |
||
492 | * @param string $name |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | 1 | protected function getPathToRelationshipId($name) |
|
501 | } |
||
502 |