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:
Complex classes like SimpleLinkedList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SimpleLinkedList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class SimpleLinkedList extends ListAbstract { |
||
25 | use ArrayAccessTrait; |
||
26 | |||
27 | protected $head; |
||
28 | private $position; |
||
29 | private $current; |
||
30 | |||
31 | View Code Duplication | public function __construct() { |
|
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | protected function search($index) { |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | */ |
||
59 | public function getLast() { |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | */ |
||
67 | View Code Duplication | public function searchLast() { |
|
79 | |||
80 | /** |
||
81 | * Generator for retrieve all nodes stored. |
||
82 | * |
||
83 | * @return null if the head is null (or list is empty) |
||
84 | */ |
||
85 | View Code Duplication | public function getAll() { |
|
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | public function contains($data) : bool { |
||
115 | |||
116 | /** |
||
117 | * {@inheritDoc} |
||
118 | */ |
||
119 | View Code Duplication | public function indexOf($data) { |
|
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | View Code Duplication | public function lastIndexOf($data) { |
|
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | public function remove($data) { |
||
194 | |||
195 | /** |
||
196 | * Add a new node in the specified index. |
||
197 | * |
||
198 | * @param integer $index the position. |
||
199 | * @param mixed $data the data to be stored. |
||
200 | */ |
||
201 | View Code Duplication | protected function insertAt($index, $data) { |
|
215 | |||
216 | protected function insertEnd($data) { |
||
229 | |||
230 | /** |
||
231 | * {@inheritDoc} |
||
232 | */ |
||
233 | View Code Duplication | protected function insertBeginning($data) { |
|
242 | |||
243 | /** |
||
244 | * Delete a node in the given position and returns it back. |
||
245 | * |
||
246 | * @param integer $index the position. |
||
247 | * @throws OutOfBoundsException if index is negative |
||
248 | * or is greater than the size of the list. |
||
249 | */ |
||
250 | public function delete($index) { |
||
289 | |||
290 | protected function deleteBeginning() {} |
||
291 | |||
292 | protected function deleteAt($index) {} |
||
293 | |||
294 | protected function deleteEnd() {} |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * Reset the cursor position. |
||
300 | */ |
||
301 | public function rewind() { |
||
305 | |||
306 | /** |
||
307 | * Returns the current node data. |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | public function current() { |
||
314 | |||
315 | /** |
||
316 | * Key or index that indicates the cursor position. |
||
317 | * |
||
318 | * @return integer The current position. |
||
319 | */ |
||
320 | public function key() { |
||
323 | |||
324 | /** |
||
325 | * Move the cursor to the next node and increments the |
||
326 | * position counter. |
||
327 | */ |
||
328 | public function next() { |
||
332 | |||
333 | /** |
||
334 | * Returns if the current pointer position is valid. |
||
335 | * |
||
336 | * @return boolean true if pointer is not last, else false. |
||
337 | */ |
||
338 | public function valid() { |
||
341 | } |
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.