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 | public function __construct($poolNumber, array $poolData) |
||
| 94 | |||
| 95 | public function setId($id) |
||
| 100 | |||
| 101 | public function getId() |
||
| 105 | |||
| 106 | public function setName($poolName) |
||
| 111 | |||
| 112 | public function getName() |
||
| 116 | |||
| 117 | public function setHostName($hostName) |
||
| 122 | |||
| 123 | public function getHostName() |
||
| 127 | |||
| 128 | public function setDefaultGateway($defaultGateway) |
||
| 132 | |||
| 133 | public function getDefaultGateway() |
||
| 137 | |||
| 138 | public function setRange(IP $range) |
||
| 142 | |||
| 143 | public function getRange() |
||
| 147 | |||
| 148 | public function setRange6(IP $range6) |
||
| 152 | |||
| 153 | public function getRange6() |
||
| 157 | |||
| 158 | public function setRoutes(array $routes) |
||
| 166 | |||
| 167 | public function getRoutes() |
||
| 171 | |||
| 172 | public function setDns(array $dns) |
||
| 180 | |||
| 181 | public function getDns() |
||
| 185 | |||
| 186 | public function setUseNat($useNat) |
||
| 190 | |||
| 191 | public function getUseNat() |
||
| 195 | |||
| 196 | public function setExtIf($extIf) |
||
| 201 | |||
| 202 | public function getExtIf() |
||
| 206 | |||
| 207 | public function setTwoFactor($twoFactor) |
||
| 211 | |||
| 212 | public function getTwoFactor() |
||
| 216 | |||
| 217 | public function setClientToClient($clientToClient) |
||
| 221 | |||
| 222 | public function getClientToClient() |
||
| 226 | |||
| 227 | public function setManagementIp(IP $managementIp) |
||
| 231 | |||
| 232 | public function getManagementIp() |
||
| 236 | |||
| 237 | public function setListen(IP $listen) |
||
| 241 | |||
| 242 | public function getListen() |
||
| 246 | |||
| 247 | public function getInstances() |
||
| 251 | |||
| 252 | public function setEnableLog($enableLog) |
||
| 256 | |||
| 257 | public function getEnableLog() |
||
| 261 | |||
| 262 | private function populateInstances() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Depending on the prefix we will divide it in a number of nets to |
||
| 294 | * balance the load over the instances, it is recommended to use a least |
||
| 295 | * a /24. |
||
| 296 | * |
||
| 297 | * A /24 or 'bigger' will be split in 4 networks, everything 'smaller' |
||
| 298 | * will be either be split in 2 networks or remain 1 network. |
||
| 299 | */ |
||
| 300 | private static function getNetCount($prefix) |
||
| 320 | |||
| 321 | public function toArray() |
||
| 357 | |||
| 358 | private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false) |
||
| 370 | |||
| 371 | private static function validateString($input) |
||
| 380 | } |
||
| 381 |
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: