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:
Complex classes like BackendReviewController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendReviewController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class BackendReviewController extends \app\backend\components\BackendController |
||
| 21 | { |
||
| 22 | public function behaviors() |
||
| 23 | { |
||
| 24 | return [ |
||
| 25 | 'access' => [ |
||
| 26 | 'class' => AccessControl::className(), |
||
| 27 | 'rules' => [ |
||
| 28 | [ |
||
| 29 | 'allow' => true, |
||
| 30 | 'roles' => ['review manage'], |
||
| 31 | ], |
||
| 32 | ], |
||
| 33 | ], |
||
| 34 | ]; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function actionIndex() |
||
| 59 | |||
| 60 | public function actionView($id) |
||
| 61 | { |
||
| 62 | $model = Review::find() |
||
| 63 | ->with(['submission']) |
||
| 64 | ->where(['id' => $id]) |
||
| 65 | ->one(); |
||
| 66 | if (null === $model) { |
||
| 67 | throw new NotFoundHttpException; |
||
| 68 | } |
||
| 69 | |||
| 70 | View Code Duplication | if (true === Yii::$app->request->isPost) { |
|
| 71 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
||
| 72 | return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->render('edit', [ |
||
| 77 | 'review' => $model |
||
| 78 | ]); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function actionUpdateStatus($id = null) |
||
| 82 | { |
||
| 83 | if (null === $id) { |
||
| 84 | $id = \Yii::$app->request->post('editableKey'); |
||
| 85 | $index = \Yii::$app->request->post('editableIndex'); |
||
| 86 | if (null === $id || null === $index) { |
||
| 87 | throw new BadRequestHttpException; |
||
| 88 | } else { |
||
| 89 | $review = $this->loadModel($id); |
||
| 90 | $reviews = \Yii::$app->request->post('Review', []); |
||
| 91 | $review->status = $reviews[$index]['status']; |
||
| 92 | return $review->update(); |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | $reviews = $reviews = \Yii::$app->request->post('Review'); |
||
| 96 | $status = $reviews['status']; |
||
| 97 | $review = $this->loadModel($id); |
||
| 98 | $review->status = $status; |
||
| 99 | if ($review->update()) { |
||
| 100 | Yii::$app->session->setFlash('success', Yii::t('app', 'Review successfully updated')); |
||
| 101 | } else { |
||
| 102 | Yii::$app->session->setFlash('error', Yii::t('app', 'Error occurred while updating review')); |
||
| 103 | } |
||
| 104 | return $this->redirect( |
||
| 105 | Url::toRoute( |
||
| 106 | [ |
||
| 107 | 'view', |
||
| 108 | 'id' => $review->id |
||
| 109 | ] |
||
| 110 | ) |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | public function actionMarkSpam($id, $spam = 1) |
||
| 116 | { |
||
| 117 | if ($spam === 1) { |
||
| 118 | $message = Yii::t('app', 'Entry successfully marked as spam'); |
||
| 119 | } else { |
||
| 120 | $message = Yii::t('app', 'Entry successfully marked as not spam'); |
||
| 121 | } |
||
| 122 | /** @var Submission $submission */ |
||
| 123 | $submission = Submission::findOne($id); |
||
| 124 | if (is_null($submission)) { |
||
| 125 | throw new NotFoundHttpException; |
||
| 126 | } |
||
| 127 | $submission->spam = $spam; |
||
| 128 | if ($spam == 1) { |
||
| 129 | /** @var Review $review */ |
||
| 130 | $review = Review::findOne(['submission_id' => $id]); |
||
| 131 | if (!is_null($review)) { |
||
| 132 | $review->status = Review::STATUS_NOT_APPROVED; |
||
| 133 | $review->save(true, ['status']); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | if ($submission->save(true, ['spam'])) { |
||
| 137 | Yii::$app->session->setFlash('success', $message); |
||
| 138 | } |
||
| 139 | return $this->redirect( |
||
| 140 | Url::toRoute( |
||
| 141 | [ |
||
| 142 | 'view', |
||
| 143 | 'id' => $id |
||
| 144 | ] |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $id |
||
| 151 | * @param null $returnUrl |
||
| 152 | * @return Response |
||
| 153 | * @throws NotFoundHttpException |
||
| 154 | * @throws \Exception |
||
| 155 | */ |
||
| 156 | public function actionDelete($id, $returnUrl = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $returnUrl |
||
| 172 | * @return Response |
||
| 173 | * @throws \Exception |
||
| 174 | */ |
||
| 175 | public function actionRemoveAll($returnUrl = ['index']) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Load review model by id |
||
| 190 | * @param $id |
||
| 191 | * @return Review |
||
| 192 | * @throws NotFoundHttpException |
||
| 193 | */ |
||
| 194 | protected function loadModel($id) |
||
| 202 | |||
| 203 | public function actionCreate($parent_id = 0) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | public function actionAjaxSearch() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function actionAjaxGetTree($root_id = null, $current_id = 0) |
||
| 329 | } |
||
| 330 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.