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 DoublyLinkedList 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 DoublyLinkedList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class DoublyLinkedList extends ListAbstract { |
||
25 | use ArrayAccessTrait; |
||
26 | |||
27 | protected $head; |
||
28 | private $tail; |
||
29 | private $position; |
||
30 | private $current; |
||
31 | |||
32 | View Code Duplication | public function __construct() { |
|
39 | |||
40 | /** |
||
41 | * Gets the data stored in the position especified. |
||
42 | * |
||
43 | * @param integer $index Index that must be greater than 0 |
||
44 | * and lower than the list size. |
||
45 | * @return mixed The data stored in the given index |
||
46 | * @throws OutOfBoundsException if index is out bounds. |
||
47 | */ |
||
48 | View Code Duplication | public function get($index) { |
|
56 | |||
57 | /** |
||
58 | * Gets the node stored in the position especified. |
||
59 | * If index is 0 or (size - 1) the method is O(1) else O(n). |
||
60 | * |
||
61 | * @param integer $index the position. |
||
62 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
||
63 | * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null the node stored in $index. |
||
64 | */ |
||
65 | protected function search($index) { |
||
95 | |||
96 | /** |
||
97 | * Returns the data in the last node with O(1). |
||
98 | * |
||
99 | * @return mixed null if the list is empty. |
||
100 | */ |
||
101 | public function getLast() { |
||
107 | |||
108 | /** |
||
109 | * Returns the last node with O(1). |
||
110 | * |
||
111 | * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null if the list is empty. |
||
112 | */ |
||
113 | public function searchLast() { |
||
119 | |||
120 | /** |
||
121 | * {@inheritDoc} |
||
122 | */ |
||
123 | public function contains($data) : bool { |
||
144 | |||
145 | /** |
||
146 | * {@inheritDoc} |
||
147 | */ |
||
148 | View Code Duplication | public function indexOf($data) { |
|
167 | |||
168 | /** |
||
169 | * {@inheritDoc} |
||
170 | */ |
||
171 | View Code Duplication | public function lastIndexOf($data) { |
|
190 | |||
191 | /** |
||
192 | * {@inheritDoc} |
||
193 | */ |
||
194 | public function remove($data) { |
||
223 | |||
224 | /** |
||
225 | * Generator for retrieve all nodes stored. |
||
226 | * |
||
227 | * @return null if the head is null (or list is empty) |
||
228 | */ |
||
229 | View Code Duplication | public function getAll() { |
|
246 | |||
247 | /** |
||
248 | * {@inheritDoc} |
||
249 | */ |
||
250 | protected function insertBeginning($data) { |
||
264 | |||
265 | protected function insertEnd($data) { |
||
273 | |||
274 | /** |
||
275 | * Add a new node in the specified index. |
||
276 | * |
||
277 | * @param integer $index the position. |
||
278 | * @param mixed $data the data to be stored. |
||
279 | */ |
||
280 | protected function insertAt($index, $data) { |
||
296 | |||
297 | /** |
||
298 | * Deletes at the beginnig of the list and returns the data stored. |
||
299 | * |
||
300 | * @return mixed the data stored in the node. |
||
301 | */ |
||
302 | View Code Duplication | protected function deleteBeginning() { |
|
315 | |||
316 | /** |
||
317 | * Deletes at the specified position and returns the data stored. |
||
318 | * |
||
319 | * @param integer $index the position. |
||
320 | * @return mixed the data stored in the node. |
||
321 | */ |
||
322 | protected function deleteAt($index) { |
||
340 | |||
341 | /** |
||
342 | * Deletes at the end of the list and returns the data stored. |
||
343 | * |
||
344 | * @return mixed the data stored in the node. |
||
345 | */ |
||
346 | View Code Duplication | protected function deleteEnd() { |
|
363 | |||
364 | /** |
||
365 | * Reset the cursor position. |
||
366 | */ |
||
367 | public function rewind() { |
||
371 | |||
372 | /** |
||
373 | * Returns the current node data. |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function current() { |
||
380 | |||
381 | /** |
||
382 | * Key or index that indicates the cursor position. |
||
383 | * |
||
384 | * @return integer The current position. |
||
385 | */ |
||
386 | public function key() { |
||
389 | |||
390 | /** |
||
391 | * Move the cursor to the next node and increments the |
||
392 | * position counter. |
||
393 | */ |
||
394 | public function next() { |
||
398 | |||
399 | /** |
||
400 | * Returns if the current pointer position is valid. |
||
401 | * |
||
402 | * @return boolean true if pointer is not last, else false. |
||
403 | */ |
||
404 | public function valid() { |
||
407 | } |
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.