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 ContentTypeController extends BaseController |
||
16 | { |
||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | View Code Duplication | public function behaviors() |
|
38 | |||
39 | /** |
||
40 | * Lists all ContentType models. |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function actionIndex() |
||
54 | |||
55 | /** |
||
56 | * Enables or disables a Device. |
||
57 | * |
||
58 | * @param int $id screen id |
||
59 | * |
||
60 | * @return \yii\web\Response |
||
61 | */ |
||
62 | View Code Duplication | public function actionToggle($id) |
|
71 | |||
72 | /** |
||
73 | * Finds the ContentType model based on its primary key value. |
||
74 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
75 | * |
||
76 | * @param int $id |
||
77 | * |
||
78 | * @return ContentType the loaded model |
||
79 | * |
||
80 | * @throws NotFoundHttpException if the model cannot be found |
||
81 | */ |
||
82 | View Code Duplication | protected function findModel($id) |
|
90 | } |
||
91 |
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.