| Conditions | 17 |
| Paths | 14 |
| Total Lines | 50 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 57 | public function check() |
||
| 58 | { |
||
| 59 | // check if user is auth'd or guest name is defined |
||
| 60 | if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
||
| 61 | throw new JsonException(__('Guest name is not defined')); |
||
| 62 | } |
||
| 63 | |||
| 64 | // check if target app_name or id is empty |
||
| 65 | if (Str::likeEmpty($this->appName) || Str::likeEmpty($this->appId) || (int)$this->appId < 0) { |
||
| 66 | throw new JsonException(__('Wrong target name or id')); |
||
| 67 | } |
||
| 68 | |||
| 69 | // check if message length is correct |
||
| 70 | if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
||
| 71 | throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', [ |
||
| 72 | 'cur' => Str::length($this->message), |
||
| 73 | 'min' => $this->_configs['minLength'], |
||
| 74 | 'max' => $this->_configs['maxLength'] |
||
| 75 | ])); |
||
| 76 | } |
||
| 77 | |||
| 78 | // guest moderation |
||
| 79 | if (!App::$User->isAuth() && (bool)$this->_configs['guestModerate']) { |
||
| 80 | $captcha = App::$Request->request->get('captcha'); |
||
| 81 | if (!App::$Captcha->validate($captcha)) { |
||
| 82 | throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again')); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | // check delay between 2 comments from 1 user or 1 ip |
||
| 87 | $query = CommentPost::where('user_id', $this->_userId) |
||
| 88 | ->orWhere('ip', $this->ip) |
||
| 89 | ->orderBy('created_at', 'DESC') |
||
| 90 | ->first(); |
||
| 91 | |||
| 92 | /** @var CommentPost $query */ |
||
| 93 | // check if latest post time for this user is founded |
||
| 94 | if ($query) { |
||
|
|
|||
| 95 | $isModerator = false; |
||
| 96 | if (App::$User->isAuth() && App::$User->identity()->role->can('global/modify')) { |
||
| 97 | $isModerator = true; |
||
| 98 | } |
||
| 99 | $postTime = Date::convertToTimestamp($query->created_at); |
||
| 100 | $delay = $postTime + $this->_configs['delay'] - time(); |
||
| 101 | if ($delay > 0 && !$isModerator) { |
||
| 102 | throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 131 |