Complex classes like AttackBlocker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AttackBlocker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class AttackBlocker |
||
12 | { |
||
13 | use ServiceInstances; |
||
14 | |||
15 | /** |
||
16 | * The request record. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $record = [ |
||
21 | 'ip' => null, |
||
22 | |||
23 | 'country' => null, |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * The ip address. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $ipAddress; |
||
32 | |||
33 | /** |
||
34 | * The cache key. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $key; |
||
39 | |||
40 | /** |
||
41 | * The max request count. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $maxRequestCount; |
||
46 | |||
47 | /** |
||
48 | * The max request count. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $maxSeconds; |
||
53 | |||
54 | /** |
||
55 | * The firewall instance. |
||
56 | * |
||
57 | * @var Firewall |
||
58 | */ |
||
59 | protected $firewall; |
||
60 | |||
61 | /** |
||
62 | * The country. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $country; |
||
67 | |||
68 | /** |
||
69 | * The enabled items. |
||
70 | * |
||
71 | * @var \Illuminate\Support\Collection |
||
72 | */ |
||
73 | protected $enabledItems; |
||
74 | |||
75 | /** |
||
76 | * AttackBlocker constructor. |
||
77 | */ |
||
78 | 66 | public function __construct() |
|
82 | |||
83 | /** |
||
84 | * Blacklist the IP address. |
||
85 | * |
||
86 | * @param $record |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | 2 | protected function blacklist($record) |
|
114 | |||
115 | /** |
||
116 | * Check for expiration. |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | protected function checkExpiration() |
||
133 | |||
134 | /** |
||
135 | * Get an empty record. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 2 | protected function getEmptyRecord($key, $type) |
|
143 | |||
144 | /** |
||
145 | * Get a timestamp for the time the cache should expire. |
||
146 | * |
||
147 | * @param $type |
||
148 | * |
||
149 | * @return \Carbon\Carbon |
||
150 | */ |
||
151 | 2 | protected function getExpirationTimestamp($type) |
|
155 | |||
156 | /** |
||
157 | * Search geo localization by ip. |
||
158 | * |
||
159 | * @param $ipAddress |
||
160 | * |
||
161 | * @return array|null |
||
162 | */ |
||
163 | 2 | protected function getGeo($ipAddress) |
|
167 | |||
168 | /** |
||
169 | * Get max request count from config. |
||
170 | * |
||
171 | * @param string $type |
||
172 | * |
||
173 | * @return int |
||
174 | */ |
||
175 | 2 | protected function getMaxRequestCountForType($type = 'ip') |
|
181 | |||
182 | /** |
||
183 | * Get max seconds from config. |
||
184 | * |
||
185 | * @param $type |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | 2 | protected function getMaxSecondsForType($type) |
|
195 | |||
196 | /** |
||
197 | * Get the response configuration. |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 1 | protected function getResponseConfig() |
|
205 | |||
206 | /** |
||
207 | * Increment request count. |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | protected function increment() |
||
217 | |||
218 | /** |
||
219 | * Check if this is an attack. |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | protected function isAttack() |
||
233 | |||
234 | /** |
||
235 | * Check for attacks. |
||
236 | * |
||
237 | * @param $ipAddress |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | 2 | public function isBeingAttacked($ipAddress) |
|
251 | |||
252 | /** |
||
253 | * Get enabled state. |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | 2 | protected function isEnabled() |
|
261 | |||
262 | /** |
||
263 | * Is the current user whitelisted? |
||
264 | * |
||
265 | * @param $type |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | 2 | private function isWhitelisted($type) |
|
274 | |||
275 | /** |
||
276 | * Load the configuration. |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | private function loadConfig() |
||
286 | |||
287 | /** |
||
288 | * Load a record. |
||
289 | * |
||
290 | * @param $ipAddress |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | 2 | protected function loadRecord($ipAddress) |
|
304 | |||
305 | /** |
||
306 | * Load all record items. |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | protected function loadRecordItems() |
||
318 | |||
319 | /** |
||
320 | * Write to the log. |
||
321 | * |
||
322 | * @param $string |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | 2 | protected function log($string) |
|
330 | |||
331 | /** |
||
332 | * Send attack the the log. |
||
333 | * |
||
334 | * @param $record |
||
335 | * |
||
336 | * @return void |
||
337 | */ |
||
338 | 2 | protected function logAttack($record) |
|
342 | |||
343 | /** |
||
344 | * Get the current date time. |
||
345 | * |
||
346 | * @return Carbon |
||
347 | */ |
||
348 | 2 | private function now() |
|
354 | |||
355 | /** |
||
356 | * Make a response. |
||
357 | * |
||
358 | * @return null|\Illuminate\Http\Response |
||
359 | */ |
||
360 | 2 | public function responseToAttack() |
|
366 | |||
367 | /** |
||
368 | * Make a hashed key. |
||
369 | * |
||
370 | * @param $field |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | 2 | public function makeHashedKey($field) |
|
381 | |||
382 | /** |
||
383 | * Make the cache key to record countries. |
||
384 | * |
||
385 | * @param $ipAddress |
||
386 | * |
||
387 | * @return string|null |
||
388 | */ |
||
389 | 2 | protected function makeKeyForType($type, $ipAddress) |
|
409 | |||
410 | /** |
||
411 | * Make a record. |
||
412 | * |
||
413 | * @param $key |
||
414 | * @param $type |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | 2 | protected function makeRecord($key, $type) |
|
452 | |||
453 | /** |
||
454 | * Send notifications. |
||
455 | * |
||
456 | * @param $record |
||
457 | * |
||
458 | * @return void |
||
459 | */ |
||
460 | 2 | protected function notify($record) |
|
474 | |||
475 | /** |
||
476 | * Renew first request timestamp, to keep the offender blocked. |
||
477 | * |
||
478 | * @param $record |
||
479 | * |
||
480 | * @return void |
||
481 | */ |
||
482 | 2 | protected function renew($record) |
|
486 | |||
487 | /** |
||
488 | * Set firewall. |
||
489 | * |
||
490 | * @param Firewall $firewall |
||
491 | * |
||
492 | * @return void |
||
493 | */ |
||
494 | 66 | public function setFirewall($firewall) |
|
498 | |||
499 | /** |
||
500 | * Store record on cache. |
||
501 | * |
||
502 | * @param $type |
||
503 | * @param array $items |
||
504 | * |
||
505 | * @return array |
||
506 | */ |
||
507 | 2 | protected function save($type, $items = []) |
|
521 | |||
522 | /** |
||
523 | * Take the necessary action to keep the offender blocked. |
||
524 | * |
||
525 | * @return void |
||
526 | */ |
||
527 | 2 | protected function takeAction($record) |
|
537 | } |
||
538 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.