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 |
||
17 | class DeviceController extends BaseController |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | View Code Duplication | public function behaviors() |
|
40 | |||
41 | /** |
||
42 | * Lists all Device models. |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | View Code Duplication | public function actionIndex() |
|
56 | |||
57 | /** |
||
58 | * Displays a single Device model. |
||
59 | * |
||
60 | * @param int $id |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function actionView($id) |
||
77 | |||
78 | /** |
||
79 | * Updates an existing Device model. |
||
80 | * If update is successful, the browser will be redirected to the 'view' page. |
||
81 | * |
||
82 | * @param int $id |
||
83 | * |
||
84 | * @return \yii\web\Response|string redirect or render |
||
85 | */ |
||
86 | public function actionUpdate($id) |
||
98 | |||
99 | /** |
||
100 | * Deletes an existing Device model. |
||
101 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
102 | * |
||
103 | * @param int $id |
||
104 | * |
||
105 | * @return \yii\web\Response |
||
106 | */ |
||
107 | public function actionDelete($id) |
||
113 | |||
114 | /** |
||
115 | * Adds a Screen to this Device or render link view. |
||
116 | * |
||
117 | * @param int $id |
||
118 | * @param int $screenId |
||
119 | * |
||
120 | * @return \yii\web\Response|string redirec or render |
||
121 | */ |
||
122 | View Code Duplication | public function actionLink($id, $screenId = null) |
|
143 | |||
144 | /** |
||
145 | * Remove a Screen from a Device. |
||
146 | * |
||
147 | * @param int $id |
||
148 | * @param int $screenId |
||
149 | * |
||
150 | * @return \yii\web\Response |
||
151 | */ |
||
152 | View Code Duplication | public function actionUnlink($id, $screenId) |
|
162 | |||
163 | /** |
||
164 | * Enables or disables a Device. |
||
165 | * |
||
166 | * @param int $id screen id |
||
167 | * |
||
168 | * @return \yii\web\Response |
||
169 | */ |
||
170 | View Code Duplication | public function actionToggle($id) |
|
179 | |||
180 | /** |
||
181 | * Finds the Device model based on its primary key value. |
||
182 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
183 | * |
||
184 | * @param int $id |
||
185 | * |
||
186 | * @return Device the loaded model |
||
187 | * |
||
188 | * @throws NotFoundHttpException if the model cannot be found |
||
189 | */ |
||
190 | View Code Duplication | protected function findModel($id) |
|
198 | } |
||
199 |
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.