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 |
||
13 | class ListIterator implements IteratorInterface |
||
14 | { |
||
15 | /** |
||
16 | * The current iteration index |
||
17 | * @var int |
||
18 | */ |
||
19 | protected $index; |
||
20 | |||
21 | /** |
||
22 | * The underlying ListInterface object |
||
23 | * @var ListInterface |
||
24 | */ |
||
25 | protected $list; |
||
26 | |||
27 | /** |
||
28 | * canRemove determine whether or not we can remove an item. |
||
29 | * The list can't remove things until after next has been called |
||
30 | * @var boolean |
||
31 | */ |
||
32 | protected $canRemove; |
||
33 | |||
34 | /** |
||
35 | * Constructs a new ListIterator at the specified start position |
||
36 | * @param ListInterface $list |
||
37 | * @param int $start |
||
38 | */ |
||
39 | public function __construct(ListInterface $list, $start = 0) |
||
47 | |||
48 | /** |
||
49 | * Returns true if this list iterator has more elements when traversing the list in the forward direction. |
||
50 | * @return boolean |
||
51 | */ |
||
52 | public function hasNext() |
||
56 | |||
57 | /** |
||
58 | * Returns the next element in the list and advances the cursor position. |
||
59 | * @return multitype |
||
60 | * @throws NoSuchElementException |
||
61 | */ |
||
62 | View Code Duplication | public function next() |
|
74 | |||
75 | /** |
||
76 | * Removes from the list the last element that was returned by next() |
||
77 | * @return void |
||
78 | * @throws IllegalStateException |
||
79 | */ |
||
80 | View Code Duplication | public function remove() |
|
92 | } |
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.