Complex classes like Pool 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 Pool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Pool |
||
| 25 | { |
||
| 26 | /** @var string */ |
||
| 27 | private $id; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | private $poolName; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | private $hostName; |
||
| 34 | |||
| 35 | /** @var bool */ |
||
| 36 | private $defaultGateway; |
||
| 37 | |||
| 38 | /** @var IP */ |
||
| 39 | private $range; |
||
| 40 | |||
| 41 | /** @var IP */ |
||
| 42 | private $range6; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | private $routes; |
||
| 46 | |||
| 47 | /** @var array */ |
||
| 48 | private $dns; |
||
| 49 | |||
| 50 | /** @var bool */ |
||
| 51 | private $useNat; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $extIf; |
||
| 55 | |||
| 56 | /** @var bool */ |
||
| 57 | private $twoFactor; |
||
| 58 | |||
| 59 | /** @var bool */ |
||
| 60 | private $clientToClient; |
||
| 61 | |||
| 62 | /** @var IP */ |
||
| 63 | private $managementIp; |
||
| 64 | |||
| 65 | /** @var IP */ |
||
| 66 | private $listen; |
||
| 67 | |||
| 68 | /** @var array */ |
||
| 69 | private $instances; |
||
| 70 | |||
| 71 | /** @var bool */ |
||
| 72 | private $enableLog; |
||
| 73 | |||
| 74 | /** @var bool */ |
||
| 75 | private $enableAcl; |
||
| 76 | |||
| 77 | /** @var array */ |
||
| 78 | private $aclGroupList; |
||
| 79 | |||
| 80 | /** @var bool */ |
||
| 81 | private $forward6; |
||
| 82 | |||
| 83 | /** @var bool */ |
||
| 84 | private $blockSmb; |
||
| 85 | |||
| 86 | public function __construct($poolNumber, array $poolData) |
||
| 87 | { |
||
| 88 | $this->setId(self::validate($poolData, 'id')); |
||
| 89 | $this->setName(self::validate($poolData, 'name', false, $this->getId())); |
||
|
|
|||
| 90 | $this->setHostName(self::validate($poolData, 'hostName')); |
||
| 91 | $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false)); |
||
| 92 | $this->setRange(new IP(self::validate($poolData, 'range'))); |
||
| 93 | $this->setRange6(new IP(self::validate($poolData, 'range6'))); |
||
| 94 | $this->setRoutes(self::validate($poolData, 'routes', false, [])); |
||
| 95 | $this->setDns(self::validate($poolData, 'dns', false, [])); |
||
| 96 | $this->setUseNat(self::validate($poolData, 'useNat', false, false)); |
||
| 97 | $this->setExtIf(self::validate($poolData, 'extIf')); |
||
| 98 | $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false)); |
||
| 99 | $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false)); |
||
| 100 | $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber))); |
||
| 101 | $this->setListen(new IP(self::validate($poolData, 'listen', false, '::'))); |
||
| 102 | $this->setEnableLog(self::validate($poolData, 'enableLog', false, false)); |
||
| 103 | $this->setEnableAcl(self::validate($poolData, 'enableAcl', false, false)); |
||
| 104 | $this->setAclGroupList(self::validate($poolData, 'aclGroupList', false, [])); |
||
| 105 | $this->setBlockSmb(self::validate($poolData, 'blockSmb', false, false)); |
||
| 106 | $this->setForward6(self::validate($poolData, 'forward6', false, true)); |
||
| 107 | $this->populateInstances(); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function setId($id) |
||
| 111 | { |
||
| 112 | self::validateSimpleString($id); |
||
| 113 | $this->id = $id; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function getId() |
||
| 117 | { |
||
| 118 | return $this->id; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setName($poolName) |
||
| 122 | { |
||
| 123 | self::validateString($poolName); |
||
| 124 | $this->poolName = $poolName; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getName() |
||
| 128 | { |
||
| 129 | return $this->poolName; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function setHostName($hostName) |
||
| 133 | { |
||
| 134 | self::validateString($hostName); |
||
| 135 | $this->hostName = $hostName; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getHostName() |
||
| 142 | |||
| 143 | public function setDefaultGateway($defaultGateway) |
||
| 144 | { |
||
| 145 | $this->defaultGateway = (bool) $defaultGateway; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getDefaultGateway() |
||
| 149 | { |
||
| 150 | return $this->defaultGateway; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function setRange(IP $range) |
||
| 157 | |||
| 158 | public function getRange() |
||
| 162 | |||
| 163 | public function setRange6(IP $range6) |
||
| 167 | |||
| 168 | public function getRange6() |
||
| 172 | |||
| 173 | public function setRoutes(array $routes) |
||
| 181 | |||
| 182 | public function getRoutes() |
||
| 186 | |||
| 187 | public function setDns(array $dns) |
||
| 195 | |||
| 196 | public function getDns() |
||
| 200 | |||
| 201 | public function setUseNat($useNat) |
||
| 205 | |||
| 206 | public function getUseNat() |
||
| 210 | |||
| 211 | public function setExtIf($extIf) |
||
| 212 | { |
||
| 213 | self::validateString($extIf); |
||
| 214 | $this->extIf = $extIf; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getExtIf() |
||
| 218 | { |
||
| 219 | return $this->extIf; |
||
| 221 | |||
| 222 | public function setTwoFactor($twoFactor) |
||
| 226 | |||
| 227 | public function getTwoFactor() |
||
| 231 | |||
| 232 | public function setClientToClient($clientToClient) |
||
| 236 | |||
| 237 | public function getClientToClient() |
||
| 241 | |||
| 242 | public function setManagementIp(IP $managementIp) |
||
| 246 | |||
| 247 | public function getManagementIp() |
||
| 251 | |||
| 252 | public function setListen(IP $listen) |
||
| 256 | |||
| 257 | public function getListen() |
||
| 261 | |||
| 262 | public function getInstances() |
||
| 266 | |||
| 267 | public function setEnableLog($enableLog) |
||
| 271 | |||
| 272 | public function getEnableLog() |
||
| 276 | |||
| 277 | public function setEnableAcl($enableAcl) |
||
| 281 | |||
| 282 | public function getEnableAcl() |
||
| 286 | |||
| 287 | public function setAclGroupList(array $aclGroupList) |
||
| 295 | |||
| 296 | public function getAclGroupList() |
||
| 300 | |||
| 301 | public function setBlockSmb($blockSmb) |
||
| 305 | |||
| 306 | public function getBlockSmb() |
||
| 310 | |||
| 311 | public function setForward6($forward6) |
||
| 315 | |||
| 316 | public function getForward6() |
||
| 320 | |||
| 321 | private function populateInstances() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Depending on the prefix we will divide it in a number of nets to |
||
| 353 | * balance the load over the instances, it is recommended to use a least |
||
| 354 | * a /24. |
||
| 355 | * |
||
| 356 | * A /24 or 'bigger' will be split in 4 networks, everything 'smaller' |
||
| 357 | * will be either be split in 2 networks or remain 1 network. |
||
| 358 | */ |
||
| 359 | private static function getNetCount($prefix) |
||
| 379 | |||
| 380 | public function toArray() |
||
| 418 | |||
| 419 | private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false) |
||
| 431 | |||
| 432 | private static function validateString($input) |
||
| 441 | |||
| 442 | private static function validateSimpleString($input) |
||
| 452 | } |
||
| 453 |
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: