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() |
||
| 117 | |||
| 118 | private function totalSlavesWeight() |
||
| 126 | |||
| 127 | public function disableCurrentSlave() |
||
| 136 | |||
| 137 | public function slaves() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Prepares a statement for execution and returns a Statement object. |
||
| 144 | * |
||
| 145 | * @param string $prepareString |
||
| 146 | * |
||
| 147 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 148 | */ |
||
| 149 | public function prepare($prepareString) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Executes an SQL statement, returning a result set as a Statement object. |
||
| 157 | * |
||
| 158 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 159 | */ |
||
| 160 | public function query() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Quotes a string for use in a query. |
||
| 170 | * |
||
| 171 | * @param string $input |
||
| 172 | * @param integer $type |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function quote($input, $type = \PDO::PARAM_STR) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Executes an SQL statement and return the number of affected rows. |
||
| 183 | * |
||
| 184 | * @param string $statement |
||
| 185 | * |
||
| 186 | * @return integer |
||
| 187 | */ |
||
| 188 | public function exec($statement) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the ID of the last inserted row or sequence value. |
||
| 196 | * |
||
| 197 | * @param string|null $name |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function lastInsertId($name = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Initiates a transaction. |
||
| 209 | * |
||
| 210 | * @return boolean TRUE on success or FALSE on failure. |
||
| 211 | */ |
||
| 212 | public function beginTransaction() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Commits a transaction. |
||
| 220 | * |
||
| 221 | * @return boolean TRUE on success or FALSE on failure. |
||
| 222 | */ |
||
| 223 | public function commit() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
| 231 | * |
||
| 232 | * @return boolean TRUE on success or FALSE on failure. |
||
| 233 | */ |
||
| 234 | public function rollBack() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the error code associated with the last operation on the database handle. |
||
| 242 | * |
||
| 243 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
| 244 | */ |
||
| 245 | public function errorCode() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns extended error information associated with the last operation on the database handle. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function errorInfo() |
||
| 259 | |||
| 260 | public function close() |
||
| 266 | |||
| 267 | private function hasCache() { |
||
| 270 | |||
| 271 | private function getCacheKey() { |
||
| 274 | |||
| 275 | public function setSlaveStatus(bool $running, ?int $delay) { |
||
| 281 | |||
| 282 | private function getSlaveStatus() { |
||
| 295 | |||
| 296 | public function isSlaveOk($maxdelay = null) { |
||
| 315 | } |
||
| 316 |