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 | |||
23 | const BACKEND_REVIEW_EDIT = 'backend-review-edit'; |
||
24 | const BACKEND_REVIEW_EDIT_SAVE = 'backend-review-edit-save'; |
||
25 | const BACKEND_REVIEW_EDIT_FORM = 'backend-review-edit-form'; |
||
26 | const BACKEND_REVIEW_AFTER_SAVE = 'backend-review-after-save'; |
||
27 | |||
28 | |||
29 | View Code Duplication | public function behaviors() |
|
30 | { |
||
31 | return [ |
||
32 | 'access' => [ |
||
33 | 'class' => AccessControl::className(), |
||
34 | 'rules' => [ |
||
35 | [ |
||
36 | 'allow' => true, |
||
37 | 'roles' => ['review manage'], |
||
38 | ], |
||
39 | ], |
||
40 | ], |
||
41 | ]; |
||
42 | } |
||
43 | |||
44 | public function actionIndex() |
||
66 | |||
67 | public function actionView($id) |
||
68 | { |
||
69 | $model = Review::find() |
||
70 | ->with(['submission']) |
||
71 | ->where(['id' => $id]) |
||
72 | ->one(); |
||
73 | if (null === $model) { |
||
74 | throw new NotFoundHttpException; |
||
75 | } |
||
76 | |||
77 | View Code Duplication | if (true === Yii::$app->request->isPost) { |
|
78 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
||
79 | return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $this->render('edit', [ |
||
84 | 'review' => $model |
||
85 | ]); |
||
86 | } |
||
87 | |||
88 | public function actionUpdateStatus($id = null) |
||
89 | { |
||
90 | if (null === $id) { |
||
91 | $id = \Yii::$app->request->post('editableKey'); |
||
92 | $index = \Yii::$app->request->post('editableIndex'); |
||
93 | if (null === $id || null === $index) { |
||
94 | throw new BadRequestHttpException; |
||
95 | } else { |
||
96 | $review = $this->loadModel($id); |
||
97 | $reviews = \Yii::$app->request->post('Review', []); |
||
98 | $review->status = $reviews[$index]['status']; |
||
99 | return $review->update(); |
||
100 | } |
||
101 | } else { |
||
102 | $reviews = $reviews = \Yii::$app->request->post('Review'); |
||
103 | $status = $reviews['status']; |
||
104 | $review = $this->loadModel($id); |
||
105 | $review->status = $status; |
||
106 | if ($review->update()) { |
||
107 | Yii::$app->session->setFlash('success', Yii::t('app', 'Review successfully updated')); |
||
108 | } else { |
||
109 | Yii::$app->session->setFlash('error', Yii::t('app', 'Error occurred while updating review')); |
||
110 | } |
||
111 | return $this->redirect( |
||
112 | Url::toRoute( |
||
113 | [ |
||
114 | 'view', |
||
115 | 'id' => $review->id |
||
116 | ] |
||
117 | ) |
||
118 | ); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | public function actionMarkSpam($id, $spam = 1) |
||
123 | { |
||
124 | if ($spam === 1) { |
||
125 | $message = Yii::t('app', 'Entry successfully marked as spam'); |
||
126 | } else { |
||
127 | $message = Yii::t('app', 'Entry successfully marked as not spam'); |
||
128 | } |
||
129 | /** @var Submission $submission */ |
||
130 | $submission = Submission::findOne($id); |
||
131 | if (is_null($submission)) { |
||
132 | throw new NotFoundHttpException; |
||
133 | } |
||
134 | $submission->spam = $spam; |
||
135 | if ($spam == 1) { |
||
136 | /** @var Review $review */ |
||
137 | $review = Review::findOne(['submission_id' => $id]); |
||
138 | if (!is_null($review)) { |
||
139 | $review->status = Review::STATUS_NOT_APPROVED; |
||
140 | $review->save(true, ['status']); |
||
141 | } |
||
142 | } |
||
143 | if ($submission->save(true, ['spam'])) { |
||
144 | Yii::$app->session->setFlash('success', $message); |
||
145 | } |
||
146 | return $this->redirect( |
||
147 | Url::toRoute( |
||
148 | [ |
||
149 | 'view', |
||
150 | 'id' => $id |
||
151 | ] |
||
152 | ) |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param $id |
||
158 | * @param null $returnUrl |
||
159 | * @return Response |
||
160 | * @throws NotFoundHttpException |
||
161 | * @throws \Exception |
||
162 | */ |
||
163 | public function actionDelete($id, $returnUrl = null) |
||
176 | |||
177 | /** |
||
178 | * @param array $returnUrl |
||
179 | * @return Response |
||
180 | * @throws \Exception |
||
181 | */ |
||
182 | public function actionRemoveAll($returnUrl = ['index']) |
||
194 | |||
195 | /** |
||
196 | * Load review model by id |
||
197 | * @param $id |
||
198 | * @return Review |
||
199 | * @throws NotFoundHttpException |
||
200 | */ |
||
201 | protected function loadModel($id) |
||
209 | |||
210 | public function actionCreate($parent_id = 0) |
||
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | public function actionAjaxSearch() |
||
305 | |||
306 | /** |
||
307 | * @return array |
||
308 | */ |
||
309 | public function actionAjaxGetTree($root_id = null, $current_id = 0) |
||
336 | } |
||
337 |
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
@return
annotation as described here.