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 |
||
| 10 | class AttackBlocker |
||
| 11 | { |
||
| 12 | use ServiceInstances; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The request record. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $record = [ |
||
| 20 | 'ip' => null, |
||
| 21 | |||
| 22 | 'country' => null, |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The ip address. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $ipAddress; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The cache key. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $key; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The max request count. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $maxRequestCount; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The max request count. |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $maxSeconds; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The firewall instance. |
||
| 55 | * |
||
| 56 | * @var Firewall |
||
| 57 | */ |
||
| 58 | protected $firewall; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The country. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $country; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The enabled items. |
||
| 69 | * |
||
| 70 | * @var \Illuminate\Support\Collection |
||
| 71 | */ |
||
| 72 | protected $enabledItems; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * AttackBlocker constructor. |
||
| 76 | */ |
||
| 77 | 75 | public function __construct() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Blacklist the IP address. |
||
| 84 | * |
||
| 85 | * @param $record |
||
| 86 | * |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | 4 | protected function blacklist($record) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Check for expiration. |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | protected function checkExpiration() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get an empty record. |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 8 | protected function getEmptyRecord($key, $type) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get a timestamp for the time the cache should expire. |
||
| 142 | * |
||
| 143 | * @param $type |
||
| 144 | * |
||
| 145 | * @return \Carbon\Carbon |
||
| 146 | */ |
||
| 147 | 8 | protected function getExpirationTimestamp($type) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Search geo localization by ip. |
||
| 154 | * |
||
| 155 | * @param $ipAddress |
||
| 156 | * |
||
| 157 | * @return array|null |
||
| 158 | */ |
||
| 159 | 8 | protected function getGeo($ipAddress) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get max request count from config. |
||
| 166 | * |
||
| 167 | * @param string $type |
||
| 168 | * |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | 8 | protected function getMaxRequestCountForType($type = 'ip') |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Get max seconds from config. |
||
| 180 | * |
||
| 181 | * @param $type |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | 8 | protected function getMaxSecondsForType($type) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get the response configuration. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | 2 | protected function getResponseConfig() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Increment request count. |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | protected function increment() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Check if this is an attack. |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | protected function isAttack() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check for attacks. |
||
| 232 | * |
||
| 233 | * @param $ipAddress |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 8 | public function isBeingAttacked($ipAddress) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get enabled state. |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | 8 | protected function isEnabled() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Is the current user whitelisted? |
||
| 260 | * |
||
| 261 | * @param $type |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | 8 | private function isWhitelisted($type) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Load the configuration. |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | private function loadConfig() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Load a record. |
||
| 285 | * |
||
| 286 | * @param $ipAddress |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | 8 | protected function loadRecord($ipAddress) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Load all record items. |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | protected function loadRecordItems() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Write to the log. |
||
| 317 | * |
||
| 318 | * @param $string |
||
| 319 | * |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | 4 | protected function log($string) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Send attack the the log. |
||
| 329 | * |
||
| 330 | * @param $record |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | 4 | protected function logAttack($record) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get the current date time. |
||
| 341 | * |
||
| 342 | * @return Carbon |
||
| 343 | */ |
||
| 344 | 8 | private function now() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Make a response. |
||
| 353 | * |
||
| 354 | * @return null|\Illuminate\Http\Response |
||
| 355 | */ |
||
| 356 | 3 | public function responseToAttack() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Make a hashed key. |
||
| 365 | * |
||
| 366 | * @param $field |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | 8 | public function makeHashedKey($field) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Make the cache key to record countries. |
||
| 380 | * |
||
| 381 | * @param $ipAddress |
||
| 382 | * |
||
| 383 | * @return string|null |
||
| 384 | */ |
||
| 385 | 8 | protected function makeKeyForType($type, $ipAddress) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Make a record. |
||
| 408 | * |
||
| 409 | * @param $key |
||
| 410 | * @param $type |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | 8 | protected function makeRecord($key, $type) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Send notifications. |
||
| 451 | * |
||
| 452 | * @param $record |
||
| 453 | * |
||
| 454 | * @return void |
||
| 455 | */ |
||
| 456 | 4 | protected function notify($record) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Renew first request timestamp, to keep the offender blocked. |
||
| 469 | * |
||
| 470 | * @param $record |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | 4 | protected function renew($record) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Set firewall. |
||
| 481 | * |
||
| 482 | * @param Firewall $firewall |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | 75 | public function setFirewall($firewall) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Store record on cache. |
||
| 493 | * |
||
| 494 | * @param $type |
||
| 495 | * @param array $items |
||
| 496 | * |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | 8 | protected function save($type, $items = []) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Take the necessary action to keep the offender blocked. |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | 4 | protected function takeAction($record) |
|
| 529 | } |
||
| 530 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: