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() |
||
| 77 | |||
| 78 | public function isConnectedToMaster() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inherit |
||
| 85 | */ |
||
| 86 | public function getCurrentConnection() |
||
| 90 | |||
| 91 | protected function wrap() |
||
| 104 | |||
| 105 | private function chooseASlave() |
||
| 122 | |||
| 123 | private function totalSlavesWeight() |
||
| 131 | |||
| 132 | public function disableCurrentSlave() |
||
| 141 | |||
| 142 | public function slaves() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Prepares a statement for execution and returns a Statement object. |
||
| 149 | * |
||
| 150 | * @param string $prepareString |
||
| 151 | * |
||
| 152 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 153 | */ |
||
| 154 | public function prepare($prepareString) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Executes an SQL statement, returning a result set as a Statement object. |
||
| 162 | * |
||
| 163 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 164 | */ |
||
| 165 | public function query() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Quotes a string for use in a query. |
||
| 175 | * |
||
| 176 | * @param string $input |
||
| 177 | * @param integer $type |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function quote($input, $type = \PDO::PARAM_STR) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Executes an SQL statement and return the number of affected rows. |
||
| 188 | * |
||
| 189 | * @param string $statement |
||
| 190 | * |
||
| 191 | * @return integer |
||
| 192 | */ |
||
| 193 | public function exec($statement) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the ID of the last inserted row or sequence value. |
||
| 201 | * |
||
| 202 | * @param string|null $name |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function lastInsertId($name = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Initiates a transaction. |
||
| 214 | * |
||
| 215 | * @return boolean TRUE on success or FALSE on failure. |
||
| 216 | */ |
||
| 217 | public function beginTransaction() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Commits a transaction. |
||
| 225 | * |
||
| 226 | * @return boolean TRUE on success or FALSE on failure. |
||
| 227 | */ |
||
| 228 | public function commit() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
| 236 | * |
||
| 237 | * @return boolean TRUE on success or FALSE on failure. |
||
| 238 | */ |
||
| 239 | public function rollBack() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the error code associated with the last operation on the database handle. |
||
| 247 | * |
||
| 248 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
| 249 | */ |
||
| 250 | public function errorCode() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns extended error information associated with the last operation on the database handle. |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function errorInfo() |
||
| 264 | |||
| 265 | public function close() |
||
| 271 | |||
| 272 | private function hasCache() { |
||
| 275 | |||
| 276 | private function getCacheKey() { |
||
| 279 | |||
| 280 | public function setSlaveStatus(bool $running, ?int $delay) { |
||
| 286 | |||
| 287 | private function getSlaveStatus() { |
||
| 312 | |||
| 313 | public function isSlaveOk($maxdelay = null) { |
||
| 332 | } |
||
| 333 |