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 | public function __construct($poolNumber, array $poolData) |
||
| 81 | { |
||
| 82 | $this->setId(self::validate($poolData, 'id')); |
||
| 83 | $this->setName(self::validate($poolData, 'name', false, $this->getId())); |
||
|
|
|||
| 84 | $this->setHostName(self::validate($poolData, 'hostName')); |
||
| 85 | $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false)); |
||
| 86 | $this->setRange(new IP(self::validate($poolData, 'range'))); |
||
| 87 | $this->setRange6(new IP(self::validate($poolData, 'range6'))); |
||
| 88 | $this->setRoutes(self::validate($poolData, 'routes', false, [])); |
||
| 89 | $this->setDns(self::validate($poolData, 'dns', false, [])); |
||
| 90 | $this->setUseNat(self::validate($poolData, 'useNat', false, false)); |
||
| 91 | $this->setExtIf(self::validate($poolData, 'extIf')); |
||
| 92 | $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false)); |
||
| 93 | $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false)); |
||
| 94 | $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber))); |
||
| 95 | $this->setListen(new IP(self::validate($poolData, 'listen', false, '::'))); |
||
| 96 | $this->setEnableLog(self::validate($poolData, 'enableLog', false, false)); |
||
| 97 | $this->setEnableAcl(self::validate($poolData, 'enableAcl', false, false)); |
||
| 98 | $this->setAclGroupList(self::validate($poolData, 'aclGroupList', false, [])); |
||
| 99 | $this->populateInstances(); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function setId($id) |
||
| 103 | { |
||
| 104 | self::validateSimpleString($id); |
||
| 105 | $this->id = $id; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getId() |
||
| 112 | |||
| 113 | public function setName($poolName) |
||
| 118 | |||
| 119 | public function getName() |
||
| 123 | |||
| 124 | public function setHostName($hostName) |
||
| 129 | |||
| 130 | public function getHostName() |
||
| 134 | |||
| 135 | public function setDefaultGateway($defaultGateway) |
||
| 139 | |||
| 140 | public function getDefaultGateway() |
||
| 144 | |||
| 145 | public function setRange(IP $range) |
||
| 149 | |||
| 150 | public function getRange() |
||
| 154 | |||
| 155 | public function setRange6(IP $range6) |
||
| 159 | |||
| 160 | public function getRange6() |
||
| 164 | |||
| 165 | public function setRoutes(array $routes) |
||
| 173 | |||
| 174 | public function getRoutes() |
||
| 178 | |||
| 179 | public function setDns(array $dns) |
||
| 187 | |||
| 188 | public function getDns() |
||
| 192 | |||
| 193 | public function setUseNat($useNat) |
||
| 197 | |||
| 198 | public function getUseNat() |
||
| 202 | |||
| 203 | public function setExtIf($extIf) |
||
| 208 | |||
| 209 | public function getExtIf() |
||
| 213 | |||
| 214 | public function setTwoFactor($twoFactor) |
||
| 218 | |||
| 219 | public function getTwoFactor() |
||
| 223 | |||
| 224 | public function setClientToClient($clientToClient) |
||
| 228 | |||
| 229 | public function getClientToClient() |
||
| 233 | |||
| 234 | public function setManagementIp(IP $managementIp) |
||
| 238 | |||
| 239 | public function getManagementIp() |
||
| 243 | |||
| 244 | public function setListen(IP $listen) |
||
| 248 | |||
| 249 | public function getListen() |
||
| 253 | |||
| 254 | public function getInstances() |
||
| 258 | |||
| 259 | public function setEnableLog($enableLog) |
||
| 263 | |||
| 264 | public function getEnableLog() |
||
| 268 | |||
| 269 | public function setEnableAcl($enableAcl) |
||
| 273 | |||
| 274 | public function getEnableAcl() |
||
| 278 | |||
| 279 | public function setAclGroupList(array $aclGroupList) |
||
| 280 | { |
||
| 287 | |||
| 288 | public function getAclGroupList() |
||
| 292 | |||
| 293 | private function populateInstances() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Depending on the prefix we will divide it in a number of nets to |
||
| 325 | * balance the load over the instances, it is recommended to use a least |
||
| 326 | * a /24. |
||
| 327 | * |
||
| 328 | * A /24 or 'bigger' will be split in 4 networks, everything 'smaller' |
||
| 329 | * will be either be split in 2 networks or remain 1 network. |
||
| 330 | */ |
||
| 331 | private static function getNetCount($prefix) |
||
| 351 | |||
| 352 | public function toArray() |
||
| 390 | |||
| 391 | private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false) |
||
| 403 | |||
| 404 | private static function validateString($input) |
||
| 413 | |||
| 414 | private static function validateSimpleString($input) |
||
| 424 | } |
||
| 425 |
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: