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 |
||
24 | class UserController extends Controller |
||
25 | { |
||
26 | private $_oldMailPath; |
||
27 | |||
28 | /** |
||
29 | * @inheritdoc |
||
30 | */ |
||
31 | View Code Duplication | public function behaviors() |
|
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | public function beforeAction($action) |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | public function afterAction($action, $result) |
||
71 | |||
72 | /** |
||
73 | * Lists all User models. |
||
74 | * @return mixed |
||
75 | */ |
||
76 | View Code Duplication | public function actionIndex() |
|
86 | |||
87 | /** |
||
88 | * Displays a single User model. |
||
89 | * @param integer $id |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function actionView($id) |
||
98 | |||
99 | /** |
||
100 | * Deletes an existing User model. |
||
101 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
102 | * @param integer $id |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function actionDelete($id) |
||
111 | |||
112 | /** |
||
113 | * Login |
||
114 | * @return string |
||
115 | */ |
||
116 | public function actionLogin() |
||
131 | |||
132 | /** |
||
133 | * Logout |
||
134 | * @return string |
||
135 | */ |
||
136 | public function actionLogout() |
||
142 | |||
143 | /** |
||
144 | * Signup new user |
||
145 | * @return string |
||
146 | */ |
||
147 | View Code Duplication | public function actionSignup() |
|
160 | |||
161 | /** |
||
162 | * Request reset password |
||
163 | * @return string |
||
164 | */ |
||
165 | public function actionRequestPasswordReset() |
||
182 | |||
183 | /** |
||
184 | * Reset password |
||
185 | * @return string |
||
186 | */ |
||
187 | public function actionResetPassword($token) |
||
205 | |||
206 | /** |
||
207 | * Reset password |
||
208 | * @return string |
||
209 | */ |
||
210 | View Code Duplication | public function actionChangePassword() |
|
221 | |||
222 | /** |
||
223 | * Activate new user |
||
224 | * @param integer $id |
||
225 | * @return type |
||
226 | * @throws UserException |
||
227 | * @throws NotFoundHttpException |
||
228 | */ |
||
229 | public function actionActivate($id) |
||
244 | |||
245 | /** |
||
246 | * Finds the User model based on its primary key value. |
||
247 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
248 | * @param integer $id |
||
249 | * @return User the loaded model |
||
250 | * @throws NotFoundHttpException if the model cannot be found |
||
251 | */ |
||
252 | protected function findModel($id) |
||
260 | } |
||
261 |
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.