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 |
||
27 | class EntitySet implements ArrayAccess, Iterator |
||
28 | { |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $container = []; |
||
33 | |||
34 | /** |
||
35 | * Checks whether an offset exists. |
||
36 | * |
||
37 | * @param int $offset |
||
38 | * @throws InvalidArgumentException |
||
39 | * @return bool |
||
40 | */ |
||
41 | View Code Duplication | public function offsetExists($offset) : bool |
|
52 | |||
53 | /** |
||
54 | * Returns the value at an offset. |
||
55 | * |
||
56 | * @param int $offset |
||
57 | * @throws InvalidArgumentException |
||
58 | * @return null|EntityInterface |
||
59 | */ |
||
60 | View Code Duplication | public function offsetGet($offset) |
|
71 | |||
72 | /** |
||
73 | * Sets a value for an offset. It appends it if offset is Null. |
||
74 | * |
||
75 | * @param null|int $offset |
||
76 | * @param EntityInterface $value |
||
77 | * @throws InvalidArgumentException |
||
78 | */ |
||
79 | public function offsetSet($offset, $value) : void |
||
103 | |||
104 | /** |
||
105 | * Deletes a record from the container at an offset. It will not reorder the container. |
||
106 | * |
||
107 | * @param mixed $offset |
||
108 | * @throws InvalidArgumentException |
||
109 | */ |
||
110 | View Code Duplication | public function offsetUnset($offset) : void |
|
123 | |||
124 | /** |
||
125 | * Rewinds the Iterator to the first element. |
||
126 | */ |
||
127 | public function rewind() : void |
||
131 | |||
132 | /** |
||
133 | * Returns the current element. |
||
134 | * |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function current() |
||
141 | |||
142 | /** |
||
143 | * Returns the key of the current element. |
||
144 | * |
||
145 | * @return int|mixed|null|string |
||
146 | */ |
||
147 | public function key() |
||
151 | |||
152 | /** |
||
153 | * Moves forward to next element. |
||
154 | */ |
||
155 | public function next() : void |
||
159 | |||
160 | /** |
||
161 | * Checks if current position is valid. |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function valid() : bool |
||
169 | |||
170 | /** |
||
171 | * Return the raw container |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | public function toArray() : array |
||
179 | |||
180 | /** |
||
181 | * Merges another EntitySet into the current container. |
||
182 | * |
||
183 | * @param EntitySet $entitySet |
||
184 | */ |
||
185 | public function merge(EntitySet $entitySet) : void |
||
189 | } |
||
190 |
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.