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 |
||
16 | class UserController implements |
||
17 | ConfigureInterface, |
||
18 | InjectionAwareInterface |
||
19 | { |
||
20 | use ConfigureTrait, |
||
21 | InjectionAwareTrait; |
||
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * @var $data description |
||
27 | */ |
||
28 | //private $data; |
||
29 | |||
30 | |||
31 | |||
32 | /** |
||
33 | * Description. |
||
34 | * |
||
35 | * @throws Exception |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | View Code Duplication | public function getIndex() |
|
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Description. |
||
54 | * |
||
55 | * @throws Exception |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | View Code Duplication | public function getPostLogin() |
|
76 | |||
77 | |||
78 | |||
79 | /** |
||
80 | * Description. |
||
81 | * |
||
82 | * @throws Exception |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | View Code Duplication | public function getPostCreateUser() |
|
103 | |||
104 | /** |
||
105 | * Handler with form to update a user. |
||
106 | * |
||
107 | * @param datatype $id User ID |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | View Code Duplication | public function getPostUpdateUser($id) |
|
129 | |||
130 | /** |
||
131 | * Description. |
||
132 | * |
||
133 | * @param datatype $input What user to login. |
||
134 | * |
||
135 | * @throws Exception |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function loginUser($input) |
||
143 | |||
144 | /** |
||
145 | * Description. |
||
146 | * |
||
147 | * @throws Exception |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | View Code Duplication | public function logoutUser() |
|
161 | |||
162 | /** |
||
163 | * Description. |
||
164 | * |
||
165 | * @param datatype $userInput What user to check ID. |
||
166 | * |
||
167 | * @throws Exception |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | View Code Duplication | public function getIDByUser($userInput) |
|
179 | |||
180 | /** |
||
181 | * Description. |
||
182 | * |
||
183 | * @throws Exception |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | public function adminControl() |
||
201 | |||
202 | /** |
||
203 | * Delete user |
||
204 | * |
||
205 | * @param datatype $id User ID. |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function removeUser($id) |
||
215 | } |
||
216 |
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.