Complex classes like IpList 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 IpList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class IpList |
||
| 9 | { |
||
| 10 | use ServiceInstances; |
||
| 11 | |||
| 12 | const IP_ADDRESS_LIST_CACHE_NAME = 'firewall.ip_address_list'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var \PragmaRX\Firewall\Vendor\Laravel\Models\Firewall |
||
| 16 | */ |
||
| 17 | private $model; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Create instance of DataRepository. |
||
| 21 | * |
||
| 22 | * @param \PragmaRX\Firewall\Vendor\Laravel\Models\Firewall $model |
||
| 23 | */ |
||
| 24 | 25 | public function __construct(FirewallModel $model) |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Get a list of non database ip addresses. |
||
| 31 | * |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | 23 | private function getNonDatabaseIps() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Remove ip from all array lists. |
||
| 53 | * |
||
| 54 | * @param $ipAddress |
||
| 55 | * |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | 2 | private function removeFromArrayList($ipAddress) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Remove the ip address from an array list. |
||
| 66 | * |
||
| 67 | * @param $type |
||
| 68 | * @param $ipAddress |
||
| 69 | * |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | 2 | private function removeFromArrayListType($type, $ipAddress) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Remove ip from database. |
||
| 87 | * |
||
| 88 | * @param \Illuminate\Database\Eloquent\Model $ipAddress |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | 2 | private function removeFromDatabaseList($ipAddress) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Transform a list of ips to a list of models. |
||
| 107 | * |
||
| 108 | * @param $ipList |
||
| 109 | * |
||
| 110 | * @return \Illuminate\Support\Collection |
||
| 111 | */ |
||
| 112 | 23 | private function toModels($ipList) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Make a model instance. |
||
| 125 | * |
||
| 126 | * @param $ip |
||
| 127 | * |
||
| 128 | * @return \Illuminate\Database\Eloquent\Model |
||
| 129 | */ |
||
| 130 | 11 | private function makeModel($ip) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Read a file contents. |
||
| 137 | * |
||
| 138 | * @param $file |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | private function readFile($file) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Format all ips in an array. |
||
| 155 | * |
||
| 156 | * @param $list |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | private function formatIpArray($list) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Make a list of arrays from all sort of things. |
||
| 169 | * |
||
| 170 | * @param $list |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 23 | private function makeArrayOfIps($list) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get a list of ips from anything. |
||
| 189 | * |
||
| 190 | * @param $item |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 11 | private function getIpsFromAnything($item) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Search for an ip in alist of ips. |
||
| 211 | * |
||
| 212 | * @param $ip |
||
| 213 | * @param $ips |
||
| 214 | * |
||
| 215 | * @return null|\Illuminate\Database\Eloquent\Model |
||
| 216 | */ |
||
| 217 | 22 | private function ipArraySearch($ip, $ips) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Get all IPs from database. |
||
| 232 | * |
||
| 233 | * @return \Illuminate\Support\Collection |
||
| 234 | */ |
||
| 235 | 23 | private function getAllFromDatabase() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Merge IP lists. |
||
| 246 | * |
||
| 247 | * @param $database_ips |
||
| 248 | * @param $config_ips |
||
| 249 | * |
||
| 250 | * @return \Illuminate\Support\Collection |
||
| 251 | */ |
||
| 252 | 23 | private function mergeLists($database_ips, $config_ips) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Check if an IP address is in a secondary (black/white) list. |
||
| 260 | * |
||
| 261 | * @param $ip_address |
||
| 262 | * |
||
| 263 | * @return bool|array |
||
| 264 | */ |
||
| 265 | 22 | public function checkSecondaryLists($ip_address) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Add an IP to black or whitelist. |
||
| 278 | * |
||
| 279 | * @param $whitelist |
||
| 280 | * @param $ip |
||
| 281 | * @param bool $force |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | 22 | public function addToList($whitelist, $ip, $force = false) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Tell in which list (black/white) an IP address is. |
||
| 324 | * |
||
| 325 | * @param $ip_address |
||
| 326 | * |
||
| 327 | * @return null|string |
||
| 328 | */ |
||
| 329 | 22 | public function whichList($ip_address) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Remove IP from all lists. |
||
| 346 | * |
||
| 347 | * @param $ip |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 4 | public function remove($ip) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Add ip or range to array list. |
||
| 370 | * |
||
| 371 | * @param $whitelist |
||
| 372 | * @param $ip |
||
| 373 | * |
||
| 374 | * @return array|mixed |
||
| 375 | */ |
||
| 376 | 11 | private function addToArrayList($whitelist, $ip) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Find an IP address in the data source. |
||
| 389 | * |
||
| 390 | * @param string $ip |
||
| 391 | * |
||
| 392 | * @return mixed |
||
| 393 | */ |
||
| 394 | 22 | public function find($ip) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Find an IP address by country. |
||
| 409 | * |
||
| 410 | * @param $country |
||
| 411 | * |
||
| 412 | * @return mixed |
||
| 413 | */ |
||
| 414 | 22 | public function findByCountry($country) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Find a Ip in the data source. |
||
| 423 | * |
||
| 424 | * @param string $ip |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | 20 | public function addToProperList($whitelist, $ip) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Delete ip address. |
||
| 437 | * |
||
| 438 | * @param $ipAddress |
||
| 439 | * |
||
| 440 | * @return bool|void |
||
| 441 | */ |
||
| 442 | 4 | public function delete($ipAddress) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Get all IP addresses. |
||
| 451 | * |
||
| 452 | * @return \Illuminate\Support\Collection |
||
| 453 | */ |
||
| 454 | 23 | public function all() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Find ip address in all lists. |
||
| 480 | * |
||
| 481 | * @param $ip |
||
| 482 | * |
||
| 483 | * @return \Illuminate\Database\Eloquent\Model|null|static |
||
| 484 | */ |
||
| 485 | 22 | private function findIp($ip) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Find ip in non database lists. |
||
| 498 | * |
||
| 499 | * @param $ip |
||
| 500 | * |
||
| 501 | * @return \Illuminate\Database\Eloquent\Model |
||
| 502 | */ |
||
| 503 | 22 | private function nonDatabaseFind($ip) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Add ip or range to database. |
||
| 514 | * |
||
| 515 | * @param $whitelist |
||
| 516 | * @param $ip |
||
| 517 | * |
||
| 518 | * @return \Illuminate\Database\Eloquent\Model |
||
| 519 | */ |
||
| 520 | 9 | private function addToDatabaseList($whitelist, $ip) |
|
| 533 | } |
||
| 534 |
This check looks for function calls that miss required arguments.