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 |
||
11 | class Shift extends Combinatorics implements IteratorInterface |
||
12 | { |
||
13 | /** |
||
14 | * The key. |
||
15 | * |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $key = 0; |
||
19 | |||
20 | /** |
||
21 | * A copy of the dataset at a give time. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $current; |
||
26 | |||
27 | /** |
||
28 | * Shift constructor. |
||
29 | * |
||
30 | * @param array $dataset |
||
31 | * The dataset |
||
32 | * @param int $length |
||
33 | * The shift length |
||
34 | */ |
||
35 | 12 | public function __construct(array $dataset = [], $length = 1) |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 12 | View Code Duplication | public function setLength($length = null) |
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 4 | public function current() |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 8 | public function next() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function rewind() |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function valid() |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 12 | public function count() |
|
91 | |||
92 | /** |
||
93 | * Internal function to do the shift. |
||
94 | * |
||
95 | * @param int $length |
||
96 | */ |
||
97 | 8 | protected function doShift($length = 1) |
|
114 | } |
||
115 |
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.