Complex classes like ProfileController 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 ProfileController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ProfileController extends Controller |
||
30 | { |
||
31 | /** |
||
32 | * @return array |
||
33 | */ |
||
34 | public function behaviors() |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | * @throws NotFoundHttpException |
||
50 | */ |
||
51 | public function actionIndex() |
||
57 | |||
58 | /** |
||
59 | * @return array|string|Response |
||
60 | * @throws NotFoundHttpException |
||
61 | */ |
||
62 | public function actionUpdate() |
||
72 | |||
73 | /** |
||
74 | * @return array|string|Response |
||
75 | * @throws NotFoundHttpException |
||
76 | */ |
||
77 | public function actionUpdatePassword() |
||
93 | |||
94 | /** |
||
95 | * @return string|Response |
||
96 | * @throws NotFoundHttpException |
||
97 | */ |
||
98 | public function actionUpdateProfile() |
||
110 | |||
111 | /** |
||
112 | * @return string|Response |
||
113 | * @throws NotFoundHttpException |
||
114 | */ |
||
115 | public function actionUpdateAvatar() |
||
138 | |||
139 | /** |
||
140 | * Deletes an existing User model. |
||
141 | * This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page. |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function actionDelete() |
||
153 | |||
154 | /** |
||
155 | * Logs in a user. |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function actionLogin() |
||
174 | |||
175 | /** |
||
176 | * Logs out the current user. |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function actionLogout() |
||
186 | |||
187 | /** |
||
188 | * Signs user up. |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | public function actionSignup() |
||
206 | |||
207 | /** |
||
208 | * @param $token |
||
209 | * @return \yii\web\Response |
||
210 | * @throws BadRequestHttpException |
||
211 | */ |
||
212 | public function actionEmailConfirm($token) |
||
228 | |||
229 | /** |
||
230 | * Requests password reset. |
||
231 | * |
||
232 | * @return mixed |
||
233 | */ |
||
234 | public function actionRequestPasswordReset() |
||
251 | |||
252 | /** |
||
253 | * Resets password. |
||
254 | * |
||
255 | * @param string $token |
||
256 | * @return mixed |
||
257 | * @throws BadRequestHttpException |
||
258 | */ |
||
259 | public function actionResetPassword($token) |
||
277 | |||
278 | /** |
||
279 | * Finds the User model based on its primary key value. |
||
280 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
281 | * @return User the loaded model |
||
282 | * @throws NotFoundHttpException if the model cannot be found |
||
283 | */ |
||
284 | protected function findModel() |
||
293 | } |
||
294 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.