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 |
||
18 | class ScreenController extends BaseController |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | View Code Duplication | public function behaviors() |
|
41 | |||
42 | /** |
||
43 | * Lists all Screen models. |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | public function actionIndex() |
||
62 | |||
63 | /** |
||
64 | * Displays a single Screen model. |
||
65 | * |
||
66 | * @param int $id |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function actionView($id) |
||
86 | |||
87 | /** |
||
88 | * Creates a new Screen model. |
||
89 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
90 | * |
||
91 | * @return \yii\web\Response|string redirect or render |
||
92 | */ |
||
93 | public function actionCreate($device_id = null) |
||
118 | |||
119 | /** |
||
120 | * Updates an existing Screen model. |
||
121 | * If update is successful, the browser will be redirected to the 'view' page. |
||
122 | * |
||
123 | * @param int $id |
||
124 | * |
||
125 | * @return \yii\web\Response|string redirect or render |
||
126 | */ |
||
127 | public function actionUpdate($id) |
||
147 | |||
148 | /** |
||
149 | * Deletes an existing Screen model. |
||
150 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
151 | * |
||
152 | * @param int $id |
||
153 | * |
||
154 | * @return \yii\web\Response |
||
155 | */ |
||
156 | public function actionDelete($id) |
||
162 | |||
163 | /** |
||
164 | * Adds a flow to this screen or render link view. |
||
165 | * |
||
166 | * @param int $id |
||
167 | * @param int $flowId |
||
168 | * |
||
169 | * @return \yii\web\Response|string redirect or render |
||
170 | */ |
||
171 | View Code Duplication | public function actionLink($id, $flowId = null) |
|
192 | |||
193 | /** |
||
194 | * Remove a flow from a screen. |
||
195 | * |
||
196 | * @param int $id |
||
197 | * @param int $flowId |
||
198 | * |
||
199 | * @return \yii\web\Response |
||
200 | */ |
||
201 | View Code Duplication | public function actionUnlink($id, $flowId) |
|
211 | |||
212 | /** |
||
213 | * Finds the Screen model based on its primary key value. |
||
214 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
215 | * |
||
216 | * @param int $id |
||
217 | * |
||
218 | * @return Screen the loaded model |
||
219 | * |
||
220 | * @throws NotFoundHttpException if the model cannot be found |
||
221 | */ |
||
222 | View Code Duplication | protected function findModel($id) |
|
230 | } |
||
231 |
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.