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 |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * configuration |
||
| 14 | * |
||
| 15 | * @var Master |
||
| 16 | */ |
||
| 17 | private $master; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * configuration |
||
| 21 | * |
||
| 22 | * @var Slave[] |
||
| 23 | */ |
||
| 24 | private $slaves; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \Ez\DbLinker\Cache |
||
| 28 | */ |
||
| 29 | private $cache; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $forceMaster = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $maxSlaveDelay = 30; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $slaveStatusCacheTtl = 10; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ExtendedServer |
||
| 48 | */ |
||
| 49 | private $lastConnection; |
||
| 50 | |||
| 51 | public function __construct(array $master, array $slaves, $cache = null) |
||
| 57 | |||
| 58 | public function disableCache() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $slaves |
||
| 64 | * @return Slave[] |
||
| 65 | */ |
||
| 66 | private function filteredSlaves(array $slaves) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param bool|null $forced |
||
| 79 | * @return Connection |
||
| 80 | * @throws \Doctrine\DBAL\DBALException |
||
| 81 | */ |
||
| 82 | private function masterConnection(bool $forced = null): Connection |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return Connection |
||
| 93 | * @throws \Doctrine\DBAL\DBALException |
||
| 94 | */ |
||
| 95 | private function slaveConnection(): ?Connection |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param Slave[] $slaves |
||
| 120 | * @return Slave |
||
| 121 | */ |
||
| 122 | private function randomSlave(array $slaves): Slave |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return ExtendedServer|null |
||
| 136 | */ |
||
| 137 | public function getLastConnection(): ?ExtendedServer |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return Slave[]|null |
||
| 144 | */ |
||
| 145 | public function slaves(): ?array |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Prepares a statement for execution and returns a Statement object. |
||
| 152 | * |
||
| 153 | * @param string $prepareString |
||
| 154 | * |
||
| 155 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 156 | */ |
||
| 157 | public function prepare($prepareString) |
||
| 179 | |||
| 180 | public function forceMaster(bool $force): void |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Executes an SQL statement, returning a result set as a Statement object. |
||
| 187 | * |
||
| 188 | * @return \Doctrine\DBAL\Driver\Statement |
||
| 189 | */ |
||
| 190 | public function query() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Quotes a string for use in a query. |
||
| 198 | * |
||
| 199 | * @param string $input |
||
| 200 | * @param integer $type |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function quote($input, $type = \PDO::PARAM_STR) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Executes an SQL statement and return the number of affected rows. |
||
| 211 | * |
||
| 212 | * @param string $statement |
||
| 213 | * |
||
| 214 | * @return integer |
||
| 215 | */ |
||
| 216 | public function exec($statement) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the ID of the last inserted row or sequence value. |
||
| 223 | * |
||
| 224 | * @param string|null $name |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function lastInsertId($name = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Initiates a transaction. |
||
| 235 | * |
||
| 236 | * @return boolean TRUE on success or FALSE on failure. |
||
| 237 | */ |
||
| 238 | public function beginTransaction() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Commits a transaction. |
||
| 245 | * |
||
| 246 | * @return boolean TRUE on success or FALSE on failure. |
||
| 247 | */ |
||
| 248 | public function commit() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
| 255 | * |
||
| 256 | * @return boolean TRUE on success or FALSE on failure. |
||
| 257 | */ |
||
| 258 | public function rollBack() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the error code associated with the last operation on the database handle. |
||
| 265 | * |
||
| 266 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
| 267 | */ |
||
| 268 | public function errorCode() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns extended error information associated with the last operation on the database handle. |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function errorInfo() |
||
| 282 | |||
| 283 | private function hasCache() { |
||
| 286 | |||
| 287 | private function getCacheKey(Slave $slave) { |
||
| 290 | |||
| 291 | public function setSlaveStatus(Slave $slave, bool $running, int $delay = null) { |
||
| 297 | |||
| 298 | private function isSlaveOK(Slave $slave): bool { |
||
| 309 | } |
||
| 310 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.