Complex classes like MasterSlavesConnection 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 MasterSlavesConnection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class MasterSlavesConnection implements Connection, ConnectionWrapper |
||
| 11 | { |
||
| 12 | use ConnectionWrapperTrait; |
||
| 13 | |||
| 14 | private $master; |
||
| 15 | private $slaves; |
||
| 16 | private $currentConnectionParams; |
||
| 17 | private $currentSlave; |
||
| 18 | private $cache; |
||
| 19 | private $forceMaster; |
||
| 20 | private $maxSlaveDelay = 30; |
||
| 21 | private $slaveStatusCacheTtl = 10; |
||
| 22 | |||
| 23 | public function __construct(array $master, array $slaves, $cache = null) |
||
| 31 | |||
| 32 | public function disableCache() { |
||
| 35 | |||
| 36 | private function checkSlaves(array $slaves) |
||
| 44 | |||
| 45 | public function connectToMaster($forced = null) |
||
| 57 | |||
| 58 | public function connectToSlave() |
||
| 72 | |||
| 73 | public function isConnectedToMaster() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @inherit |
||
| 80 | */ |
||
| 81 | public function getCurrentConnection() |
||
| 85 | |||
| 86 | protected function wrap() |
||
| 99 | |||
| 100 | private function chooseASlave() |
||
| 114 | |||
| 115 | private function totalSlavesWeight() |
||
| 123 | |||
| 124 | public function disableCurrentSlave() |
||
| 133 | |||
| 134 | public function slaves() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Prepares a statement for execution and returns a Statement object. |
||
| 141 | * |
||
| 142 | * @param string $prepareString |
||
| 143 | * |
||
| 144 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 145 | */ |
||
| 146 | public function prepare($prepareString) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Executes an SQL statement, returning a result set as a Statement object. |
||
| 154 | * |
||
| 155 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 156 | */ |
||
| 157 | public function query() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Quotes a string for use in a query. |
||
| 167 | * |
||
| 168 | * @param string $input |
||
| 169 | * @param integer $type |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function quote($input, $type = \PDO::PARAM_STR) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Executes an SQL statement and return the number of affected rows. |
||
| 180 | * |
||
| 181 | * @param string $statement |
||
| 182 | * |
||
| 183 | * @return integer |
||
| 184 | */ |
||
| 185 | public function exec($statement) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the ID of the last inserted row or sequence value. |
||
| 193 | * |
||
| 194 | * @param string|null $name |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function lastInsertId($name = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Initiates a transaction. |
||
| 206 | * |
||
| 207 | * @return boolean TRUE on success or FALSE on failure. |
||
| 208 | */ |
||
| 209 | public function beginTransaction() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Commits a transaction. |
||
| 217 | * |
||
| 218 | * @return boolean TRUE on success or FALSE on failure. |
||
| 219 | */ |
||
| 220 | public function commit() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
| 228 | * |
||
| 229 | * @return boolean TRUE on success or FALSE on failure. |
||
| 230 | */ |
||
| 231 | public function rollBack() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns the error code associated with the last operation on the database handle. |
||
| 239 | * |
||
| 240 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
| 241 | */ |
||
| 242 | public function errorCode() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns extended error information associated with the last operation on the database handle. |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function errorInfo() |
||
| 256 | |||
| 257 | public function close() |
||
| 263 | |||
| 264 | private function hasCache() { |
||
| 267 | |||
| 268 | private function getCacheKey() { |
||
| 271 | |||
| 272 | public function setSlaveStatus(bool $running, int $delay) { |
||
| 278 | |||
| 279 | private function getSlaveStatus() { |
||
| 292 | |||
| 293 | public function isSlaveOk($maxdelay = null) { |
||
| 312 | } |
||
| 313 |