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 | * User to connect with |
||
| 44 | * --user <username> |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $user; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Password to authenticate with |
||
| 52 | * --password <password> |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $password; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * List of tables to backup |
||
| 60 | * --tables array of strings |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $tablesToDump = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List of databases to backup |
||
| 68 | * --databases array of strings |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $databasesToDump = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * List of tables to ignore |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $tablesToIgnore = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * List of tables where only the table structure is stored |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $structureOnly = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Use mysqldump quick mode |
||
| 90 | * -q |
||
| 91 | * |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $quick = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Lock tables option |
||
| 98 | * --lock-tables |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | private $lockTables; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Issue a BEGIN SQL statement before dumping data from server |
||
| 106 | * --single-transaction |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | private $singleTransaction; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Use mysqldump with compression |
||
| 114 | * -C |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | private $compress = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Dump only table structures |
||
| 122 | * --no-data |
||
| 123 | * |
||
| 124 | * @var boolean |
||
| 125 | */ |
||
| 126 | private $noData = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Whether to add SET @@GLOBAL.GTID_PURGED to output |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | private $gtidPurged; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Table separated data files |
||
| 137 | * --tab |
||
| 138 | * |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | private $filePerTable; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Use mysqldump extended insert mode |
||
| 145 | * -e, --extended-insert |
||
| 146 | * |
||
| 147 | * @var boolean |
||
| 148 | */ |
||
| 149 | private $extendedInsert = false; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Dump blob fields as hex. |
||
| 153 | * --hex-blob |
||
| 154 | * |
||
| 155 | * @var boolean |
||
| 156 | */ |
||
| 157 | private $hexBlob = false; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Dump routines. |
||
| 161 | * --routines |
||
| 162 | * |
||
| 163 | * @var boolean |
||
| 164 | */ |
||
| 165 | private $routines = false; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Path to dump file |
||
| 169 | * |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | private $dumpPathname; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Compression command to pipe output to |
||
| 176 | * |
||
| 177 | * @var \phpbu\App\Backup\Target\Compression |
||
| 178 | */ |
||
| 179 | private $compression; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Constructor. |
||
| 183 | * |
||
| 184 | * @param string $path |
||
| 185 | */ |
||
| 186 | 32 | public function __construct(string $path = '') |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Set the mysql credentials. |
||
| 194 | * |
||
| 195 | * @param string $user |
||
| 196 | * @param string $password |
||
| 197 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 198 | */ |
||
| 199 | 15 | public function credentials(string $user = '', string $password = '') : Mysqldump |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Set the mysql hostname. |
||
| 208 | * |
||
| 209 | * @param string $host |
||
| 210 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 211 | */ |
||
| 212 | 14 | public function useHost(string $host) : Mysqldump |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set the mysql port. |
||
| 220 | * |
||
| 221 | * @param int $port |
||
| 222 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 223 | */ |
||
| 224 | 14 | public function usePort(int $port) : Mysqldump |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Use '-q' quick mode. |
||
| 232 | * |
||
| 233 | * @param boolean $bool |
||
| 234 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 235 | */ |
||
| 236 | 13 | public function useQuickMode(bool $bool) : Mysqldump |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Use '--lock-tables' option. |
||
| 244 | * |
||
| 245 | * @param bool $bool |
||
| 246 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 247 | */ |
||
| 248 | 14 | public function lockTables(bool $bool) : Mysqldump |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Use '--single-transaction' option. |
||
| 256 | * |
||
| 257 | * @param bool $bool |
||
| 258 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 259 | */ |
||
| 260 | 14 | public function singleTransaction(bool $bool) : Mysqldump |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Use '-C' compress mode. |
||
| 268 | * |
||
| 269 | * @param bool $bool |
||
| 270 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 271 | */ |
||
| 272 | 13 | public function useCompression(bool $bool) : Mysqldump |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Use '-e' extended insert mode. |
||
| 280 | * |
||
| 281 | * @param bool $bool |
||
| 282 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 283 | */ |
||
| 284 | 14 | public function useExtendedInsert(bool $bool) : Mysqldump |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Use '--hex-blob' to encode binary fields. |
||
| 292 | * |
||
| 293 | * @param bool $bool |
||
| 294 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 295 | */ |
||
| 296 | 14 | public function dumpBlobsHexadecimal(bool $bool) : Mysqldump |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Set tables to dump. |
||
| 304 | * |
||
| 305 | * @param array $tables |
||
| 306 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 307 | */ |
||
| 308 | 15 | public function dumpTables(array $tables) : Mysqldump |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Set databases to dump. |
||
| 316 | * |
||
| 317 | * @param array $databases |
||
| 318 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 319 | */ |
||
| 320 | 16 | public function dumpDatabases(array $databases) : Mysqldump |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Set tables to ignore. |
||
| 328 | * |
||
| 329 | * @param array $tables |
||
| 330 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 331 | */ |
||
| 332 | 14 | public function ignoreTables(array $tables) : Mysqldump |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Set tables where only table structure should be dumped. |
||
| 340 | * |
||
| 341 | * @param array $tables |
||
| 342 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 343 | */ |
||
| 344 | 14 | public function dumpStructureOnly(array $tables) : Mysqldump |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Dump no table data at all. |
||
| 352 | * |
||
| 353 | * @param bool $bool |
||
| 354 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 355 | */ |
||
| 356 | 14 | public function dumpNoData(bool $bool) : Mysqldump |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Add a general transaction ID statement to the dump file. |
||
| 364 | * |
||
| 365 | * @param string $purge |
||
| 366 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 367 | */ |
||
| 368 | 15 | public function addGTIDStatement(string $purge) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Produce table separated data files. |
||
| 376 | * |
||
| 377 | * @param bool $bool |
||
| 378 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 379 | */ |
||
| 380 | 13 | public function produceFilePerTable(bool $bool) : Mysqldump |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Dump procedures and functions. |
||
| 388 | * |
||
| 389 | * @param bool $bool |
||
| 390 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 391 | */ |
||
| 392 | 13 | public function dumpRoutines(bool $bool) : Mysqldump |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Pipe compressor. |
||
| 400 | * |
||
| 401 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
| 402 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 403 | */ |
||
| 404 | 3 | public function compressOutput(Compression $compression) : Mysqldump |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Set the dump target path. |
||
| 412 | * |
||
| 413 | * @param string $path |
||
| 414 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 415 | */ |
||
| 416 | 14 | public function dumpTo(string $path) : Mysqldump |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Mysqldump CommandLine generator. |
||
| 424 | * |
||
| 425 | * @return \SebastianFeldmann\Cli\CommandLine |
||
| 426 | */ |
||
| 427 | 32 | protected function createCommandLine() : CommandLine |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Configure source data (tables, databases). |
||
| 475 | * |
||
| 476 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 477 | * @throws \phpbu\App\Exception |
||
| 478 | */ |
||
| 479 | 32 | private function configureSourceData(Cmd $cmd) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Configure source tables. |
||
| 490 | * |
||
| 491 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 492 | * @throws \phpbu\App\Exception |
||
| 493 | */ |
||
| 494 | 2 | private function configureSourceTables(Cmd $cmd) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Configure source databases. |
||
| 505 | * |
||
| 506 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 507 | */ |
||
| 508 | 30 | private function configureSourceDatabases(Cmd $cmd) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Add --ignore-table options |
||
| 527 | * |
||
| 528 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 529 | */ |
||
| 530 | 31 | private function configureIgnoredTables(Cmd $cmd) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Add compressor pipe if set. |
||
| 541 | * |
||
| 542 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
| 543 | */ |
||
| 544 | 31 | private function configureCompression(CommandLine $process) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Configure output redirect. |
||
| 556 | * |
||
| 557 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
| 558 | */ |
||
| 559 | 31 | private function configureOutput(CommandLine $process) |
|
| 568 | } |
||
| 569 |