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 PostController 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 PostController, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 30 | class PostController extends Controller | ||
| 31 | { | ||
| 32 | /** | ||
| 33 | * @inheritdoc | ||
| 34 | */ | ||
| 35 | public function behaviors() | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Lists all Post models on specific post type. | ||
| 61 | * If there is user, the action will generate list of all Post models based on user. | ||
| 62 | * | ||
| 63 | * @param integer $type | ||
| 64 | * @param null|string $user | ||
| 65 | * @throws \yii\web\ForbiddenHttpException | ||
| 66 | * @throws \yii\web\NotFoundHttpException | ||
| 67 | * @return mixed | ||
| 68 | */ | ||
| 69 | public function actionIndex($type, $user = null) | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Creates a new Post model. | ||
| 89 | * If creation is successful, the browser will be redirected to the 'update' page. | ||
| 90 | * | ||
| 91 | * @param integer $type Post type ID | ||
| 92 | * @throws \yii\web\ForbiddenHttpException | ||
| 93 | * @throws \yii\web\NotFoundHttpException | ||
| 94 | * @return mixed | ||
| 95 | */ | ||
| 96 | public function actionCreate($type) | ||
| 138 | |||
| 139 | /** | ||
| 140 | * Updates an existing Post model. | ||
| 141 | * If update is successful, the browser will be redirected to the 'view' page. | ||
| 142 | * | ||
| 143 | * @param integer $id | ||
| 144 | * @throws \yii\web\ForbiddenHttpException | ||
| 145 | * @throws \yii\web\NotFoundHttpException | ||
| 146 | * @return mixed | ||
| 147 | */ | ||
| 148 | public function actionUpdate($id) | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Deletes an existing Post model. | ||
| 176 | * If deletion is successful, the browser will be redirected to the 'index' page. | ||
| 177 | * | ||
| 178 | * @param integer $id | ||
| 179 | * @throws \Exception | ||
| 180 | * @throws \yii\web\ForbiddenHttpException | ||
| 181 | * @throws \yii\web\NotFoundHttpException | ||
| 182 | * @return mixed | ||
| 183 | */ | ||
| 184 | public function actionDelete($id) | ||
| 198 | |||
| 199 | /** | ||
| 200 | * Bulk action for Post triggered when button 'Apply' clicked. | ||
| 201 | * The action depends on the value of the dropdown next to the button. | ||
| 202 | * Only accept POST HTTP method. | ||
| 203 | */ | ||
| 204 | public function actionBulkAction() | ||
| 249 | |||
| 250 | /** | ||
| 251 | * Search POST model via AJAX with JSON as the response. | ||
| 252 | */ | ||
| 253 | public function actionAjaxSearch() | ||
| 267 | |||
| 268 | /** | ||
| 269 | * Get permission to access model by current user. | ||
| 270 | * If the user does not obtain the permission, a 403 exeption will be thrown. | ||
| 271 | * | ||
| 272 | * @param $model Post | ||
| 273 | * @throws ForbiddenHttpException | ||
| 274 | */ | ||
| 275 | public function getPermission($model) | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Finds the Post model based on its primary key value. | ||
| 284 | * If the model is not found, a 404 HTTP exception will be thrown. | ||
| 285 | * | ||
| 286 | * @param integer $id | ||
| 287 | * @return Post the loaded model | ||
| 288 | * @throws NotFoundHttpException if the model cannot be found | ||
| 289 | */ | ||
| 290 | protected function findModel($id) | ||
| 298 | |||
| 299 | /** | ||
| 300 | * Finds the PostType model based on its primary key value. | ||
| 301 | * If the model is not found, a 404 HTTP exception will be thrown. | ||
| 302 | * | ||
| 303 | * @param integer $id | ||
| 304 | * @return PostType the loaded model | ||
| 305 | * @throws NotFoundHttpException if the model cannot be found | ||
| 306 | */ | ||
| 307 | protected function findPostType($id) | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Finds the Term model based on its primary key value. | ||
| 318 | * If the model is not found, it return false. | ||
| 319 | * | ||
| 320 | * @param integer $id | ||
| 321 | * @return Term|bool|null|static | ||
| 322 | */ | ||
| 323 | protected function findTerm($id) | ||
| 331 | } | ||
| 332 | 
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.