| Conditions | 10 |
| Paths | 7 |
| Total Lines | 25 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 10 |
| Changes | 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 |
||
| 20 | 2 | public function getByLoginPassword(string $login, string $password): ?User |
|
| 21 | { |
||
| 22 | /** @var User $user */ |
||
| 23 | 2 | $user = $this->getByLogin($login); |
|
| 24 | |||
| 25 | 2 | if (!$user || ($user->getActiveUntil() && $user->getActiveUntil() < new Chronos())) { |
|
|
1 ignored issue
–
show
|
|||
| 26 | 1 | return null; |
|
| 27 | } |
||
| 28 | |||
| 29 | 2 | $hashFromDb = $user->getPassword(); |
|
| 30 | 2 | $isMd5 = mb_strlen($hashFromDb) === 32 && ctype_xdigit($hashFromDb); |
|
| 31 | |||
| 32 | // If we found a user and he has a correct MD5 or correct new hash, then return the user |
||
| 33 | 2 | if (($isMd5 && md5($password) === $hashFromDb) || password_verify($password, $hashFromDb)) { |
|
| 34 | |||
| 35 | // Update the hash in DB, if we are still MD5, or if PHP default options changed |
||
| 36 | 2 | if ($isMd5 || password_needs_rehash($hashFromDb, PASSWORD_DEFAULT)) { |
|
| 37 | 2 | $user->setPassword($password); |
|
| 38 | 2 | _em()->flush(); |
|
| 39 | } |
||
| 40 | |||
| 41 | 2 | return $user; |
|
| 42 | } |
||
| 43 | |||
| 44 | 1 | return null; |
|
| 45 | } |
||
| 99 |