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 |
||
28 | class MediaController extends Controller |
||
29 | { |
||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | public function behaviors() |
||
67 | |||
68 | /** |
||
69 | * Lists all Media models. |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | View Code Duplication | public function actionIndex() |
|
83 | |||
84 | |||
85 | /** |
||
86 | * Creates a new Media model. |
||
87 | * If creation is successful, the browser will display thumbnail and options to the 'create' page. |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | public function actionCreate() |
||
95 | |||
96 | /** |
||
97 | * Updates an existing Media model. |
||
98 | * If update is successful, the browser will be redirected to the 'view' page. |
||
99 | * |
||
100 | * @param integer $id |
||
101 | * @return mixed |
||
102 | * @throws ForbiddenHttpException |
||
103 | * @throws NotFoundHttpException |
||
104 | */ |
||
105 | public function actionUpdate($id) |
||
125 | |||
126 | /** |
||
127 | * Deletes an existing Media model. |
||
128 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
129 | * |
||
130 | * @param integer $id |
||
131 | * @return mixed |
||
132 | * @throws ForbiddenHttpException |
||
133 | * @throws NotFoundHttpException |
||
134 | */ |
||
135 | public function actionDelete($id) |
||
143 | |||
144 | /** |
||
145 | * Bulk action for Media triggered when button 'Apply' clicked. |
||
146 | * The action depends on the value of the dropdown next to the button. |
||
147 | * Only accept POST HTTP method. |
||
148 | */ |
||
149 | public function actionBulkAction() |
||
159 | |||
160 | /** |
||
161 | * Upload media file and store it to database. |
||
162 | * Media versions can be set from application params. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function actionAjaxUpload() |
||
195 | |||
196 | /** |
||
197 | * Update attributes of Media model via AJAX request. |
||
198 | */ |
||
199 | public function actionAjaxUpdate() |
||
207 | |||
208 | /** |
||
209 | * Delete Media model and its files based on media primary key. |
||
210 | * |
||
211 | * @param $id |
||
212 | * @return array |
||
213 | * @throws ForbiddenHttpException |
||
214 | * @throws NotFoundHttpException |
||
215 | */ |
||
216 | public function actionAjaxDelete($id) |
||
222 | |||
223 | /** |
||
224 | * Get permission to access model by current user. |
||
225 | * If the user does not obtain the permission, a 403 exeption will be thrown. |
||
226 | * |
||
227 | * @param $model Media |
||
228 | * @throws ForbiddenHttpException |
||
229 | */ |
||
230 | public function getPermission($model) |
||
236 | |||
237 | /** |
||
238 | * Finds the Media model based on its primary key value. |
||
239 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
240 | * |
||
241 | * @param integer $id |
||
242 | * @return Media the loaded model |
||
243 | * @throws NotFoundHttpException if the model cannot be found |
||
244 | */ |
||
245 | protected function findModel($id) |
||
253 | } |
||
254 |
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.