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(): void |
||
62 | |||
63 | /** |
||
64 | * @param array $slaves |
||
65 | * @return Slave[] |
||
66 | */ |
||
67 | private function filteredSlaves(array $slaves): array |
||
77 | |||
78 | /** |
||
79 | * @param bool|null $forced |
||
80 | * @return Connection |
||
81 | * @throws \Doctrine\DBAL\DBALException |
||
82 | */ |
||
83 | public function masterConnection(bool $forced = null): Connection |
||
91 | |||
92 | /** |
||
93 | * @return Connection |
||
94 | * @throws \Doctrine\DBAL\DBALException |
||
95 | */ |
||
96 | public function slaveConnection(): ?Connection |
||
118 | |||
119 | /** |
||
120 | * @param Slave[] $slaves |
||
121 | * @return Slave|null |
||
122 | */ |
||
123 | private function randomSlave(array $slaves): ?Slave |
||
137 | |||
138 | /** |
||
139 | * @return ExtendedServer|null |
||
140 | */ |
||
141 | public function getLastConnection(): ?ExtendedServer |
||
145 | |||
146 | /** |
||
147 | * @return Slave[]|null |
||
148 | */ |
||
149 | public function slaves(): ?array |
||
153 | |||
154 | /** |
||
155 | * Prepares a statement for execution and returns a Statement object. |
||
156 | * |
||
157 | * @param string $prepareString |
||
158 | * |
||
159 | * @return \Doctrine\DBAL\Driver\Statement |
||
160 | * @throws \Doctrine\DBAL\DBALException |
||
161 | */ |
||
162 | public function prepare($prepareString) |
||
184 | |||
185 | public function forceMaster(bool $force): void |
||
189 | |||
190 | /** |
||
191 | * Executes an SQL statement, returning a result set as a Statement object. |
||
192 | * |
||
193 | * @return \Doctrine\DBAL\Driver\Statement |
||
194 | * @throws \Doctrine\DBAL\DBALException |
||
195 | */ |
||
196 | public function query() |
||
201 | |||
202 | /** |
||
203 | * Quotes a string for use in a query. |
||
204 | * |
||
205 | * @param string $input |
||
206 | * @param integer $type |
||
207 | * |
||
208 | * @return string |
||
209 | * @throws \Doctrine\DBAL\DBALException |
||
210 | */ |
||
211 | public function quote($input, $type = \PDO::PARAM_STR) |
||
215 | |||
216 | /** |
||
217 | * Executes an SQL statement and return the number of affected rows. |
||
218 | * |
||
219 | * @param string $statement |
||
220 | * |
||
221 | * @return integer |
||
222 | * @throws \Doctrine\DBAL\DBALException |
||
223 | */ |
||
224 | public function exec($statement) |
||
228 | |||
229 | /** |
||
230 | * Returns the ID of the last inserted row or sequence value. |
||
231 | * |
||
232 | * @param string|null $name |
||
233 | * |
||
234 | * @return string |
||
235 | * @throws \Doctrine\DBAL\DBALException |
||
236 | */ |
||
237 | public function lastInsertId($name = null) |
||
241 | |||
242 | /** |
||
243 | * Initiates a transaction. |
||
244 | * |
||
245 | * @return boolean TRUE on success or FALSE on failure. |
||
246 | * @throws \Doctrine\DBAL\DBALException |
||
247 | */ |
||
248 | public function beginTransaction() |
||
252 | |||
253 | /** |
||
254 | * Commits a transaction. |
||
255 | * |
||
256 | * @return boolean TRUE on success or FALSE on failure. |
||
257 | * @throws \Doctrine\DBAL\DBALException |
||
258 | */ |
||
259 | public function commit() |
||
263 | |||
264 | /** |
||
265 | * Rolls back the current transaction, as initiated by beginTransaction(). |
||
266 | * |
||
267 | * @return boolean TRUE on success or FALSE on failure. |
||
268 | * @throws \Doctrine\DBAL\DBALException |
||
269 | */ |
||
270 | public function rollBack() |
||
274 | |||
275 | /** |
||
276 | * Returns the error code associated with the last operation on the database handle. |
||
277 | * |
||
278 | * @return string|null The error code, or null if no operation has been run on the database handle. |
||
279 | * @throws \Doctrine\DBAL\DBALException |
||
280 | */ |
||
281 | public function errorCode() |
||
285 | |||
286 | /** |
||
287 | * Returns extended error information associated with the last operation on the database handle. |
||
288 | * |
||
289 | * @return array |
||
290 | * @throws \Doctrine\DBAL\DBALException |
||
291 | */ |
||
292 | public function errorInfo() |
||
296 | |||
297 | private function hasCache() { |
||
300 | |||
301 | private function getCacheKey(Slave $slave) { |
||
304 | |||
305 | public function setSlaveStatus(Slave $slave, bool $running, int $delay = null): array |
||
312 | |||
313 | private function isSlaveOK(Slave $slave): bool { |
||
324 | } |
||
325 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.