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 | 72 | public function __construct() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Blacklist the IP address. |
||
| 85 | * |
||
| 86 | * @param $record |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | 6 | protected function blacklist($record) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Check for expiration. |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | protected function checkExpiration() |
||
| 121 | { |
||
| 122 | 7 | $this->enabledItems->each(function ($index, $type) { |
|
| 123 | 7 | if (($this->now()->diffInSeconds($this->record[$type]['lastRequestAt'])) <= ($this->getMaxSecondsForType($type))) { |
|
| 124 | 7 | return $this->record; |
|
| 125 | } |
||
| 126 | |||
| 127 | 1 | return $this->record[$type] = $this->getEmptyRecord($this->record[$type]['key'], $type); |
|
| 128 | 7 | }); |
|
| 129 | 7 | } |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get an empty record. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 7 | protected function getEmptyRecord($key, $type) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get a timestamp for the time the cache should expire. |
||
| 143 | * |
||
| 144 | * @param $type |
||
| 145 | * |
||
| 146 | * @return \Carbon\Carbon |
||
| 147 | */ |
||
| 148 | 7 | protected function getExpirationTimestamp($type) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Search geo localization by ip. |
||
| 155 | * |
||
| 156 | * @param $ipAddress |
||
| 157 | * |
||
| 158 | * @return array|null |
||
| 159 | */ |
||
| 160 | 7 | protected function getGeo($ipAddress) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get max request count from config. |
||
| 167 | * |
||
| 168 | * @param string $type |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | 7 | protected function getMaxRequestCountForType($type = 'ip') |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get max seconds from config. |
||
| 181 | * |
||
| 182 | * @param $type |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | 7 | protected function getMaxSecondsForType($type) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get the response configuration. |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 2 | protected function getResponseConfig() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Increment request count. |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | protected function increment() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check if this is an attack. |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | protected function isAttack() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Check for attacks. |
||
| 233 | * |
||
| 234 | * @param $ipAddress |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 8 | public function isBeingAttacked($ipAddress) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get enabled state. |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | 8 | protected function isEnabled() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Is the current user whitelisted? |
||
| 261 | * |
||
| 262 | * @param $type |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 7 | private function isWhitelisted($type) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Load the configuration. |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | private function loadConfig() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Load a record. |
||
| 286 | * |
||
| 287 | * @param $ipAddress |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 7 | protected function loadRecord($ipAddress) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Load all record items. |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | protected function loadRecordItems() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Write to the log. |
||
| 318 | * |
||
| 319 | * @param $string |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | 7 | protected function log($string) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Send attack the the log. |
||
| 330 | * |
||
| 331 | * @param $record |
||
| 332 | * |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | 6 | protected function logAttack($record) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get the current date time. |
||
| 342 | * |
||
| 343 | * @return Carbon |
||
| 344 | */ |
||
| 345 | 7 | private function now() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Make a response. |
||
| 354 | * |
||
| 355 | * @return null|\Illuminate\Http\Response |
||
| 356 | */ |
||
| 357 | 3 | public function responseToAttack() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Make a hashed key. |
||
| 366 | * |
||
| 367 | * @param $field |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 7 | public function makeHashedKey($field) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Make the cache key to record countries. |
||
| 381 | * |
||
| 382 | * @param $ipAddress |
||
| 383 | * |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | 7 | protected function makeKeyForType($type, $ipAddress) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Make a record. |
||
| 409 | * |
||
| 410 | * @param $key |
||
| 411 | * @param $type |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | 7 | protected function makeRecord($key, $type) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Send notifications. |
||
| 452 | * |
||
| 453 | * @param $record |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | 6 | protected function notify($record) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Renew first request timestamp, to keep the offender blocked. |
||
| 474 | * |
||
| 475 | * @param $record |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | 6 | protected function renew($record) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Set firewall. |
||
| 486 | * |
||
| 487 | * @param Firewall $firewall |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | 72 | public function setFirewall($firewall) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Store record on cache. |
||
| 498 | * |
||
| 499 | * @param $type |
||
| 500 | * @param array $items |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | 7 | protected function save($type, $items = []) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Take the necessary action to keep the offender blocked. |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 6 | protected function takeAction($record) |
|
| 534 | } |
||
| 535 |
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: