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 CircularLinkedList 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 CircularLinkedList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CircularLinkedList extends ListAbstract { |
||
25 | use ArrayAccessTrait; |
||
26 | protected $head; |
||
27 | private $tail; |
||
28 | private $current; |
||
29 | private $position; |
||
30 | |||
31 | View Code Duplication | public function __construct() { |
|
38 | |||
39 | |||
40 | /** |
||
41 | * Inserts at the beginning of the list. |
||
42 | * |
||
43 | * @param mixed $data |
||
44 | */ |
||
45 | protected function insertBeginning($data) { |
||
57 | |||
58 | /** |
||
59 | * Add a new node in the specified index. |
||
60 | * |
||
61 | * @param integer $index the position. |
||
62 | * @param mixed $data the data to be stored. |
||
63 | */ |
||
64 | View Code Duplication | protected function insertEnd($data) { |
|
70 | |||
71 | /** |
||
72 | * Add a new node in the specified index. |
||
73 | * |
||
74 | * @param integer $index the position. |
||
75 | * @param mixed $data the data to be stored. |
||
76 | */ |
||
77 | View Code Duplication | protected function insertAt($index, $data) { |
|
91 | |||
92 | /** |
||
93 | * Returns the last node data with O(1). |
||
94 | * |
||
95 | * @return mixed null if the list is empty. |
||
96 | */ |
||
97 | public function getLast() { |
||
102 | |||
103 | /** |
||
104 | * Returns the last node with O(1). |
||
105 | * |
||
106 | * @return mixed null if the list is empty. |
||
107 | */ |
||
108 | protected function searchLast() { |
||
115 | |||
116 | /** |
||
117 | * Returns the data stored in the given position. |
||
118 | * If index is 0 or size - 1 the method is O(1) else O(n). |
||
119 | * |
||
120 | * @param integer $index the position. |
||
121 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
||
122 | * @return mixed the data stored in $index node. |
||
123 | */ |
||
124 | View Code Duplication | public function get($index) { |
|
132 | |||
133 | /** |
||
134 | * Returns the node stored in the given position. |
||
135 | * If index is 0 or (size - 1) the method is O(1) else O(n). |
||
136 | * |
||
137 | * @param integer $index the position. |
||
138 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
||
139 | * @return DataStructures\Lists\Nodes\SimpleLinkedListNode the node stored in $index. |
||
140 | */ |
||
141 | protected function search($index) { |
||
163 | |||
164 | /** |
||
165 | * {@inheritDoc} |
||
166 | */ |
||
167 | public function contains($data) : bool { |
||
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | */ |
||
193 | View Code Duplication | public function indexOf($data) { |
|
212 | |||
213 | /** |
||
214 | * {@inheritDoc} |
||
215 | */ |
||
216 | View Code Duplication | public function lastIndexOf($data) { |
|
235 | |||
236 | /** |
||
237 | * {@inheritDoc} |
||
238 | */ |
||
239 | public function remove($data) { |
||
268 | |||
269 | /** |
||
270 | * Generator for retrieve all nodes stored. |
||
271 | * |
||
272 | * @return null if the head is null (or list is empty) |
||
273 | */ |
||
274 | View Code Duplication | public function getAll() { |
|
291 | |||
292 | /** |
||
293 | * {@inheritDoc} |
||
294 | */ |
||
295 | View Code Duplication | protected function deleteBeginning() { |
|
309 | |||
310 | /** |
||
311 | * {@inheritDoc} |
||
312 | */ |
||
313 | protected function deleteAt($index) { |
||
328 | |||
329 | /** |
||
330 | * {@inheritDoc} |
||
331 | */ |
||
332 | View Code Duplication | protected function deleteEnd() { |
|
348 | |||
349 | public function clear() { |
||
354 | |||
355 | /** |
||
356 | * Reset the cursor position. |
||
357 | */ |
||
358 | public function rewind() { |
||
362 | |||
363 | /** |
||
364 | * Returns the current node data. |
||
365 | * |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function current() { |
||
371 | |||
372 | /** |
||
373 | * Key or index that indicates the cursor position. |
||
374 | * |
||
375 | * @return integer The current position. |
||
376 | */ |
||
377 | public function key() { |
||
380 | |||
381 | /** |
||
382 | * Move the cursor to the next node and increments the |
||
383 | * position counter. |
||
384 | */ |
||
385 | public function next() { |
||
389 | |||
390 | /** |
||
391 | * Returns if the current pointer position is valid. |
||
392 | * |
||
393 | * @return boolean true if pointer is not last, else false. |
||
394 | */ |
||
395 | public function valid() { |
||
398 | } |
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.