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 |
||
| 15 | class BackendChunkGroupController extends BackendController |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @inheritdoc |
||
| 19 | */ |
||
| 20 | public function behaviors() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Lists all ContentBlockGroup models. |
||
| 34 | * @return mixed |
||
| 35 | */ |
||
| 36 | public function actionIndex() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Displays a single ContentBlockGroup model. |
||
| 49 | * @param integer $id |
||
| 50 | * @return mixed |
||
| 51 | */ |
||
| 52 | public function actionView($id) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Creates a new ContentBlockGroup model. |
||
| 61 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | public function actionCreate($parent_id = 1, $returnUrl = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Updates an existing ContentBlockGroup model. |
||
| 87 | * If update is successful, the browser will be redirected to the 'view' page. |
||
| 88 | * @param integer $id |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function actionUpdate($id, $returnUrl = null) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Deletes an existing ContentBlockGroup model. |
||
| 109 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
| 110 | * @param integer $id |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function actionDelete($id, $returnUrl = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Finds the ContentBlockGroup model based on its primary key value. |
||
| 126 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 127 | * @param integer $id |
||
| 128 | * @return ContentBlockGroup the loaded model |
||
| 129 | * @throws NotFoundHttpException if the model cannot be found |
||
| 130 | */ |
||
| 131 | protected function findModel($id) |
||
| 139 | } |
||
| 140 |
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.