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 | 37 | 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) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Check for expiration. |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | protected function checkExpiration() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get an empty record. |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | 2 | protected function getEmptyRecord($key, $type) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get a timestamp for the time the cache should expire. |
||
| 145 | * |
||
| 146 | * @param $type |
||
| 147 | * |
||
| 148 | * @return \Carbon\Carbon |
||
| 149 | */ |
||
| 150 | 2 | protected function getExpirationTimestamp($type) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get firewall. |
||
| 157 | * |
||
| 158 | * @return Firewall |
||
| 159 | */ |
||
| 160 | public function getFirewall() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Search geo localization by ip. |
||
| 167 | * |
||
| 168 | * @param $ipAddress |
||
| 169 | * |
||
| 170 | * @return array|null |
||
| 171 | */ |
||
| 172 | 2 | protected function getGeo($ipAddress) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Get the cache key. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getKey() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get max request count from config. |
||
| 189 | * |
||
| 190 | * @param string $type |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | 2 | protected function getMaxRequestCountForType($type = 'ip') |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get max seconds from config. |
||
| 203 | * |
||
| 204 | * @param $type |
||
| 205 | * |
||
| 206 | * @return int |
||
| 207 | */ |
||
| 208 | 2 | protected function getMaxSecondsForType($type) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get attack records. |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function getRecord() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the response configuration. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 2 | protected function getResponseConfig() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Increment request count. |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | protected function increment() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Check if this is an attack. |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | protected function isAttack() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Check for attacks. |
||
| 265 | * |
||
| 266 | * @param $ipAddress |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 2 | public function isBeingAttacked($ipAddress) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get enabled state. |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | 2 | protected function isEnabled() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Is the current user whitelisted? |
||
| 293 | * |
||
| 294 | * @param $type |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 2 | private function isWhitelisted($type) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Load the configuration. |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | private function loadConfig() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Load a record. |
||
| 318 | * |
||
| 319 | * @param $ipAddress |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | 2 | protected function loadRecord($ipAddress) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Load all record items. |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | protected function loadRecordItems() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Write to the log. |
||
| 350 | * |
||
| 351 | * @param $string |
||
| 352 | * |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | 2 | protected function log($string) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Send attack the the log. |
||
| 362 | * |
||
| 363 | * @param $record |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | 2 | protected function logAttack($record) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Make a response. |
||
| 374 | * |
||
| 375 | * @return null|\Illuminate\Http\Response |
||
| 376 | */ |
||
| 377 | 2 | public function responseToAttack() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Make a hashed key. |
||
| 386 | * |
||
| 387 | * @param $field |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 2 | public function makeHashedKey($field) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Make the cache key to record countries. |
||
| 401 | * |
||
| 402 | * @param $ipAddress |
||
| 403 | * |
||
| 404 | * @return string|null |
||
| 405 | */ |
||
| 406 | 2 | protected function makeKeyForType($type, $ipAddress) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Make a record. |
||
| 429 | * |
||
| 430 | * @param $key |
||
| 431 | * @param $type |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | 2 | protected function makeRecord($key, $type) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Send notifications. |
||
| 472 | * |
||
| 473 | * @param $record |
||
| 474 | * |
||
| 475 | * @return void |
||
| 476 | */ |
||
| 477 | 2 | protected function notify($record) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Renew first request timestamp, to keep the offender blocked. |
||
| 494 | * |
||
| 495 | * @param $record |
||
| 496 | * |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | 2 | protected function renew($record) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Set firewall. |
||
| 506 | * |
||
| 507 | * @param Firewall $firewall |
||
| 508 | * |
||
| 509 | * @return void |
||
| 510 | */ |
||
| 511 | 37 | public function setFirewall($firewall) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Store record on cache. |
||
| 518 | * |
||
| 519 | * @param $type |
||
| 520 | * @param array $items |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | 2 | protected function save($type, $items = []) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Take the necessary action to keep the offender blocked. |
||
| 541 | * |
||
| 542 | * @return void |
||
| 543 | */ |
||
| 544 | 2 | protected function takeAction($record) |
|
| 554 | } |
||
| 555 |
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: