| Conditions | 8 |
| Paths | 9 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 121 | public function actionRecovery($id = null, $token = null) |
||
| 122 | { |
||
| 123 | if (App::$User->isAuth()) { // always auth? prevent any actions |
||
| 124 | throw new ForbiddenException(); |
||
| 125 | } |
||
| 126 | |||
| 127 | // is recovery submit? |
||
| 128 | if (Obj::isLikeInt($id) && Str::length($token) >= 64) { |
||
| 129 | $rObject = UserRecovery::where('id', '=', $id) |
||
| 130 | ->where('token', '=', $token) |
||
| 131 | ->where('archive', '=', false); |
||
| 132 | // check if recovery row exist |
||
| 133 | if ($rObject->count() !== 1) { |
||
| 134 | throw new NotFoundException('This recovery data is not found'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $rData = $rObject->first(); |
||
| 138 | // check if user with this "user_id" in recovery row exist |
||
| 139 | $rUser = App::$User->identity($rData->user_id); |
||
| 140 | if ($rUser === null) { |
||
| 141 | throw new NotFoundException('User is not found'); |
||
| 142 | } |
||
| 143 | |||
| 144 | // all is ok, lets set new pwd |
||
| 145 | $rUser->password = $rData->password; |
||
|
|
|||
| 146 | $rUser->save(); |
||
| 147 | |||
| 148 | $rData->archive = true; |
||
| 149 | $rData->save(); |
||
| 150 | |||
| 151 | // add notification |
||
| 152 | App::$Session->getFlashBag()->add('success', __('Your account are successful recovered. We recommend you change password')); |
||
| 153 | |||
| 154 | // lets open user session with recovered data |
||
| 155 | $loginModel = new FormLogin(); |
||
| 156 | $loginModel->openSession($rUser); |
||
| 157 | App::$Response->redirect('/'); // session is opened, refresh page |
||
| 158 | } |
||
| 159 | |||
| 160 | // lets work with recovery form data |
||
| 161 | $model = new FormRecovery(); |
||
| 162 | if ($model->send()) { |
||
| 163 | if ($model->validate()) { |
||
| 164 | $model->make(); |
||
| 165 | App::$Session->getFlashBag()->add('success', __('We send to you email with instruction to recovery your account')); |
||
| 166 | } else { |
||
| 167 | App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | // render visual form content |
||
| 172 | $this->response = App::$View->render('recovery', [ |
||
| 173 | 'model' => $model |
||
| 174 | ]); |
||
| 175 | } |
||
| 176 | |||
| 225 | } |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: