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 |
||
16 | View Code Duplication | class ArticleController extends CommonAdminController |
|
|
|||
17 | { |
||
18 | use AdminBeforeActionTrait, AccessTrait, AdditionFieldsTrait; |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $setEditingScenarios = true; |
||
24 | |||
25 | /** |
||
26 | * @return mixed|string |
||
27 | */ |
||
28 | public function actionIndex() |
||
36 | |||
37 | /** |
||
38 | * @param int|string $id |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function actionView($id) |
||
52 | |||
53 | /** |
||
54 | * @return mixed|string|\yii\web\Response |
||
55 | */ |
||
56 | public function actionCreate() |
||
67 | |||
68 | /** |
||
69 | * @param int|string $id |
||
70 | * |
||
71 | * @return string|\yii\web\Response |
||
72 | */ |
||
73 | public function actionUpdate($id) |
||
85 | |||
86 | /** |
||
87 | * @param int|string $id |
||
88 | * |
||
89 | * @return mixed|\yii\web\Response |
||
90 | */ |
||
91 | public function actionDelete($id) |
||
99 | |||
100 | /** |
||
101 | * Returns Product model name. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getModelName():string |
||
109 | |||
110 | /** |
||
111 | * Returns ProductSearch model name. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function getSearchModelName():string |
||
119 | } |
||
120 |
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.