Conditions | 13 |
Paths | 10 |
Total Lines | 59 |
Code Lines | 35 |
Lines | 24 |
Ratio | 40.68 % |
Changes | 1 | ||
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 |
||
51 | public function check() |
||
52 | { |
||
53 | // check if user is auth'd or guest name is defined |
||
54 | View Code Duplication | if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
|
55 | throw new JsonException(__('Guest name is not defined')); |
||
56 | } |
||
57 | |||
58 | // check if pathway is empty |
||
59 | if (Str::likeEmpty($this->pathway)) { |
||
60 | throw new JsonException(__('Wrong target pathway')); |
||
61 | } |
||
62 | |||
63 | // check if message length is correct |
||
64 | View Code Duplication | if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
|
65 | throw new JsonException(__('Message length is incorrect. Current: %cur% , min - %min%, max - %max%', [ |
||
66 | 'cur' => Str::length($this->message), |
||
67 | 'min' => $this->_configs['minLength'], |
||
68 | 'max' => $this->_configs['maxLength'] |
||
69 | ])); |
||
70 | } |
||
71 | |||
72 | // sounds like answer, lets try to find post thread comment |
||
73 | if ($this->replayTo > 0) { |
||
74 | $count = CommentPost::where('id', '=', $this->replayTo)->count(); |
||
75 | if ($count !== 1) { |
||
76 | throw new JsonException(__('Comment post thread is not founded')); |
||
77 | } |
||
78 | // check for prevent spam |
||
79 | $query = CommentAnswer::where(function($q) { |
||
80 | $q->where('user_id', '=', $this->user_id) |
||
81 | ->orWhere('ip', '=', $this->ip); |
||
82 | })->orderBy('created_at', 'DESC') |
||
83 | ->first(); |
||
84 | |||
85 | // something is founded :D |
||
86 | View Code Duplication | if ($query !== null) { |
|
87 | $answerTime = Date::convertToTimestamp($query->created_at); |
||
88 | $delay = $answerTime + $this->_configs['delay'] - time(); |
||
89 | if ($delay > 0) { // sounds like config time is not passed now |
||
90 | throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
||
91 | } |
||
92 | } |
||
93 | } else { // sounds like post, lets try to check latest post |
||
94 | $query = CommentPost::where(function($q) { |
||
95 | $q->where('user_id', '=', $this->user_id) |
||
96 | ->orWhere('ip', '=', $this->ip); |
||
97 | })->orderBy('created_at', 'DESC') |
||
98 | ->first(); |
||
99 | |||
100 | // check if latest post time for this user is founded |
||
101 | View Code Duplication | if ($query !== null) { |
|
102 | $postTime = Date::convertToTimestamp($query->created_at); |
||
103 | $delay = $postTime + $this->_configs['delay'] - time(); |
||
104 | if ($delay > 0) { |
||
105 | throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
139 | } |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.