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 | 60 | public function __construct(FirewallModel $model) |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Get a list of non database ip addresses. |
||
| 31 | * |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | 56 | private function getNonDatabaseIps() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Remove ip from all array lists. |
||
| 53 | * |
||
| 54 | * @param $ip |
||
| 55 | * |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | 4 | private function removeFromArrayList($ip) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Remove the ip address from an array list. |
||
| 66 | * |
||
| 67 | * @param $type |
||
| 68 | * @param $ip |
||
| 69 | * |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | 4 | private function removeFromArrayListType($type, $ip) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Remove ip from database. |
||
| 91 | * |
||
| 92 | * @param \Illuminate\Database\Eloquent\Model $ip |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 4 | private function removeFromDatabaseList($ip) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Transform a list of ips to a list of models. |
||
| 109 | * |
||
| 110 | * @param $ipList |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Support\Collection |
||
| 113 | */ |
||
| 114 | 56 | private function toModels($ipList) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Make a model instance. |
||
| 127 | * |
||
| 128 | * @param $ip |
||
| 129 | * |
||
| 130 | * @return \Illuminate\Database\Eloquent\Model |
||
| 131 | */ |
||
| 132 | 24 | private function makeModel($ip) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Read a file contents. |
||
| 139 | * |
||
| 140 | * @param $file |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | private function readFile($file) |
||
| 145 | { |
||
| 146 | if ($this->fileSystem()->exists($file)) { |
||
| 147 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
||
| 148 | |||
| 149 | return $this->makeArrayOfIps($lines); |
||
| 150 | } |
||
| 151 | |||
| 152 | return []; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Format all ips in an array. |
||
| 157 | * |
||
| 158 | * @param $list |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | 56 | private function formatIpArray($list) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Make a list of arrays from all sort of things. |
||
| 171 | * |
||
| 172 | * @param $list |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | 56 | private function makeArrayOfIps($list) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get a list of ips from anything. |
||
| 191 | * |
||
| 192 | * @param $item |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 24 | private function getIpsFromAnything($item) |
|
| 197 | { |
||
| 198 | 24 | if (starts_with($item, 'country:')) { |
|
| 199 | 2 | return [$item]; |
|
| 200 | } |
||
| 201 | |||
| 202 | 22 | $item = $this->ipAddress()->hostToIp($item); |
|
| 203 | |||
| 204 | 22 | if ($this->ipAddress()->ipV4Valid($item)) { |
|
| 205 | 22 | return [$item]; |
|
| 206 | } |
||
| 207 | |||
| 208 | return $this->readFile($item); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Search for an ip in alist of ips. |
||
| 213 | * |
||
| 214 | * @param $ip |
||
| 215 | * @param $ips |
||
| 216 | * |
||
| 217 | * @return null|\Illuminate\Database\Eloquent\Model |
||
| 218 | */ |
||
| 219 | 52 | private function ipArraySearch($ip, $ips) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get all IPs from database. |
||
| 234 | * |
||
| 235 | * @return \Illuminate\Support\Collection |
||
| 236 | */ |
||
| 237 | 56 | private function getAllFromDatabase() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Merge IP lists. |
||
| 248 | * |
||
| 249 | * @param $database_ips |
||
| 250 | * @param $config_ips |
||
| 251 | * |
||
| 252 | * @return \Illuminate\Support\Collection |
||
| 253 | */ |
||
| 254 | 56 | private function mergeLists($database_ips, $config_ips) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Check if an IP address is in a secondary (black/white) list. |
||
| 262 | * |
||
| 263 | * @param $ip_address |
||
| 264 | * |
||
| 265 | * @return bool|array |
||
| 266 | */ |
||
| 267 | 52 | public function checkSecondaryLists($ip_address) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Add an IP to black or whitelist. |
||
| 280 | * |
||
| 281 | * @param $whitelist |
||
| 282 | * @param $ip |
||
| 283 | * @param bool $force |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | 43 | 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 | 52 | public function whichList($ip_address) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Remove IP from all lists. |
||
| 346 | * |
||
| 347 | * @param $ip |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 8 | public function remove($ip) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Add ip or range to array list. |
||
| 368 | * |
||
| 369 | * @param $whitelist |
||
| 370 | * @param $ip |
||
| 371 | * |
||
| 372 | * @return array|mixed |
||
| 373 | */ |
||
| 374 | 24 | private function addToArrayList($whitelist, $ip) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Find an IP address in the data source. |
||
| 387 | * |
||
| 388 | * @param string $ip |
||
| 389 | * |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | 52 | public function find($ip) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Find an IP address by country. |
||
| 407 | * |
||
| 408 | * @param $country |
||
| 409 | * |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | 52 | public function findByCountry($country) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Find a Ip in the data source. |
||
| 421 | * |
||
| 422 | * @param string $ip |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | 39 | public function addToProperList($whitelist, $ip) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Delete ip address. |
||
| 435 | * |
||
| 436 | * @param $ip |
||
| 437 | * |
||
| 438 | * @return bool|void |
||
| 439 | */ |
||
| 440 | 8 | public function delete($ip) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Get all IP addresses. |
||
| 449 | * |
||
| 450 | * @return \Illuminate\Support\Collection |
||
| 451 | */ |
||
| 452 | 56 | public function all() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Find ip address in all lists. |
||
| 474 | * |
||
| 475 | * @param $ip |
||
| 476 | * |
||
| 477 | * @return \Illuminate\Database\Eloquent\Model|null|static |
||
| 478 | */ |
||
| 479 | 52 | private function findIp($ip) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Find ip in non database lists. |
||
| 492 | * |
||
| 493 | * @param $ip |
||
| 494 | * |
||
| 495 | * @return \Illuminate\Database\Eloquent\Model |
||
| 496 | */ |
||
| 497 | 52 | private function nonDatabaseFind($ip) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Add ip or range to database. |
||
| 508 | * |
||
| 509 | * @param $whitelist |
||
| 510 | * @param $ip |
||
| 511 | * |
||
| 512 | * @return \Illuminate\Database\Eloquent\Model |
||
| 513 | */ |
||
| 514 | 15 | private function addToDatabaseList($whitelist, $ip) |
|
| 527 | } |
||
| 528 |
This check looks for function calls that miss required arguments.