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 |
||
19 | class ContentController extends BaseController |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | View Code Duplication | public function behaviors() |
|
42 | |||
43 | /** |
||
44 | * Lists all Content models. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function actionIndex() |
||
73 | |||
74 | /** |
||
75 | * Displays a single Content model. |
||
76 | * |
||
77 | * @param int $id |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function actionView($id) |
||
89 | |||
90 | /** |
||
91 | * Creates a new Content model with type choice assistance. |
||
92 | * |
||
93 | * @param int $flowId |
||
94 | * @param string $type content type |
||
95 | * |
||
96 | * @return \yii\web\Response|string redirect or render |
||
97 | */ |
||
98 | public function actionGenerate($flowId, $type = null) |
||
152 | |||
153 | /** |
||
154 | * Receives an uploaded file and responds with filepath. |
||
155 | * |
||
156 | * @api |
||
157 | * |
||
158 | * @param string $type content type |
||
159 | * |
||
160 | * @return string json status |
||
161 | */ |
||
162 | View Code Duplication | public function actionUpload($type) |
|
177 | |||
178 | /** |
||
179 | * Receives an url to download on server -- sideloading. |
||
180 | * |
||
181 | * @api |
||
182 | * |
||
183 | * @param string $type content type |
||
184 | * @param string $url |
||
185 | * |
||
186 | * @return string json status |
||
187 | */ |
||
188 | View Code Duplication | public function actionSideload($type, $url) |
|
203 | |||
204 | /** |
||
205 | * Updates an existing Content model. |
||
206 | * If update is successful, the browser will be redirected to the 'view' page. |
||
207 | * |
||
208 | * @param int $id |
||
209 | * |
||
210 | * @return \yii\web\Response|string redirect or render |
||
211 | */ |
||
212 | public function actionUpdate($id) |
||
225 | |||
226 | /** |
||
227 | * Deletes an existing Content model. |
||
228 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
229 | * |
||
230 | * @param int $id |
||
231 | * |
||
232 | * @return \yii\web\Response |
||
233 | */ |
||
234 | public function actionDelete($id) |
||
242 | |||
243 | /** |
||
244 | * Renders specific content for preview. |
||
245 | * |
||
246 | * @param int $id content id |
||
247 | * |
||
248 | * @return string HTML render |
||
249 | */ |
||
250 | public function actionPreview($id) |
||
259 | |||
260 | /** |
||
261 | * Enables or disable a specific content. |
||
262 | * |
||
263 | * @param int $id content id |
||
264 | * |
||
265 | * @return \yii\web\Response |
||
266 | */ |
||
267 | public function actionToggle($id) |
||
277 | |||
278 | /** |
||
279 | * Finds the Content model based on its primary key value. |
||
280 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
281 | * If the user has not enough rights, a 403 HTTP exception will be thrown. |
||
282 | * |
||
283 | * @param int $id |
||
284 | * @param \yii\web\User $user |
||
285 | * |
||
286 | * @return Content the loaded model |
||
287 | * |
||
288 | * @throws NotFoundHttpException if the model cannot be found |
||
289 | * @throws ForbiddenHttpException if the model cannot be accessed |
||
290 | */ |
||
291 | protected function findViewableModel($id, $user) |
||
300 | |||
301 | /** |
||
302 | * Finds the Content model based on its primary key value. |
||
303 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
304 | * |
||
305 | * @param int $id |
||
306 | * |
||
307 | * @return Content the loaded model |
||
308 | * |
||
309 | * @throws NotFoundHttpException if the model cannot be found |
||
310 | */ |
||
311 | View Code Duplication | protected function findModel($id) |
|
319 | } |
||
320 |
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.