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 | * Port to connect to |
||
36 | * --port <number> |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $port; |
||
41 | |||
42 | /** |
||
43 | * The connection protocol |
||
44 | * --protocol |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $protocol; |
||
49 | |||
50 | /** |
||
51 | * User to connect with |
||
52 | * --user <username> |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $user; |
||
57 | |||
58 | /** |
||
59 | * Password to authenticate with |
||
60 | * --password <password> |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $password; |
||
65 | |||
66 | /** |
||
67 | * List of tables to backup |
||
68 | * --tables array of strings |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $tablesToDump = []; |
||
73 | |||
74 | /** |
||
75 | * List of databases to backup |
||
76 | * --databases array of strings |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | private $databasesToDump = []; |
||
81 | |||
82 | /** |
||
83 | * List of tables to ignore |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $tablesToIgnore = []; |
||
88 | |||
89 | /** |
||
90 | * List of tables where only the table structure is stored |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | private $structureOnly = []; |
||
95 | |||
96 | /** |
||
97 | * Use mysqldump quick mode |
||
98 | * -q |
||
99 | * |
||
100 | * @var bool |
||
101 | */ |
||
102 | private $quick = false; |
||
103 | |||
104 | /** |
||
105 | * Lock tables option |
||
106 | * --lock-tables |
||
107 | * |
||
108 | * @var bool |
||
109 | */ |
||
110 | private $lockTables; |
||
111 | |||
112 | /** |
||
113 | * Issue a BEGIN SQL statement before dumping data from server |
||
114 | * --single-transaction |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $singleTransaction; |
||
119 | |||
120 | /** |
||
121 | * Use mysqldump with compression |
||
122 | * -C |
||
123 | * |
||
124 | * @var bool |
||
125 | */ |
||
126 | private $compress = false; |
||
127 | |||
128 | /** |
||
129 | * Dump only table structures |
||
130 | * --no-data |
||
131 | * |
||
132 | * @var bool |
||
133 | */ |
||
134 | private $noData = false; |
||
135 | |||
136 | /** |
||
137 | * Whether to add SET @@GLOBAL.GTID_PURGED to output |
||
138 | * |
||
139 | * @var string |
||
140 | */ |
||
141 | private $gtidPurged; |
||
142 | |||
143 | /** |
||
144 | * Table separated data files |
||
145 | * --tab |
||
146 | * |
||
147 | * @var bool |
||
148 | */ |
||
149 | private $filePerTable; |
||
150 | |||
151 | /** |
||
152 | * Skip mysqldump extended insert mode |
||
153 | * --skip-extended-insert |
||
154 | * |
||
155 | * @var bool |
||
156 | */ |
||
157 | private $skipExtendedInsert = false; |
||
158 | |||
159 | /** |
||
160 | * Dump blob fields as hex. |
||
161 | * --hex-blob |
||
162 | * |
||
163 | * @var bool |
||
164 | */ |
||
165 | private $hexBlob = false; |
||
166 | |||
167 | /** |
||
168 | * Dump routines. |
||
169 | * --routines |
||
170 | * |
||
171 | * @var bool |
||
172 | */ |
||
173 | private $routines = false; |
||
174 | |||
175 | /** |
||
176 | * Skip triggers |
||
177 | * --skip-triggers |
||
178 | * |
||
179 | * @var boolean |
||
180 | */ |
||
181 | private $skipTriggers = false; |
||
182 | |||
183 | /** |
||
184 | * Path to dump file |
||
185 | * |
||
186 | * @var string |
||
187 | */ |
||
188 | private $dumpPathname; |
||
189 | |||
190 | /** |
||
191 | * Compression command to pipe output to |
||
192 | * |
||
193 | * @var \phpbu\App\Backup\Target\Compression |
||
194 | */ |
||
195 | private $compression; |
||
196 | |||
197 | /** |
||
198 | * Constructor |
||
199 | * |
||
200 | * @param string $path |
||
201 | */ |
||
202 | 37 | public function __construct(string $path = '') |
|
207 | |||
208 | /** |
||
209 | * Set the mysql credentials |
||
210 | * |
||
211 | * @param string $user |
||
212 | * @param string $password |
||
213 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
214 | */ |
||
215 | 17 | public function credentials(string $user = '', string $password = '') : Mysqldump |
|
221 | |||
222 | /** |
||
223 | * Set the mysql hostname |
||
224 | * |
||
225 | * @param string $host |
||
226 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
227 | */ |
||
228 | 16 | public function useHost(string $host) : Mysqldump |
|
233 | |||
234 | /** |
||
235 | * Set the mysql port |
||
236 | * |
||
237 | * @param int $port |
||
238 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
239 | */ |
||
240 | 16 | public function usePort(int $port) : Mysqldump |
|
245 | |||
246 | /** |
||
247 | * Set the connection protocol. |
||
248 | * |
||
249 | * @param string $protocol |
||
250 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
251 | */ |
||
252 | 16 | public function useProtocol(string $protocol) : Mysqldump |
|
257 | |||
258 | /** |
||
259 | * Use '-q' quick mode |
||
260 | * |
||
261 | * @param boolean $bool |
||
262 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
263 | */ |
||
264 | 15 | public function useQuickMode(bool $bool) : Mysqldump |
|
269 | |||
270 | /** |
||
271 | * Use '--lock-tables' option |
||
272 | * |
||
273 | * @param bool $bool |
||
274 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
275 | */ |
||
276 | 16 | public function lockTables(bool $bool) : Mysqldump |
|
281 | |||
282 | /** |
||
283 | * Use '--single-transaction' option |
||
284 | * |
||
285 | * @param bool $bool |
||
286 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
287 | */ |
||
288 | 16 | public function singleTransaction(bool $bool) : Mysqldump |
|
293 | |||
294 | /** |
||
295 | * Use '-C' compress mode |
||
296 | * |
||
297 | * @param bool $bool |
||
298 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
299 | */ |
||
300 | 15 | public function useCompression(bool $bool) : Mysqldump |
|
305 | |||
306 | /** |
||
307 | * Use '--skip-extended-insert' option |
||
308 | * |
||
309 | * @param bool $bool |
||
310 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
311 | */ |
||
312 | 16 | public function skipExtendedInsert(bool $bool) : Mysqldump |
|
313 | { |
||
314 | 16 | $this->skipExtendedInsert = $bool; |
|
315 | 16 | return $this; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Use '--hex-blob' to encode binary fields |
||
320 | * |
||
321 | * @param bool $bool |
||
322 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
323 | */ |
||
324 | 16 | public function dumpBlobsHexadecimal(bool $bool) : Mysqldump |
|
329 | |||
330 | /** |
||
331 | * Set tables to dump |
||
332 | * |
||
333 | * @param array $tables |
||
334 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
335 | */ |
||
336 | 17 | public function dumpTables(array $tables) : Mysqldump |
|
341 | |||
342 | /** |
||
343 | * Set databases to dump |
||
344 | * |
||
345 | * @param array $databases |
||
346 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
347 | */ |
||
348 | 18 | public function dumpDatabases(array $databases) : Mysqldump |
|
353 | |||
354 | /** |
||
355 | * Set tables to ignore |
||
356 | * |
||
357 | * @param array $tables |
||
358 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
359 | */ |
||
360 | 16 | public function ignoreTables(array $tables) : Mysqldump |
|
365 | |||
366 | /** |
||
367 | * Set tables where only table structure should be dumped |
||
368 | * |
||
369 | * @param array $tables |
||
370 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
371 | */ |
||
372 | 16 | public function dumpStructureOnly(array $tables) : Mysqldump |
|
377 | |||
378 | /** |
||
379 | * Dump no table data at all |
||
380 | * |
||
381 | * @param bool $bool |
||
382 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
383 | */ |
||
384 | 16 | public function dumpNoData(bool $bool) : Mysqldump |
|
389 | |||
390 | /** |
||
391 | * Add a general transaction ID statement to the dump file |
||
392 | * |
||
393 | * @param string $purge |
||
394 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
395 | */ |
||
396 | 17 | public function addGTIDStatement(string $purge) |
|
401 | |||
402 | /** |
||
403 | * Produce table separated data files |
||
404 | * |
||
405 | * @param bool $bool |
||
406 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
407 | */ |
||
408 | 15 | public function produceFilePerTable(bool $bool) : Mysqldump |
|
413 | |||
414 | /** |
||
415 | * Dump procedures and functions |
||
416 | * |
||
417 | * @param bool $bool |
||
418 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
419 | */ |
||
420 | 15 | public function dumpRoutines(bool $bool) : Mysqldump |
|
425 | |||
426 | /** |
||
427 | * Skip triggers |
||
428 | * |
||
429 | * @param bool $bool |
||
430 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
431 | */ |
||
432 | 16 | public function skipTriggers(bool $bool) : Mysqldump |
|
437 | |||
438 | /** |
||
439 | * Pipe compressor |
||
440 | * |
||
441 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
442 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
443 | */ |
||
444 | 3 | public function compressOutput(Compression $compression) : Mysqldump |
|
449 | |||
450 | /** |
||
451 | * Set the dump target path |
||
452 | * |
||
453 | * @param string $path |
||
454 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
455 | */ |
||
456 | 16 | public function dumpTo(string $path) : Mysqldump |
|
461 | |||
462 | /** |
||
463 | * Mysqldump CommandLine generator |
||
464 | * |
||
465 | * @return \SebastianFeldmann\Cli\CommandLine |
||
466 | * @throws \phpbu\App\Exception |
||
467 | */ |
||
468 | 37 | protected function createCommandLine() : CommandLine |
|
515 | |||
516 | /** |
||
517 | * Configure source data (tables, databases) |
||
518 | * |
||
519 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
520 | * @throws \phpbu\App\Exception |
||
521 | */ |
||
522 | 37 | private function configureSourceData(Cmd $cmd) |
|
530 | |||
531 | /** |
||
532 | * Configure source tables |
||
533 | * |
||
534 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
535 | * @throws \phpbu\App\Exception |
||
536 | */ |
||
537 | 2 | private function configureSourceTables(Cmd $cmd) |
|
545 | |||
546 | /** |
||
547 | * Configure source databases |
||
548 | * |
||
549 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
550 | */ |
||
551 | 35 | private function configureSourceDatabases(Cmd $cmd) |
|
567 | |||
568 | /** |
||
569 | * Add --ignore-table options |
||
570 | * |
||
571 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
572 | */ |
||
573 | 36 | private function configureIgnoredTables(Cmd $cmd) |
|
581 | |||
582 | /** |
||
583 | * Add compressor pipe if set |
||
584 | * |
||
585 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
586 | */ |
||
587 | 36 | private function configureCompression(CommandLine $process) |
|
596 | |||
597 | /** |
||
598 | * Configure output redirect |
||
599 | * |
||
600 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
601 | */ |
||
602 | 36 | private function configureOutput(CommandLine $process) |
|
611 | } |
||
612 |