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 |
||
| 22 | class Pool |
||
| 23 | { |
||
| 24 | /** @var int */ |
||
| 25 | private $id; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | private $poolName; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | private $hostName; |
||
| 32 | |||
| 33 | /** @var bool */ |
||
| 34 | private $defaultGateway; |
||
| 35 | |||
| 36 | /** @var IP */ |
||
| 37 | private $range; |
||
| 38 | |||
| 39 | /** @var IP */ |
||
| 40 | private $range6; |
||
| 41 | |||
| 42 | /** @var array */ |
||
| 43 | private $routes; |
||
| 44 | |||
| 45 | /** @var array */ |
||
| 46 | private $dns; |
||
| 47 | |||
| 48 | /** @var bool */ |
||
| 49 | private $useNat; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $extIf; |
||
| 53 | |||
| 54 | /** @var bool */ |
||
| 55 | private $twoFactor; |
||
| 56 | |||
| 57 | /** @var bool */ |
||
| 58 | private $clientToClient; |
||
| 59 | |||
| 60 | /** @var IP */ |
||
| 61 | private $managementIp; |
||
| 62 | |||
| 63 | /** @var IP */ |
||
| 64 | private $listen; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | private $instances; |
||
| 68 | |||
| 69 | /** @var bool */ |
||
| 70 | private $enableLog; |
||
| 71 | |||
| 72 | public function __construct($poolNumber, array $poolData) |
||
| 92 | |||
| 93 | public function setId($id) |
||
| 98 | |||
| 99 | public function getId() |
||
| 103 | |||
| 104 | public function setName($poolName) |
||
| 109 | |||
| 110 | public function getName() |
||
| 114 | |||
| 115 | public function setHostName($hostName) |
||
| 120 | |||
| 121 | public function getHostName() |
||
| 125 | |||
| 126 | public function setDefaultGateway($defaultGateway) |
||
| 130 | |||
| 131 | public function getDefaultGateway() |
||
| 135 | |||
| 136 | public function setRange(IP $range) |
||
| 140 | |||
| 141 | public function getRange() |
||
| 145 | |||
| 146 | public function setRange6(IP $range6) |
||
| 150 | |||
| 151 | public function getRange6() |
||
| 155 | |||
| 156 | public function setRoutes(array $routes) |
||
| 164 | |||
| 165 | public function getRoutes() |
||
| 169 | |||
| 170 | public function setDns(array $dns) |
||
| 178 | |||
| 179 | public function getDns() |
||
| 183 | |||
| 184 | public function setUseNat($useNat) |
||
| 188 | |||
| 189 | public function getUseNat() |
||
| 193 | |||
| 194 | public function setExtIf($extIf) |
||
| 199 | |||
| 200 | public function getExtIf() |
||
| 204 | |||
| 205 | public function setTwoFactor($twoFactor) |
||
| 209 | |||
| 210 | public function getTwoFactor() |
||
| 214 | |||
| 215 | public function setClientToClient($clientToClient) |
||
| 219 | |||
| 220 | public function getClientToClient() |
||
| 224 | |||
| 225 | public function setManagementIp(IP $managementIp) |
||
| 229 | |||
| 230 | public function getManagementIp() |
||
| 234 | |||
| 235 | public function setListen(IP $listen) |
||
| 239 | |||
| 240 | public function getListen() |
||
| 244 | |||
| 245 | public function getInstances() |
||
| 249 | |||
| 250 | public function setEnableLog($enableLog) |
||
| 254 | |||
| 255 | public function getEnableLog() |
||
| 259 | |||
| 260 | private function populateInstances() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Depending on the prefix we will divide it in a number of nets to |
||
| 292 | * balance the load over the instances, it is recommended to use a least |
||
| 293 | * a /24. |
||
| 294 | * |
||
| 295 | * A /24 or 'bigger' will be split in 4 networks, everything 'smaller' |
||
| 296 | * will be either be split in 2 networks or remain 1 network. |
||
| 297 | */ |
||
| 298 | private static function getNetCount($prefix) |
||
| 318 | |||
| 319 | public function toArray() |
||
| 355 | |||
| 356 | private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false) |
||
| 368 | } |
||
| 369 |
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: