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 |
||
11 | final class EntityCollection implements EntityInterface, \Iterator, \ArrayAccess |
||
12 | { |
||
13 | use IteratorTrait; |
||
14 | |||
15 | /** |
||
16 | * Array of EntityInterface objects. |
||
17 | * |
||
18 | * @var EntityInterface[] |
||
19 | */ |
||
20 | private $container = []; |
||
21 | |||
22 | /** |
||
23 | * Sets the value at the specified index $offset to $value. |
||
24 | * |
||
25 | * @param integer $offset The index being set. |
||
26 | * @param mixed $value The new value for the index. |
||
27 | * |
||
28 | * @return void |
||
29 | * |
||
30 | * @throws \InvalidArgumentException Thrown if $offset is not null and is not an integer. |
||
31 | */ |
||
32 | public function offsetSet($offset, $value) |
||
55 | |||
56 | /** |
||
57 | * Returns whether the requested index exists. |
||
58 | * |
||
59 | * @param integer $offset The index being checked. |
||
60 | * |
||
61 | * @return boolean |
||
62 | */ |
||
63 | View Code Duplication | public function offsetExists($offset) |
|
72 | |||
73 | /** |
||
74 | * Unsets the value at the specified index. |
||
75 | * |
||
76 | * @param integer $offset The index being unset. |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function offsetUnset($offset) |
||
87 | |||
88 | /** |
||
89 | * Returns the value at the specified index. |
||
90 | * |
||
91 | * @param integer $offset The index with the value. |
||
92 | * |
||
93 | * @return EntityInterface|null |
||
94 | */ |
||
95 | View Code Duplication | public function offsetGet($offset) |
|
104 | |||
105 | /** |
||
106 | * Converts this entity collection into an xml string. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function jsonSerialize() |
||
114 | } |
||
115 |
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.