Complex classes like IndexController 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 IndexController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class IndexController extends BaseController |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @inheritdoc |
||
| 23 | */ |
||
| 24 | 57 | public function behaviors() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | 57 | public function actions() |
|
| 68 | |||
| 69 | 8 | public function successCallback($client) |
|
| 77 | |||
| 78 | 25 | public function actionIndex() |
|
| 82 | |||
| 83 | 16 | public function actionLogin() |
|
| 98 | |||
| 99 | 15 | public function actionSignup() |
|
| 100 | { |
||
| 101 | 15 | $model = new SignupForm(); |
|
| 102 | 15 | if ($model->load(Yii::$app->request->post()) && $model->signup()) { |
|
| 103 | 3 | if ($model->sendEmail()) { |
|
| 104 | 1 | Yii::$app->session->setFlash( |
|
| 105 | 1 | 'success', |
|
| 106 | 1 | Yii::t( |
|
| 107 | 1 | 'app.messages', |
|
| 108 | 1 | 'Please activate your account' |
|
| 109 | 1 | ) . '. ' . |
|
| 110 | 1 | Yii::t( |
|
| 111 | 1 | 'app.messages', |
|
| 112 | 1 | 'A letter for activation was sent to {email}', |
|
| 113 | 1 | ['email' => $model->email] |
|
| 114 | ) |
||
| 115 | ); |
||
| 116 | } else { |
||
| 117 | 2 | Yii::$app->session->setFlash( |
|
| 118 | 2 | 'error', |
|
| 119 | 2 | Yii::t( |
|
| 120 | 2 | 'app.messages', |
|
| 121 | 2 | 'An error occurred while sending a message to activate account' |
|
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | 3 | return $this->goHome(); |
|
| 126 | } |
||
| 127 | |||
| 128 | 15 | return $this->render('signup', [ |
|
| 129 | 15 | 'model' => $model, |
|
| 130 | ]); |
||
| 131 | } |
||
| 132 | |||
| 133 | 9 | public function actionSignupProvider() |
|
| 134 | { |
||
| 135 | 9 | $session = Yii::$app->session; |
|
| 136 | 9 | $provider = $session['provider']; |
|
| 137 | 9 | if (!Yii::$app->user->isGuest || $provider === null) { |
|
| 138 | 1 | return $this->goHome(); |
|
| 139 | } |
||
| 140 | |||
| 141 | 8 | $model = new SignupProviderForm($provider); |
|
| 142 | |||
| 143 | // check exist user and provider |
||
| 144 | 8 | if ($user = User::findByProvider($provider['type'], $provider['profile']['id'])) { |
|
| 145 | 2 | $session['provider'] = null; |
|
| 146 | 2 | if ($user->isActive()) { |
|
| 147 | 1 | $user->updateProvider($model->parseProvider()); |
|
|
|
|||
| 148 | 1 | $user->authorize(true); |
|
| 149 | 1 | return $this->goHome(); |
|
| 150 | } else { |
||
| 151 | 1 | $session->setFlash('error', $user->getStatusDescription()); |
|
| 152 | 1 | return $this->goHome(); |
|
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | 8 | $model->prepareUser(); |
|
| 157 | |||
| 158 | 8 | if ($model->isVerifiedEmail() && $model->signup(false)) { |
|
| 159 | 1 | $session['provider'] = null; |
|
| 160 | 1 | return $this->goHome(); |
|
| 161 | } |
||
| 162 | |||
| 163 | 7 | if ($model->load(Yii::$app->request->post()) && $model->signup()) { |
|
| 164 | 5 | if ($model->sendEmail()) { |
|
| 165 | 4 | $session->setFlash( |
|
| 166 | 4 | 'success', |
|
| 167 | 4 | Yii::t( |
|
| 168 | 4 | 'app.messages', |
|
| 169 | 4 | 'Please activate your account' |
|
| 170 | 4 | ) . '. ' . |
|
| 171 | 4 | Yii::t( |
|
| 172 | 4 | 'app.messages', |
|
| 173 | 4 | 'A letter for activation was sent to {email}', |
|
| 174 | 4 | ['email' => $model->email] |
|
| 175 | ) |
||
| 176 | ); |
||
| 177 | } else { |
||
| 178 | 1 | $session->setFlash( |
|
| 179 | 1 | 'error', |
|
| 180 | 1 | Yii::t( |
|
| 181 | 1 | 'app.messages', |
|
| 182 | 1 | 'An error occurred while sending a message to activate account' |
|
| 183 | ) |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | 5 | $session['provider'] = null; |
|
| 187 | 5 | return $this->goHome(); |
|
| 188 | } |
||
| 189 | |||
| 190 | 7 | return $this->render('signupProvider', [ |
|
| 191 | 7 | 'model' => $model |
|
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | 3 | public function actionConfirmRequest() |
|
| 196 | { |
||
| 197 | 3 | $user = Yii::$app->user->identity; |
|
| 198 | 3 | if ($user->isConfirmed()) { |
|
| 199 | 1 | Http::exception(403); |
|
| 200 | } // @codeCoverageIgnore |
||
| 201 | |||
| 202 | 2 | $model = new ConfirmEmailForm(); |
|
| 203 | |||
| 204 | 2 | if ($model->sendEmail($user)) { |
|
| 205 | 1 | Yii::$app->session->setFlash( |
|
| 206 | 1 | 'success', |
|
| 207 | 1 | Yii::t( |
|
| 208 | 1 | 'app.messages', |
|
| 209 | 1 | 'A letter for activation was sent to {email}', |
|
| 210 | 1 | ['email' => $user->email] |
|
| 211 | ) |
||
| 212 | ); |
||
| 213 | } else { |
||
| 214 | 1 | Yii::$app->session->setFlash( |
|
| 215 | 1 | 'error', |
|
| 216 | 1 | Yii::t( |
|
| 217 | 1 | 'app.messages', |
|
| 218 | 1 | 'An error occurred while sending a message to activate account' |
|
| 219 | ) |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | 2 | return $this->goHome(); |
|
| 223 | } |
||
| 224 | |||
| 225 | 3 | public function actionConfirmEmail($token) |
|
| 245 | |||
| 246 | 8 | public function actionRequestPasswordReset() |
|
| 247 | { |
||
| 248 | 8 | $model = new PasswordResetRequestForm(); |
|
| 249 | |||
| 250 | 8 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
|
| 251 | 2 | if ($model->sendEmail()) { |
|
| 252 | 1 | Yii::$app->session->setFlash( |
|
| 253 | 1 | 'success', |
|
| 254 | 1 | Yii::t( |
|
| 255 | 1 | 'app.messages', |
|
| 256 | 1 | 'We\'ve sent you an email with instructions to reset your password' |
|
| 257 | ) |
||
| 258 | ); |
||
| 259 | } else { |
||
| 260 | 1 | Yii::$app->session->setFlash( |
|
| 261 | 1 | 'error', |
|
| 262 | 1 | Yii::t( |
|
| 263 | 1 | 'app.messages', |
|
| 264 | 1 | 'An error occurred while sending a message to reset your password' |
|
| 265 | ) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | 2 | return $this->goHome(); |
|
| 269 | } |
||
| 270 | |||
| 271 | 8 | return $this->render('requestPasswordResetToken', [ |
|
| 272 | 8 | 'model' => $model, |
|
| 273 | ]); |
||
| 274 | } |
||
| 275 | |||
| 276 | 6 | public function actionResetPassword($token) |
|
| 303 | |||
| 304 | 1 | public function actionLogout() |
|
| 309 | |||
| 310 | /** @see commands/MaintenanceController **/ |
||
| 311 | 2 | public function actionMaintenance() |
|
| 320 | } |
||
| 321 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.