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 |
||
25 | class UserController extends Controller |
||
26 | { |
||
27 | /** |
||
28 | * @inheritdoc |
||
29 | */ |
||
30 | View Code Duplication | public function behaviors() |
|
57 | |||
58 | /** |
||
59 | * Lists all User models. |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | View Code Duplication | public function actionIndex() |
|
73 | |||
74 | /** |
||
75 | * Displays a single User model. |
||
76 | * |
||
77 | * @param integer $id |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function actionView($id) |
||
86 | |||
87 | /** |
||
88 | * Creates a new User model. |
||
89 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function actionCreate() |
||
112 | |||
113 | /** |
||
114 | * Updates an existing User model. |
||
115 | * If update is successful, the browser will be redirected to the 'view' page. |
||
116 | * |
||
117 | * @param integer $id |
||
118 | * @throws \yii\web\ForbiddenHttpException |
||
119 | * @throws \yii\web\NotFoundHttpException |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function actionUpdate($id) |
||
138 | |||
139 | /** |
||
140 | * Deletes an existing User model. |
||
141 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
142 | * |
||
143 | * @param integer $id |
||
144 | * @throws \Exception |
||
145 | * @throws \yii\web\ForbiddenHttpException |
||
146 | * @throws \yii\web\NotFoundHttpException |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public function actionDelete($id) |
||
157 | |||
158 | /** |
||
159 | * Bulk action for User triggered when button 'Apply' clicked. |
||
160 | * The action depends on the value of the dropdown next to the button. |
||
161 | * Only accept POST HTTP method. |
||
162 | */ |
||
163 | public function actionBulkAction() |
||
197 | |||
198 | /** |
||
199 | * Update the user data for the user who has logged in. |
||
200 | * |
||
201 | * @return string|\yii\web\Response |
||
202 | * @throws \yii\web\NotFoundHttpException |
||
203 | */ |
||
204 | View Code Duplication | public function actionProfile() |
|
216 | |||
217 | /** |
||
218 | * Reset password for logged user. |
||
219 | * It requires three inputs from users to reset password, they are the old password, new password, and repeat password. |
||
220 | * The old password will be checked by User::passwordValidation. |
||
221 | * |
||
222 | * @return string|\yii\web\Response |
||
223 | * @throws \yii\web\NotFoundHttpException |
||
224 | */ |
||
225 | public function actionResetPassword() |
||
226 | { |
||
227 | $model = $this->findModel(Yii::$app->user->id); |
||
228 | $model->setScenario('resetPassword'); |
||
229 | |||
230 | if ($model->load(Yii::$app->request->post())) { |
||
231 | $model->setPassword($model->password); |
||
232 | if ($model->save()) { |
||
233 | return $this->redirect(['view', 'id' => $model->id]); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | return $this->render('reset-password', [ |
||
238 | 'model' => $model, |
||
239 | ]); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Finds the User model based on its primary key value. |
||
244 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
245 | * |
||
246 | * @param integer $id |
||
247 | * @return User the loaded model |
||
248 | * @throws NotFoundHttpException if the model cannot be found |
||
249 | */ |
||
250 | protected function findModel($id) |
||
258 | |||
259 | /** |
||
260 | * @param $model User |
||
261 | * @throws ForbiddenHttpException |
||
262 | */ |
||
263 | protected function checkPermission($model) |
||
269 | } |
||
270 |
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.