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 | public function __construct($poolNumber, array $poolData) |
||
| 78 | { |
||
| 79 | $this->setId(self::validate($poolData, 'id')); |
||
| 80 | $this->setName(self::validate($poolData, 'name', false, $this->getId())); |
||
|
|
|||
| 81 | $this->setHostName(self::validate($poolData, 'hostName')); |
||
| 82 | $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false)); |
||
| 83 | $this->setRange(new IP(self::validate($poolData, 'range'))); |
||
| 84 | $this->setRange6(new IP(self::validate($poolData, 'range6'))); |
||
| 85 | $this->setRoutes(self::validate($poolData, 'routes', false, [])); |
||
| 86 | $this->setDns(self::validate($poolData, 'dns', false, [])); |
||
| 87 | $this->setUseNat(self::validate($poolData, 'useNat', false, false)); |
||
| 88 | $this->setExtIf(self::validate($poolData, 'extIf')); |
||
| 89 | $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false)); |
||
| 90 | $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false)); |
||
| 91 | $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber))); |
||
| 92 | $this->setListen(new IP(self::validate($poolData, 'listen', false, '::'))); |
||
| 93 | $this->setEnableLog(self::validate($poolData, 'enableLog', false, false)); |
||
| 94 | $this->setEnableAcl(self::validate($poolData, 'enableAcl', false, false)); |
||
| 95 | $this->populateInstances(); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setId($id) |
||
| 99 | { |
||
| 100 | self::validateSimpleString($id); |
||
| 101 | $this->id = $id; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getId() |
||
| 108 | |||
| 109 | public function setName($poolName) |
||
| 114 | |||
| 115 | public function getName() |
||
| 119 | |||
| 120 | public function setHostName($hostName) |
||
| 125 | |||
| 126 | public function getHostName() |
||
| 130 | |||
| 131 | public function setDefaultGateway($defaultGateway) |
||
| 135 | |||
| 136 | public function getDefaultGateway() |
||
| 140 | |||
| 141 | public function setRange(IP $range) |
||
| 145 | |||
| 146 | public function getRange() |
||
| 150 | |||
| 151 | public function setRange6(IP $range6) |
||
| 155 | |||
| 156 | public function getRange6() |
||
| 160 | |||
| 161 | public function setRoutes(array $routes) |
||
| 169 | |||
| 170 | public function getRoutes() |
||
| 174 | |||
| 175 | public function setDns(array $dns) |
||
| 183 | |||
| 184 | public function getDns() |
||
| 188 | |||
| 189 | public function setUseNat($useNat) |
||
| 193 | |||
| 194 | public function getUseNat() |
||
| 198 | |||
| 199 | public function setExtIf($extIf) |
||
| 204 | |||
| 205 | public function getExtIf() |
||
| 209 | |||
| 210 | public function setTwoFactor($twoFactor) |
||
| 214 | |||
| 215 | public function getTwoFactor() |
||
| 219 | |||
| 220 | public function setClientToClient($clientToClient) |
||
| 224 | |||
| 225 | public function getClientToClient() |
||
| 229 | |||
| 230 | public function setManagementIp(IP $managementIp) |
||
| 234 | |||
| 235 | public function getManagementIp() |
||
| 239 | |||
| 240 | public function setListen(IP $listen) |
||
| 244 | |||
| 245 | public function getListen() |
||
| 249 | |||
| 250 | public function getInstances() |
||
| 254 | |||
| 255 | public function setEnableLog($enableLog) |
||
| 259 | |||
| 260 | public function getEnableLog() |
||
| 264 | |||
| 265 | public function setEnableAcl($enableAcl) |
||
| 269 | |||
| 270 | public function getEnableAcl() |
||
| 274 | |||
| 275 | private function populateInstances() |
||
| 276 | { |
||
| 277 | $instanceCount = self::getNetCount($this->getRange()->getPrefix()); |
||
| 278 | $splitRange = $this->getRange()->split($instanceCount); |
||
| 279 | $splitRange6 = $this->getRange6()->split($instanceCount); |
||
| 280 | |||
| 281 | for ($i = 0; $i < $instanceCount; ++$i) { |
||
| 282 | // protocol is udp unless it is the last instance when there is |
||
| 283 | // not just one instance |
||
| 284 | if (1 === $instanceCount || $i !== $instanceCount - 1) { |
||
| 285 | $proto = 'udp'; |
||
| 286 | $port = 1194 + $i; |
||
| 287 | } else { |
||
| 288 | $proto = 'tcp'; |
||
| 289 | $port = 1194; |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->instances[] = new Instance( |
||
| 293 | [ |
||
| 294 | 'range' => $splitRange[$i], |
||
| 295 | 'range6' => $splitRange6[$i], |
||
| 296 | 'dev' => sprintf('tun-%s-%d', $this->getId(), $i), |
||
| 297 | 'proto' => $proto, |
||
| 298 | 'port' => $port, |
||
| 299 | 'managementPort' => 11940 + $i, |
||
| 300 | ] |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Depending on the prefix we will divide it in a number of nets to |
||
| 307 | * balance the load over the instances, it is recommended to use a least |
||
| 308 | * a /24. |
||
| 309 | * |
||
| 310 | * A /24 or 'bigger' will be split in 4 networks, everything 'smaller' |
||
| 311 | * will be either be split in 2 networks or remain 1 network. |
||
| 312 | */ |
||
| 313 | private static function getNetCount($prefix) |
||
| 333 | |||
| 334 | public function toArray() |
||
| 371 | |||
| 372 | private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false) |
||
| 384 | |||
| 385 | private static function validateString($input) |
||
| 394 | |||
| 395 | private static function validateSimpleString($input) |
||
| 405 | } |
||
| 406 |
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: