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 |
||
12 | class ReverseListIterator implements IteratorInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * |
||
17 | * @var ListInterface |
||
18 | */ |
||
19 | protected $list; |
||
20 | |||
21 | /** |
||
22 | * |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $index; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var boolean |
||
30 | */ |
||
31 | protected $canRemove; |
||
32 | |||
33 | /** |
||
34 | * Construct a ReverseListIterator |
||
35 | * @param ListInterface $list |
||
36 | */ |
||
37 | public function __construct(ListInterface $list) |
||
42 | |||
43 | /** |
||
44 | * Returns true if this list iterator has more elements when traversing the list in the backward direction. |
||
45 | * @return boolean |
||
46 | */ |
||
47 | View Code Duplication | public function next() |
|
59 | |||
60 | /** |
||
61 | * Returns the next element in the list and advances the cursor position. |
||
62 | * @return multitype |
||
63 | * @throws NoSuchElementException |
||
64 | */ |
||
65 | public function hasNext() |
||
69 | |||
70 | /** |
||
71 | * Removes from the list the last element that was returned by next() |
||
72 | * @return void |
||
73 | * @throws IllegalStateException |
||
74 | */ |
||
75 | View Code Duplication | public function remove() |
|
87 | } |
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.