| Conditions | 7 |
| Paths | 5 |
| Total Lines | 59 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 48 | public function make() |
||
| 49 | { |
||
| 50 | $user = App::$User->getIdentityViaEmail($this->email); |
||
| 51 | if ($user === null) { |
||
| 52 | throw new SyntaxException('Email not found'); |
||
| 53 | } |
||
| 54 | if ($user->approve_token !== '0' && Str::length($user->approve_token) > 0) { |
||
|
|
|||
| 55 | throw new SyntaxException('You must approve your account'); |
||
| 56 | } |
||
| 57 | |||
| 58 | $rows = UserRecovery::where('user_id', '=', $user->getId()) |
||
| 59 | ->orderBy('id', 'DESC') |
||
| 60 | ->first(); |
||
| 61 | |||
| 62 | if ($rows !== null && $rows !== false) { |
||
| 63 | // prevent spam of recovery messages |
||
| 64 | if (Date::convertToTimestamp($rows->created_at) > time() - self::DELAY) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | // generate pwd, token and pwdCrypt |
||
| 70 | $newPwd = Str::randomLatinNumeric(mt_rand(8, 16)); |
||
| 71 | $pwdCrypt = App::$Security->password_hash($newPwd); |
||
| 72 | $token = Str::randomLatinNumeric(mt_rand(64, 128)); |
||
| 73 | |||
| 74 | // write new data to recovery table |
||
| 75 | $rObject = new UserRecovery(); |
||
| 76 | $rObject->user_id = $user->id; |
||
| 77 | $rObject->password = $pwdCrypt; |
||
| 78 | $rObject->token = $token; |
||
| 79 | $rObject->save(); |
||
| 80 | |||
| 81 | // write logs data |
||
| 82 | $log = new UserLog(); |
||
| 83 | $log->user_id = $user->id; |
||
| 84 | $log->type = 'RECOVERY'; |
||
| 85 | $log->message = __('Password recovery is initialized from: %ip%', ['ip' => App::$Request->getClientIp()]); |
||
| 86 | $log->save(); |
||
| 87 | |||
| 88 | // generate mail template |
||
| 89 | $mailTemplate = App::$View->render('user/mail/recovery', [ |
||
| 90 | 'login' => $user->login, |
||
| 91 | 'email' => $this->email, |
||
| 92 | 'password' => $newPwd, |
||
| 93 | 'token' => $token, |
||
| 94 | 'id' => $rObject->id |
||
| 95 | ]); |
||
| 96 | |||
| 97 | $sender = App::$Properties->get('adminEmail'); |
||
| 98 | |||
| 99 | // format SWIFTMailer format |
||
| 100 | $mailMessage = \Swift_Message::newInstance(App::$Translate->get('Profile', 'Account recovery on %site%', ['site' => App::$Request->getHost()])) |
||
| 101 | ->setFrom([$sender]) |
||
| 102 | ->setTo([$this->email]) |
||
| 103 | ->setBody($mailTemplate, 'text/html'); |
||
| 104 | // send message |
||
| 105 | App::$Mailer->send($mailMessage); |
||
| 106 | } |
||
| 107 | } |
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: