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 | 68 | public function __construct() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Blacklist the IP address. |
||
| 85 | * |
||
| 86 | * @param $record |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | 4 | protected function blacklist($record) |
|
| 91 | { |
||
| 92 | 4 | if ($record['isBlacklisted']) { |
|
| 93 | 1 | return false; |
|
| 94 | } |
||
| 95 | |||
| 96 | 4 | $blacklistUnknown = $this->config()->get("attack_blocker.action.{$record['type']}.blacklist_unknown"); |
|
| 97 | |||
| 98 | 4 | $blackWhitelisted = $this->config()->get("attack_blocker.action.{$record['type']}.blacklist_whitelisted"); |
|
| 99 | |||
| 100 | 4 | if ($blacklistUnknown || $blackWhitelisted) { |
|
| 101 | 2 | $record['isBlacklisted'] = true; |
|
| 102 | |||
| 103 | 2 | $ipAddress = $record['type'] == 'country' |
|
| 104 | ? 'country:'.$record['country_code'] |
||
| 105 | 2 | : $record['ipAddress']; |
|
| 106 | |||
| 107 | 2 | $this->firewall->blacklist($ipAddress, $blackWhitelisted); |
|
| 108 | |||
| 109 | 2 | $this->save($record); |
|
| 110 | |||
| 111 | 2 | return true; |
|
| 112 | } |
||
| 113 | |||
| 114 | 3 | return false; |
|
| 115 | } |
||
| 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 | 5 | 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 | 5 | protected function getExpirationTimestamp($type) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Search geo localization by ip. |
||
| 157 | * |
||
| 158 | * @param $ipAddress |
||
| 159 | * |
||
| 160 | * @return array|null |
||
| 161 | */ |
||
| 162 | 5 | protected function getGeo($ipAddress) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get max request count from config. |
||
| 169 | * |
||
| 170 | * @param string $type |
||
| 171 | * |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | 5 | protected function getMaxRequestCountForType($type = 'ip') |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Get max seconds from config. |
||
| 183 | * |
||
| 184 | * @param $type |
||
| 185 | * |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | 5 | protected function getMaxSecondsForType($type) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get the response configuration. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | 1 | protected function getResponseConfig() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Increment request count. |
||
| 207 | * |
||
| 208 | * @return void |
||
| 209 | */ |
||
| 210 | protected function increment() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Check if this is an attack. |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | protected function isAttack() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check for attacks. |
||
| 235 | * |
||
| 236 | * @param $ipAddress |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | 6 | public function isBeingAttacked($ipAddress) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get enabled state. |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 6 | protected function isEnabled() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Is the current user whitelisted? |
||
| 263 | * |
||
| 264 | * @param $type |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 5 | private function isWhitelisted($type) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Load the configuration. |
||
| 276 | * |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | private function loadConfig() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Load a record. |
||
| 288 | * |
||
| 289 | * @param $ipAddress |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 5 | protected function loadRecord($ipAddress) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Load all record items. |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | protected function loadRecordItems() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Write to the log. |
||
| 320 | * |
||
| 321 | * @param $string |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | 5 | protected function log($string) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Send attack the the log. |
||
| 332 | * |
||
| 333 | * @param $record |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | 4 | protected function logAttack($record) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Make a response. |
||
| 344 | * |
||
| 345 | * @return null|\Illuminate\Http\Response |
||
| 346 | */ |
||
| 347 | 1 | public function responseToAttack() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Make a hashed key. |
||
| 356 | * |
||
| 357 | * @param $field |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | 5 | public function makeHashedKey($field) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Make the cache key to record countries. |
||
| 371 | * |
||
| 372 | * @param $ipAddress |
||
| 373 | * |
||
| 374 | * @return string|null |
||
| 375 | */ |
||
| 376 | 5 | protected function makeKeyForType($type, $ipAddress) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Make a record. |
||
| 399 | * |
||
| 400 | * @param $key |
||
| 401 | * @param $type |
||
| 402 | * |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | 5 | protected function makeRecord($key, $type) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Send notifications. |
||
| 442 | * |
||
| 443 | * @param $record |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | 4 | protected function notify($record) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Renew first request timestamp, to keep the offender blocked. |
||
| 464 | * |
||
| 465 | * @param $record |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | 4 | protected function renew($record) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Set firewall. |
||
| 476 | * |
||
| 477 | * @param Firewall $firewall |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | 68 | public function setFirewall($firewall) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Store record on cache. |
||
| 488 | * |
||
| 489 | * @param $type |
||
| 490 | * @param array $items |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | 5 | protected function save($type, $items = []) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Take the necessary action to keep the offender blocked. |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | 4 | protected function takeAction($record) |
|
| 524 | } |
||
| 525 |
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: