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; |
||
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() |
|
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | 10 | public function getIterator() |
|
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 26 | public function count() |
|
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | 1 | public function serialize() |
|
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 1 | public function unserialize($serialized) |
|
77 | |||
78 | /** |
||
79 | * @inheritdoc |
||
80 | */ |
||
81 | 1 | public function offsetExists($offset) |
|
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | * |
||
89 | * @return Error |
||
90 | */ |
||
91 | 15 | public function offsetGet($offset) |
|
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | 11 | public function offsetSet($offset, $value) |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 1 | public function offsetUnset($offset) |
|
111 | |||
112 | /** |
||
113 | * @return Error[] |
||
114 | */ |
||
115 | 4 | public function getArrayCopy() |
|
119 | |||
120 | /** |
||
121 | * @param Error $error |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | 20 | public function add(Error $error) |
|
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( |
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( |
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( |
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( |
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( |
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( |
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( |
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( |
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( |
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( |
399 | |||
400 | /** |
||
401 | * @return string |
||
402 | */ |
||
403 | 9 | protected function getPathToData() |
|
407 | |||
408 | /** |
||
409 | * @return string |
||
410 | */ |
||
411 | 1 | protected function getPathToType() |
|
415 | |||
416 | /** |
||
417 | * @return string |
||
418 | */ |
||
419 | 1 | protected function getPathToId() |
|
423 | |||
424 | /** |
||
425 | * @param string $name |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | 1 | protected function getPathToAttribute($name) |
|
433 | |||
434 | /** |
||
435 | * @return string |
||
436 | */ |
||
437 | 4 | protected function getPathToRelationships() |
|
441 | |||
442 | /** |
||
443 | * @param string $name |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 3 | protected function getPathToRelationship($name) |
|
451 | |||
452 | /** |
||
453 | * @param string $name |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 1 | protected function getPathToRelationshipType($name) |
|
462 | |||
463 | /** |
||
464 | * @param string $name |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | 1 | protected function getPathToRelationshipId($name) |
|
473 | } |
||
474 |
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.