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 boolean |
||
| 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 | * Use mysqldump extended insert mode |
||
| 153 | * -e, --extended-insert |
||
| 154 | * |
||
| 155 | * @var boolean |
||
| 156 | */ |
||
| 157 | private $extendedInsert = false; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Dump blob fields as hex. |
||
| 161 | * --hex-blob |
||
| 162 | * |
||
| 163 | * @var boolean |
||
| 164 | */ |
||
| 165 | private $hexBlob = false; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Dump routines. |
||
| 169 | * --routines |
||
| 170 | * |
||
| 171 | * @var boolean |
||
| 172 | */ |
||
| 173 | private $routines = false; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Path to dump file |
||
| 177 | * |
||
| 178 | * @var string |
||
| 179 | */ |
||
| 180 | private $dumpPathname; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Compression command to pipe output to |
||
| 184 | * |
||
| 185 | * @var \phpbu\App\Backup\Target\Compression |
||
| 186 | */ |
||
| 187 | private $compression; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Constructor. |
||
| 191 | * |
||
| 192 | * @param string $path |
||
| 193 | */ |
||
| 194 | 35 | public function __construct(string $path = '') |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Set the mysql credentials. |
||
| 202 | * |
||
| 203 | * @param string $user |
||
| 204 | * @param string $password |
||
| 205 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 206 | */ |
||
| 207 | 16 | public function credentials(string $user = '', string $password = '') : Mysqldump |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the mysql hostname. |
||
| 216 | * |
||
| 217 | * @param string $host |
||
| 218 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 219 | */ |
||
| 220 | 15 | public function useHost(string $host) : Mysqldump |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Set the mysql port. |
||
| 228 | * |
||
| 229 | * @param int $port |
||
| 230 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 231 | */ |
||
| 232 | 15 | public function usePort(int $port) : Mysqldump |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Set the connection protocol. |
||
| 240 | * |
||
| 241 | * @param string $protocol |
||
| 242 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 243 | */ |
||
| 244 | 15 | public function useProtocol(string $protocol) : Mysqldump |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Use '-q' quick mode. |
||
| 252 | * |
||
| 253 | * @param boolean $bool |
||
| 254 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 255 | */ |
||
| 256 | 14 | public function useQuickMode(bool $bool) : Mysqldump |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Use '--lock-tables' option. |
||
| 264 | * |
||
| 265 | * @param bool $bool |
||
| 266 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 267 | */ |
||
| 268 | 15 | public function lockTables(bool $bool) : Mysqldump |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Use '--single-transaction' option. |
||
| 276 | * |
||
| 277 | * @param bool $bool |
||
| 278 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 279 | */ |
||
| 280 | 15 | public function singleTransaction(bool $bool) : Mysqldump |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Use '-C' compress mode. |
||
| 288 | * |
||
| 289 | * @param bool $bool |
||
| 290 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 291 | */ |
||
| 292 | 14 | public function useCompression(bool $bool) : Mysqldump |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Use '-e' extended insert mode. |
||
| 300 | * |
||
| 301 | * @param bool $bool |
||
| 302 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 303 | */ |
||
| 304 | 15 | public function useExtendedInsert(bool $bool) : Mysqldump |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Use '--hex-blob' to encode binary fields. |
||
| 312 | * |
||
| 313 | * @param bool $bool |
||
| 314 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 315 | */ |
||
| 316 | 15 | public function dumpBlobsHexadecimal(bool $bool) : Mysqldump |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Set tables to dump. |
||
| 324 | * |
||
| 325 | * @param array $tables |
||
| 326 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 327 | */ |
||
| 328 | 16 | public function dumpTables(array $tables) : Mysqldump |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set databases to dump. |
||
| 336 | * |
||
| 337 | * @param array $databases |
||
| 338 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 339 | */ |
||
| 340 | 17 | public function dumpDatabases(array $databases) : Mysqldump |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Set tables to ignore. |
||
| 348 | * |
||
| 349 | * @param array $tables |
||
| 350 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 351 | */ |
||
| 352 | 15 | public function ignoreTables(array $tables) : Mysqldump |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Set tables where only table structure should be dumped. |
||
| 360 | * |
||
| 361 | * @param array $tables |
||
| 362 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 363 | */ |
||
| 364 | 15 | public function dumpStructureOnly(array $tables) : Mysqldump |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Dump no table data at all. |
||
| 372 | * |
||
| 373 | * @param bool $bool |
||
| 374 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 375 | */ |
||
| 376 | 15 | public function dumpNoData(bool $bool) : Mysqldump |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Add a general transaction ID statement to the dump file. |
||
| 384 | * |
||
| 385 | * @param string $purge |
||
| 386 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 387 | */ |
||
| 388 | 16 | public function addGTIDStatement(string $purge) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Produce table separated data files. |
||
| 396 | * |
||
| 397 | * @param bool $bool |
||
| 398 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 399 | */ |
||
| 400 | 14 | public function produceFilePerTable(bool $bool) : Mysqldump |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Dump procedures and functions. |
||
| 408 | * |
||
| 409 | * @param bool $bool |
||
| 410 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 411 | */ |
||
| 412 | 14 | public function dumpRoutines(bool $bool) : Mysqldump |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Pipe compressor. |
||
| 420 | * |
||
| 421 | * @param \phpbu\App\Backup\Target\Compression $compression |
||
| 422 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 423 | */ |
||
| 424 | 3 | public function compressOutput(Compression $compression) : Mysqldump |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Set the dump target path. |
||
| 432 | * |
||
| 433 | * @param string $path |
||
| 434 | * @return \phpbu\App\Cli\Executable\Mysqldump |
||
| 435 | */ |
||
| 436 | 15 | public function dumpTo(string $path) : Mysqldump |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Mysqldump CommandLine generator. |
||
| 444 | * |
||
| 445 | * @return \SebastianFeldmann\Cli\CommandLine |
||
| 446 | * @throws \phpbu\App\Exception |
||
| 447 | */ |
||
| 448 | 35 | protected function createCommandLine() : CommandLine |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Configure source data (tables, databases). |
||
| 497 | * |
||
| 498 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 499 | * @throws \phpbu\App\Exception |
||
| 500 | */ |
||
| 501 | 35 | private function configureSourceData(Cmd $cmd) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Configure source tables. |
||
| 512 | * |
||
| 513 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 514 | * @throws \phpbu\App\Exception |
||
| 515 | */ |
||
| 516 | 2 | private function configureSourceTables(Cmd $cmd) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Configure source databases. |
||
| 527 | * |
||
| 528 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 529 | */ |
||
| 530 | 33 | private function configureSourceDatabases(Cmd $cmd) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Add --ignore-table options |
||
| 549 | * |
||
| 550 | * @param \SebastianFeldmann\Cli\Command\Executable $cmd |
||
| 551 | */ |
||
| 552 | 34 | private function configureIgnoredTables(Cmd $cmd) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Add compressor pipe if set. |
||
| 563 | * |
||
| 564 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
| 565 | */ |
||
| 566 | 34 | private function configureCompression(CommandLine $process) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Configure output redirect. |
||
| 578 | * |
||
| 579 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
| 580 | */ |
||
| 581 | 34 | private function configureOutput(CommandLine $process) |
|
| 590 | } |
||
| 591 |