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 |
||
7 | class OrderRepository implements OrderRepositoryInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var mixed |
||
11 | */ |
||
12 | public $column; |
||
13 | |||
14 | /** |
||
15 | * OrderRepository constructor. |
||
16 | */ |
||
17 | public function __construct() |
||
21 | |||
22 | /** |
||
23 | * @param Model $model |
||
24 | * |
||
25 | * @return bool |
||
26 | */ |
||
27 | View Code Duplication | public function increment(Model $model) |
|
38 | |||
39 | /** |
||
40 | * @param Model $model |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | View Code Duplication | public function decrement(Model $model) |
|
55 | |||
56 | /** |
||
57 | * @param Model $model |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function delete(Model $model) |
||
70 | |||
71 | /** |
||
72 | * @param Model $model |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function toFirst(Model $model) |
||
89 | |||
90 | /** |
||
91 | * @param Model $model |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function toLast(Model $model) |
||
110 | |||
111 | /** |
||
112 | * @param Model $model |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function toMiddle(Model $model) |
||
140 | |||
141 | /** |
||
142 | * @param Model $model1 |
||
143 | * @param Model $model2 |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function switchModels(Model $model1, Model $model2) |
||
158 | |||
159 | /** |
||
160 | * @param Model $model |
||
161 | * @param $index1 |
||
162 | * @param $index2 |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function switchIndexes(Model $model, $index1, $index2) |
||
173 | |||
174 | /** |
||
175 | * @param Model $model |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function count(Model $model) |
||
183 | } |
||
184 |
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.