Complex classes like Mysqldump 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 Mysqldump, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Mysqldump extends Abstraction implements Executable |
||
23 | { |
||
24 | use OptionMasker; |
||
25 | |||
26 | /** |
||
27 | * Host to connect to |
||
28 | * --host <hostname> |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $host; |
||
33 | |||
34 | /** |
||
35 | * User to connect with |
||
36 | * --user <username> |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $user; |
||
41 | |||
42 | /** |
||
43 | * Password to authenticate with |
||
44 | * --password <password> |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $password; |
||
49 | |||
50 | /** |
||
51 | * List of tables to backup |
||
52 | * --tables array of strings |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private $tablesToDump = []; |
||
57 | |||
58 | /** |
||
59 | * List of databases to backup |
||
60 | * --databases array of strings |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | private $databasesToDump = []; |
||
65 | |||
66 | /** |
||
67 | * List of tables to ignore |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $tablesToIgnore = []; |
||
72 | |||
73 | /** |
||
74 | * List of tables where only the table structure is stored |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | private $structureOnly = []; |
||
79 | |||
80 | /** |
||
81 | * Use mysqldump quick mode |
||
82 | * -q |
||
83 | * |
||
84 | * @var bool |
||
85 | */ |
||
86 | private $quick = false; |
||
87 | |||
88 | /** |
||
89 | * Lock tables option |
||
90 | * --lock-tables |
||
91 | * |
||
92 | * @var bool |
||
93 | */ |
||
94 | private $lockTables; |
||
95 | |||
96 | /** |
||
97 | * Issue a BEGIN SQL statement before dumping data from server |
||
98 | * --single-transaction |
||
99 | * |
||
100 | * @var bool |
||
101 | */ |
||
102 | private $singleTransaction; |
||
103 | |||
104 | /** |
||
105 | * Use mysqldump with compression |
||
106 | * -C |
||
107 | * |
||
108 | * @var bool |
||
109 | */ |
||
110 | private $compress = false; |
||
111 | |||
112 | /** |
||
113 | * Dump only table structures |
||
114 | * --no-data |
||
115 | * |
||
116 | * @var boolean |
||
117 | */ |
||
118 | private $noData = false; |
||
119 | |||
120 | /** |
||
121 | * Whether to add SET @@GLOBAL.GTID_PURGED to output |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | private $gtidPurged; |
||
126 | |||
127 | /** |
||
128 | * Table separated data files |
||
129 | * --tab |
||
130 | * |
||
131 | * @var bool |
||
132 | */ |
||
133 | private $filePerTable; |
||
134 | |||
135 | /** |
||
136 | * Use mysqldump extended insert mode |
||
137 | * -e, --extended-insert |
||
138 | * |
||
139 | * @var boolean |
||
140 | */ |
||
141 | private $extendedInsert = false; |
||
142 | |||
143 | /** |
||
144 | * Dump blob fields as hex. |
||
145 | * --hex-blob |
||
146 | * |
||
147 | * @var boolean |
||
148 | */ |
||
149 | private $hexBlob = false; |
||
150 | |||
151 | /** |
||
152 | * Path to dump file |
||
153 | * |
||
154 | * @var string |
||
155 | */ |
||
156 | private $dumpPathname; |
||
157 | |||
158 | /** |
||
159 | * Compression command to pipe output to |
||
160 | * |
||
161 | * @var \phpbu\App\Backup\Target\Compression |
||
162 | */ |
||
163 | private $compression; |
||
164 | |||
165 | /** |
||
166 | * Constructor. |
||
167 | * |
||
168 | * @param string $path |
||
169 | */ |
||
170 | 30 | public function __construct(string $path = '') |
|
175 | |||
176 | /** |
||
177 | * Set the mysql credentials. |
||
178 | * |
||
179 | * @param string $user |
||
180 | * @param string $password |
||
181 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
182 | */ |
||
183 | 14 | public function credentials(string $user = '', string $password = '') : Mysqldump |
|
189 | |||
190 | /** |
||
191 | * Set the mysql hostname. |
||
192 | * |
||
193 | * @param string $host |
||
194 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
195 | */ |
||
196 | 12 | public function useHost(string $host) : Mysqldump |
|
201 | |||
202 | /** |
||
203 | * Use '-q' quick mode. |
||
204 | * |
||
205 | * @param boolean $bool |
||
206 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
207 | */ |
||
208 | 12 | public function useQuickMode(bool $bool) : Mysqldump |
|
213 | |||
214 | /** |
||
215 | * Use '--lock-tables' option. |
||
216 | * |
||
217 | * @param bool $bool |
||
218 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
219 | */ |
||
220 | 13 | public function lockTables(bool $bool) : Mysqldump |
|
225 | |||
226 | /** |
||
227 | * Use '--single-transaction' option. |
||
228 | * |
||
229 | * @param bool $bool |
||
230 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
231 | */ |
||
232 | 13 | public function singleTransaction(bool $bool) : Mysqldump |
|
237 | |||
238 | /** |
||
239 | * Use '-C' compress mode. |
||
240 | * |
||
241 | * @param bool $bool |
||
242 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
243 | */ |
||
244 | 12 | public function useCompression(bool $bool) : Mysqldump |
|
249 | |||
250 | /** |
||
251 | * Use '-e' extended insert mode. |
||
252 | * |
||
253 | * @param bool $bool |
||
254 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
255 | */ |
||
256 | 13 | public function useExtendedInsert(bool $bool) : Mysqldump |
|
261 | |||
262 | /** |
||
263 | * Use '--hex-blob' to encode binary fields. |
||
264 | * |
||
265 | * @param bool $bool |
||
266 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
267 | */ |
||
268 | 13 | public function dumpBlobsHexadecimal(bool $bool) : Mysqldump |
|
273 | |||
274 | /** |
||
275 | * Set tables to dump. |
||
276 | * |
||
277 | * @param array $tables |
||
278 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
279 | */ |
||
280 | 14 | public function dumpTables(array $tables) : Mysqldump |
|
285 | |||
286 | /** |
||
287 | * Set databases to dump. |
||
288 | * |
||
289 | * @param array $databases |
||
290 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
291 | */ |
||
292 | 15 | public function dumpDatabases(array $databases) : Mysqldump |
|
297 | |||
298 | /** |
||
299 | * Set tables to ignore. |
||
300 | * |
||
301 | * @param array $tables |
||
302 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
303 | */ |
||
304 | 13 | public function ignoreTables(array $tables) : Mysqldump |
|
309 | |||
310 | /** |
||
311 | * Set tables where only table structure should be dumped. |
||
312 | * |
||
313 | * @param array $tables |
||
314 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
315 | */ |
||
316 | 13 | public function dumpStructureOnly(array $tables) : Mysqldump |
|
321 | |||
322 | /** |
||
323 | * Dump no table data at all. |
||
324 | * |
||
325 | * @param bool $bool |
||
326 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
327 | */ |
||
328 | 13 | public function dumpNoData(bool $bool) : Mysqldump |
|
333 | |||
334 | /** |
||
335 | * Add a general transaction ID statement to the dump file. |
||
336 | * |
||
337 | * @param string $purge |
||
338 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
339 | */ |
||
340 | 14 | public function addGTIDStatement(string $purge) |
|
345 | |||
346 | /** |
||
347 | * Produce table separated data files. |
||
348 | * |
||
349 | * @param bool $bool |
||
350 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
351 | */ |
||
352 | 12 | public function produceFilePerTable(bool $bool) : Mysqldump |
|
357 | |||
358 | /** |
||
359 | * Pipe compressor. |
||
360 | * |
||
361 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
362 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
363 | */ |
||
364 | 3 | public function compressOutput(Compression $compression) : Mysqldump |
|
369 | |||
370 | /** |
||
371 | * Set the dump target path. |
||
372 | * |
||
373 | * @param string $path |
||
374 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
375 | */ |
||
376 | 13 | public function dumpTo(string $path) : Mysqldump |
|
381 | |||
382 | /** |
||
383 | * Mysqldump CommandLine generator. |
||
384 | * |
||
385 | * @return \SebastianFeldmann\Cli\CommandLine |
||
386 | */ |
||
387 | 30 | protected function createCommandLine() : CommandLine |
|
430 | |||
431 | /** |
||
432 | * Configure source data (tables, databases). |
||
433 | * |
||
434 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
435 | * @throws \phpbu\App\Exception |
||
436 | */ |
||
437 | 30 | private function configureSourceData(Cmd $cmd) |
|
445 | |||
446 | /** |
||
447 | * Configure source tables. |
||
448 | * |
||
449 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
450 | * @throws \phpbu\App\Exception |
||
451 | */ |
||
452 | 2 | private function configureSourceTables(Cmd $cmd) |
|
460 | |||
461 | /** |
||
462 | * Configure source databases. |
||
463 | * |
||
464 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
465 | */ |
||
466 | 28 | private function configureSourceDatabases(Cmd $cmd) |
|
482 | |||
483 | /** |
||
484 | * Add --ignore-table options |
||
485 | * |
||
486 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
487 | */ |
||
488 | 29 | private function configureIgnoredTables(Cmd $cmd) |
|
496 | |||
497 | /** |
||
498 | * Add compressor pipe if set. |
||
499 | * |
||
500 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
501 | */ |
||
502 | 29 | private function configureCompression(CommandLine $process) |
|
511 | |||
512 | /** |
||
513 | * Configure output redirect. |
||
514 | * |
||
515 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
516 | */ |
||
517 | 29 | private function configureOutput(CommandLine $process) |
|
526 | } |
||
527 |