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 | 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) |
||
| 87 | |||
| 88 | public function actionUpdateStatus($id = null) |
||
| 121 | |||
| 122 | public function actionMarkSpam($id, $spam = 1) |
||
| 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() |
||
| 247 | { |
||
| 248 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 249 | $search = \Yii::$app->request->get('search', []); |
||
| 250 | $object = !empty($search['object']) ? intval($search['object']) : 0; |
||
| 251 | $term = !empty($search['term']) ? $search['term'] : ''; |
||
| 252 | |||
| 253 | $result = [ |
||
| 254 | 'more' => false, |
||
| 255 | 'results' => [] |
||
| 256 | ]; |
||
| 257 | |||
| 258 | if (null === $object = Object::findById($object)) { |
||
| 259 | return $result; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** @var ActiveRecord $class */ |
||
| 263 | $class = $object->object_class; |
||
| 264 | $list = Object::find()->select("object_class")->column(); |
||
| 265 | if (!in_array($class, $list)) { |
||
| 266 | return $result; |
||
| 267 | } |
||
| 268 | |||
| 269 | $query = $class::find() |
||
| 270 | ->select('id, name, "#" `url`') |
||
| 271 | ->andWhere(['like', 'name', $term]) |
||
| 272 | ->asArray(true); |
||
| 273 | $result['results'] = array_values($query->all()); |
||
| 274 | array_walk($result['results'], |
||
| 275 | function (&$val) use ($class) |
||
| 276 | { |
||
| 277 | if (null !== $model = $class::findOne(['id' => $val['id']])) { |
||
| 278 | if (Product::className() === $model->className()) { |
||
| 279 | $val['url'] = Url::toRoute([ |
||
| 280 | '@product', |
||
| 281 | 'model' => $model, |
||
| 282 | 'category_group_id' => $model->category->category_group_id, |
||
| 283 | ]); |
||
| 284 | } elseif (Category::className() === $model->className()) { |
||
| 285 | $val['url'] = Url::toRoute([ |
||
| 286 | '@category', |
||
| 287 | 'last_category_id' => $model->id, |
||
| 288 | 'category_group_id' => $model->category_group_id, |
||
| 289 | ]); |
||
| 290 | } else if (Page::className() === $model->className()) { |
||
| 291 | $val['url'] = Url::toRoute([ |
||
| 292 | '@article', |
||
| 293 | 'id' => $model->id, |
||
| 294 | ]); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | }); |
||
| 298 | |||
| 299 | return $result; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function actionAjaxGetTree($root_id = null, $current_id = 0) |
||
| 332 | } |
||
| 333 |
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.