Complex classes like DumpCommand 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 DumpCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class DumpCommand extends AbstractDatabaseCommand |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $tableDefinitions = null; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $commandConfig = null; |
||
28 | |||
29 | protected function configure() |
||
30 | { |
||
31 | $this |
||
32 | ->setName('db:dump') |
||
33 | ->addArgument('filename', InputArgument::OPTIONAL, 'Dump filename') |
||
34 | ->addOption( |
||
35 | 'add-time', |
||
36 | 't', |
||
37 | InputOption::VALUE_OPTIONAL, |
||
38 | 'Adds time to filename (only if filename was not provided)' |
||
39 | ) |
||
40 | ->addOption( |
||
41 | 'compression', |
||
42 | 'c', |
||
43 | InputOption::VALUE_REQUIRED, |
||
44 | 'Compress the dump file using one of the supported algorithms' |
||
45 | ) |
||
46 | ->addOption( |
||
47 | 'xml', |
||
48 | null, |
||
49 | InputOption::VALUE_NONE, |
||
50 | 'Dump database in xml format' |
||
51 | ) |
||
52 | ->addOption( |
||
53 | 'hex-blob', |
||
54 | null, |
||
55 | InputOption::VALUE_NONE, |
||
56 | 'Dump binary columns using hexadecimal notation (for example, "abc" becomes 0x616263)' |
||
57 | ) |
||
58 | ->addOption( |
||
59 | 'only-command', |
||
60 | null, |
||
61 | InputOption::VALUE_NONE, |
||
62 | 'Print only mysqldump command. Do not execute' |
||
63 | ) |
||
64 | ->addOption( |
||
65 | 'print-only-filename', |
||
66 | null, |
||
67 | InputOption::VALUE_NONE, |
||
68 | 'Execute and prints no output except the dump filename' |
||
69 | ) |
||
70 | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'do everything but the dump') |
||
71 | ->addOption( |
||
72 | 'no-single-transaction', |
||
73 | null, |
||
74 | InputOption::VALUE_NONE, |
||
75 | 'Do not use single-transaction (not recommended, this is blocking)' |
||
76 | ) |
||
77 | ->addOption( |
||
78 | 'human-readable', |
||
79 | null, |
||
80 | InputOption::VALUE_NONE, |
||
81 | 'Use a single insert with column names per row. Useful to track database differences. Use db:import ' . |
||
82 | '--optimize for speeding up the import.' |
||
83 | ) |
||
84 | ->addOption( |
||
85 | 'add-routines', |
||
86 | null, |
||
87 | InputOption::VALUE_NONE, |
||
88 | 'Include stored routines in dump (procedures & functions)' |
||
89 | ) |
||
90 | ->addOption('stdout', null, InputOption::VALUE_NONE, 'Dump to stdout') |
||
91 | ->addOption( |
||
92 | 'strip', |
||
93 | 's', |
||
94 | InputOption::VALUE_OPTIONAL, |
||
95 | 'Tables to strip (dump only structure of those tables)' |
||
96 | ) |
||
97 | ->addOption('exclude', 'e', InputOption::VALUE_OPTIONAL, 'Tables to exclude from the dump') |
||
98 | ->addOption('include', 'i', InputOption::VALUE_OPTIONAL, 'Tables to include in the dump') |
||
99 | ->addOption('force', 'f', InputOption::VALUE_NONE, 'Do not prompt if all options are defined') |
||
100 | ->setDescription('Dumps database with mysqldump cli client according to informations from local.xml'); |
||
101 | |||
102 | $help = <<<HELP |
||
103 | Dumps configured magento database with `mysqldump`. You must have installed |
||
104 | the MySQL client tools. |
||
105 | |||
106 | On debian systems run `apt-get install mysql-client` to do that. |
||
107 | |||
108 | The command reads app/etc/local.xml to find the correct settings. |
||
109 | |||
110 | See it in action: http://youtu.be/ttjZHY6vThs |
||
111 | |||
112 | - If you like to prepend a timestamp to the dump name the --add-time option |
||
113 | can be used. |
||
114 | |||
115 | - The command comes with a compression function. Add i.e. `--compression=gz` |
||
116 | to dump directly in gzip compressed file. |
||
117 | |||
118 | HELP; |
||
119 | $this->setHelp($help); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | * |
||
125 | * @deprecated Use database helper |
||
126 | */ |
||
127 | private function getTableDefinitions() |
||
140 | |||
141 | /** |
||
142 | * Generate help for table definitions |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getTableDefinitionHelp() |
||
200 | |||
201 | public function getHelp() |
||
208 | |||
209 | /** |
||
210 | * @param InputInterface $input |
||
211 | * @param OutputInterface $output |
||
212 | * |
||
213 | * @return int|void |
||
214 | */ |
||
215 | protected function execute(InputInterface $input, OutputInterface $output) |
||
234 | |||
235 | /** |
||
236 | * @param InputInterface $input |
||
237 | * @param OutputInterface $output |
||
238 | * @return array |
||
239 | */ |
||
240 | private function createExecsArray(InputInterface $input, OutputInterface $output) |
||
332 | |||
333 | /** |
||
334 | * @param array $execs |
||
335 | * @param string $fileName |
||
336 | * @param InputInterface $input |
||
337 | * @param OutputInterface $output |
||
338 | */ |
||
339 | private function runExecs(array $execs, $fileName, InputInterface $input, OutputInterface $output) |
||
370 | |||
371 | /** |
||
372 | * @param string $command |
||
373 | * @param InputInterface $input |
||
374 | * @param OutputInterface $output |
||
375 | * @return bool |
||
376 | */ |
||
377 | private function runExec($command, InputInterface $input, OutputInterface $output) |
||
396 | |||
397 | /** |
||
398 | * @param InputInterface $input |
||
399 | * @param OutputInterface $output |
||
400 | * @return array |
||
401 | */ |
||
402 | private function stripTables(InputInterface $input, OutputInterface $output) |
||
421 | |||
422 | /** |
||
423 | * Commands which filter mysql data. Piped to mysqldump command |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | protected function postDumpPipeCommands() |
||
431 | |||
432 | /** |
||
433 | * @param InputInterface $input |
||
434 | * @param OutputInterface $output |
||
435 | * @param Compressor $compressor |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | protected function getFileName(InputInterface $input, OutputInterface $output, Compressor $compressor) |
||
503 | |||
504 | /** |
||
505 | * @param InputInterface $input |
||
506 | * @return bool |
||
507 | */ |
||
508 | private function nonCommandOutput(InputInterface $input) |
||
515 | } |
||
516 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.