| Conditions | 10 |
| Paths | 8 |
| Total Lines | 50 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 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 |
||
| 11 | public function __invoke(array $post) { |
||
| 12 | |||
| 13 | if (Auth::check()) return true; |
||
| 14 | |||
| 15 | # Declare variables |
||
| 16 | |||
| 17 | $name = ''; $captcha = ''; |
||
| 18 | |||
| 19 | # Extract post array |
||
| 20 | |||
| 21 | extract($post); |
||
| 22 | |||
| 23 | # Validate values |
||
| 24 | |||
| 25 | if (false === ($name = Validate::userName($name))) return 'USER_ERROR_NAME_INVALID'; |
||
| 26 | |||
| 27 | if (false === Security::checkCaptcha($captcha)) return 'USER_ERROR_CAPTCHA_INCORRECT'; |
||
| 28 | |||
| 29 | # Create user object |
||
| 30 | |||
| 31 | $user = Entitizer::get(ENTITY_TYPE_USER); |
||
| 32 | |||
| 33 | # Init user |
||
| 34 | |||
| 35 | if (!$user->init($name, 'name')) return 'USER_ERROR_NAME_INCORRECT'; |
||
| 36 | |||
| 37 | if (Auth::admin() && ($user->rank < RANK_ADMINISTRATOR)) return 'USER_ERROR_NAME_INCORRECT'; |
||
| 38 | |||
| 39 | # Check access |
||
| 40 | |||
| 41 | if (!Auth::admin() && ($user->rank === RANK_GUEST)) return 'USER_ERROR_ACCESS'; |
||
| 42 | |||
| 43 | # Create session |
||
| 44 | |||
| 45 | $secret = Entitizer::get(ENTITY_TYPE_USER_SECRET, $user->id); $secret->remove(); |
||
| 46 | |||
| 47 | $code = Str::random(40); $ip = REQUEST_CLIENT_IP; $time = REQUEST_TIME; |
||
| 48 | |||
| 49 | $data = ['id' => $user->id, 'code' => $code, 'ip' => $ip, 'time' => $time]; |
||
| 50 | |||
| 51 | if (!$secret->create($data)) return 'USER_ERROR_AUTH_RESET'; |
||
| 52 | |||
| 53 | # Send mail |
||
| 54 | |||
| 55 | Auth\Utils\Mail::reset($user, $code); |
||
| 56 | |||
| 57 | # ------------------------ |
||
| 58 | |||
| 59 | return true; |
||
| 60 | } |
||
| 61 | } |
||
| 63 |