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 TermRelationshipController extends Controller |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @inheritdoc |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function behaviors() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * If user change the value of checkbox when updating a post, this action will be triggered. |
||
| 60 | * If the checkbox checked it will trigger addItem (create TermRelationship) while if unchecked will trigger |
||
| 61 | * remItem (remove TermRelationship). |
||
| 62 | * |
||
| 63 | * @throws \Exception |
||
| 64 | * @throws \yii\web\NotFoundHttpException |
||
| 65 | */ |
||
| 66 | public function actionAjaxChangeHierarchical() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Create new TermRelationship model through AJAX via Seletize box on 'create' and 'update post' page. |
||
| 90 | */ |
||
| 91 | public function actionAjaxCreateNonHierarchical() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Delete TermRelationship for non-hierarchical Taxonomy |
||
| 104 | * which is triggered when the user remove item in Selectize box. |
||
| 105 | * |
||
| 106 | * @throws \Exception |
||
| 107 | * @throws \yii\web\NotFoundHttpException |
||
| 108 | */ |
||
| 109 | public function actionAjaxDeleteNonHierarchical() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Finds the TermRelationship model based on its primary key value. |
||
| 123 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 124 | * |
||
| 125 | * @param integer $post_id |
||
| 126 | * @param integer $term_id |
||
| 127 | * @return TermRelationship the loaded model |
||
| 128 | * @throws NotFoundHttpException if the model cannot be found |
||
| 129 | */ |
||
| 130 | protected function findModel($post_id, $term_id) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Finds the Term model based on its primary key value. |
||
| 141 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 142 | * |
||
| 143 | * @param integer $id |
||
| 144 | * @return Term|false the loaded model |
||
| 145 | */ |
||
| 146 | protected function findTerm($id) |
||
| 154 | } |
||
| 155 |
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.