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 |
||
| 19 | class FormController extends Controller |
||
| 20 | { |
||
| 21 | |||
| 22 | View Code Duplication | public function behaviors() |
|
|
1 ignored issue
–
show
|
|||
| 23 | { |
||
| 24 | return [ |
||
| 25 | 'access' => [ |
||
| 26 | 'class' => AccessControl::className(), |
||
| 27 | 'rules' => [ |
||
| 28 | [ |
||
| 29 | 'allow' => true, |
||
| 30 | 'roles' => ['form manage'], |
||
| 31 | ], |
||
| 32 | ], |
||
| 33 | ], |
||
| 34 | ]; |
||
| 35 | } |
||
| 36 | |||
| 37 | View Code Duplication | public function actionIndex() |
|
| 50 | |||
| 51 | public function actionEdit($id = null) |
||
| 168 | |||
| 169 | public function actionView($id, $show_deleted = 0) |
||
| 170 | { |
||
| 171 | $form = Form::findById($id); |
||
| 172 | $propertyGroups = $form->getPropertyGroups(); |
||
| 173 | $submission = new Submission(); |
||
| 174 | |||
| 175 | $dynamicModel = new DynamicSearchModel($submission, $propertyGroups); |
||
| 176 | $data = $dynamicModel->search(Yii::$app->request->get()); |
||
| 177 | $data->query->andWhere('form_id = :form_id', [':form_id' => $form->id]); |
||
| 178 | $data->query->andWhere(['is_deleted' => $show_deleted]); |
||
| 179 | $data->query->andFilterWhere(['sending_status' => $dynamicModel->sending_status]); |
||
| 180 | $data->query->andFilterWhere(['like', 'ip', $dynamicModel->ip]); |
||
| 181 | $data->query->andFilterWhere(['like', 'user_agent', $dynamicModel->user_agent]); |
||
| 182 | |||
| 183 | |||
| 184 | return $this->render( |
||
| 185 | 'view', |
||
| 186 | [ |
||
| 187 | 'searchModel' => $dynamicModel, |
||
| 188 | 'dataProvider' => $data, |
||
| 189 | 'form' => $form, |
||
| 190 | ] |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function actionViewSubmission($id) |
||
| 195 | { |
||
| 196 | $submission = Submission::findOne($id); |
||
| 197 | if ($submission === null) { |
||
| 198 | throw new NotFoundHttpException('Submission not found'); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $this->render( |
||
| 202 | 'view-submission', |
||
| 203 | [ |
||
| 204 | 'submission' => $submission, |
||
| 205 | ] |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function actionDeleteSubmission($id) |
||
| 234 | |||
| 235 | public function actionRestoreSubmission($id) |
||
| 247 | |||
| 248 | View Code Duplication | public function actionDownload($key, $submissionId) |
|
| 249 | { |
||
| 250 | $submission = Submission::findOne($submissionId); |
||
| 251 | if ($submission === null) { |
||
| 252 | throw new NotFoundHttpException('Submission not found'); |
||
| 253 | } |
||
| 254 | $prop = $submission->getPropertyValuesByKey($key); |
||
| 255 | return \Yii::$app->response->sendFile( |
||
| 256 | Yii::getAlias(Yii::$app->getModule('core')->visitorsFileUploadPath) . |
||
| 257 | DIRECTORY_SEPARATOR . |
||
| 258 | $prop->values[0]['value'] |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 262 | View Code Duplication | public function actionDelete($id = null) |
|
| 276 | |||
| 277 | public function actionRemoveAll() |
||
| 289 | } |
||
| 290 |
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.