Conditions | 6 |
Paths | 9 |
Total Lines | 58 |
Code Lines | 27 |
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 |
||
150 | public function checkAllowed($event) |
||
151 | { |
||
152 | $rule = $this->getRule($event); |
||
153 | |||
154 | // If no rule is set there must be no limit to how often an event can |
||
155 | // happen. |
||
156 | if (!$rule) { |
||
157 | return true; |
||
158 | } |
||
159 | |||
160 | $banClass = get_class($this->banEntry); |
||
161 | |||
162 | $filter = array( |
||
163 | 'Event' => $event, |
||
164 | 'IP' => $this->getIP() |
||
165 | ); |
||
166 | |||
167 | // If a rule has a ban time that is not 0 it means bans expire, so add a |
||
168 | // filter to take this into account. |
||
169 | if ($rule['bantime'] !== 0) { |
||
170 | $maxDate = $this->getPastDate($rule['bantime'])->format('c'); |
||
171 | $filter['Created:GreaterThan'] = $maxDate; |
||
172 | } |
||
173 | |||
174 | $bans = $banClass::get()->filter($filter); |
||
175 | |||
176 | // Check if a ban exists. |
||
177 | if ($bans->count() > 0) { |
||
178 | return false; |
||
179 | } |
||
180 | |||
181 | $maxDate = $this->getPastDate($rule['findtime'])->format('c'); |
||
182 | |||
183 | $entries = $this->getEntries($event)->filter( |
||
184 | array( |
||
185 | 'Created:GreaterThan' => $maxDate |
||
186 | ) |
||
187 | ); |
||
188 | |||
189 | // If there are no log entries the client must not have triggered this |
||
190 | // event before, so let it happen. |
||
191 | if (!$entries) { |
||
192 | return true; |
||
193 | } |
||
194 | |||
195 | // Check if the number of entries is greater than the number of hits |
||
196 | // allowed in findtime. |
||
197 | if ($entries->count() > $rule['hits']) { |
||
198 | $banEntry = $this->banEntry; |
||
199 | $banEntry->IP = $this->getIP(); |
||
200 | $banEntry->Event = $event; |
||
201 | $banEntry->write(); |
||
202 | |||
203 | return false; |
||
204 | } |
||
205 | |||
206 | return true; |
||
207 | } |
||
208 | } |
||
209 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.