Conditions | 13 |
Paths | 14 |
Total Lines | 51 |
Code Lines | 27 |
Lines | 23 |
Ratio | 45.1 % |
Changes | 2 | ||
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 |
||
47 | public function check() |
||
48 | { |
||
49 | // check if user is auth'd or guest name is defined |
||
50 | View Code Duplication | if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
|
51 | throw new JsonException(__('Guest name is not defined')); |
||
52 | } |
||
53 | |||
54 | // guest moderation |
||
55 | View Code Duplication | if (!App::$User->isAuth() && (bool)$this->_configs['guestModerate']) { |
|
56 | $captcha = App::$Request->request->get('captcha'); |
||
57 | if (!App::$Captcha->validate($captcha)) { |
||
58 | throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again')); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | // check if replayTo is defined |
||
63 | if ($this->replayTo < 1) { |
||
64 | throw new JsonException(__('Comment post thread is not founded')); |
||
65 | } |
||
66 | |||
67 | // check if message length is correct |
||
68 | View Code Duplication | if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
|
69 | throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', [ |
||
70 | 'cur' => Str::length($this->message), |
||
71 | 'min' => $this->_configs['minLength'], |
||
72 | 'max' => $this->_configs['maxLength'] |
||
73 | ])); |
||
74 | } |
||
75 | |||
76 | $count = CommentPost::where('id', '=', $this->replayTo)->count(); |
||
77 | if ($count !== 1) { |
||
78 | throw new JsonException(__('Comment post thread is not founded')); |
||
79 | } |
||
80 | |||
81 | // check to prevent spam |
||
82 | $query = CommentAnswer::where('user_id', '=', $this->_userId) |
||
83 | ->orWhere('ip', '=', $this->ip) |
||
84 | ->orderBy('created_at', 'DESC') |
||
85 | ->first(); |
||
86 | |||
87 | // something is founded :D |
||
88 | View Code Duplication | if ($query !== null) { |
|
89 | $answerTime = Date::convertToTimestamp($query->created_at); |
||
90 | $delay = $answerTime + $this->_configs['delay'] - time(); |
||
91 | if ($delay > 0) { // sounds like config time is not passed now |
||
92 | throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
121 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.