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 |
||
14 | class DumpCommand extends AbstractDatabaseCommand |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $tableDefinitions = null; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $commandConfig = null; |
||
25 | |||
26 | protected function configure() |
||
27 | { |
||
28 | $this |
||
29 | ->setName('db:dump') |
||
30 | ->addArgument('filename', InputArgument::OPTIONAL, 'Dump filename') |
||
31 | ->addOption( |
||
32 | 'add-time', |
||
33 | 't', |
||
34 | InputOption::VALUE_OPTIONAL, |
||
35 | 'Adds time to filename (only if filename was not provided)' |
||
36 | ) |
||
37 | ->addOption( |
||
38 | 'compression', |
||
39 | 'c', |
||
40 | InputOption::VALUE_REQUIRED, |
||
41 | 'Compress the dump file using one of the supported algorithms' |
||
42 | ) |
||
43 | ->addOption( |
||
44 | 'only-command', |
||
45 | null, |
||
46 | InputOption::VALUE_NONE, |
||
47 | 'Print only mysqldump command. |
||
48 | Do not execute' |
||
49 | ) |
||
50 | ->addOption( |
||
51 | 'print-only-filename', |
||
52 | null, |
||
53 | InputOption::VALUE_NONE, |
||
54 | 'Execute and prints no output except the dump filename' |
||
55 | ) |
||
56 | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'do everything but the dump') |
||
57 | ->addOption( |
||
58 | 'no-single-transaction', |
||
59 | null, |
||
60 | InputOption::VALUE_NONE, |
||
61 | 'Do not use single-transaction (not recommended, this is blocking)' |
||
62 | ) |
||
63 | ->addOption( |
||
64 | 'human-readable', |
||
65 | null, |
||
66 | InputOption::VALUE_NONE, |
||
67 | 'Use a single insert with column names per row. Useful to track database differences. Use ' . |
||
68 | 'db:import --optimize for speeding up the import.' |
||
69 | ) |
||
70 | ->addOption( |
||
71 | 'add-routines', |
||
72 | null, |
||
73 | InputOption::VALUE_NONE, |
||
74 | 'Include stored routines in dump (procedures & functions)' |
||
75 | ) |
||
76 | ->addOption('stdout', null, InputOption::VALUE_NONE, 'Dump to stdout') |
||
77 | ->addOption( |
||
78 | 'strip', |
||
79 | 's', |
||
80 | InputOption::VALUE_OPTIONAL, |
||
81 | 'Tables to strip (dump only structure of those tables)' |
||
82 | ) |
||
83 | ->addOption('force', 'f', InputOption::VALUE_NONE, 'Do not prompt if all options are defined') |
||
84 | ->setDescription('Dumps database with mysqldump cli client according to informations from env.php'); |
||
85 | |||
86 | $help = <<<HELP |
||
87 | Dumps configured magento database with `mysqldump`. |
||
88 | You must have installed the MySQL client tools. |
||
89 | |||
90 | On debian systems run `apt-get install mysql-client` to do that. |
||
91 | |||
92 | The command reads app/etc/env.php to find the correct settings. |
||
93 | If you like to skip data of some tables you can use the --strip option. |
||
94 | The strip option creates only the structure of the defined tables and |
||
95 | forces `mysqldump` to skip the data. |
||
96 | |||
97 | Dumps your database and excludes some tables. This is useful i.e. for development. |
||
98 | |||
99 | Separate each table to strip by a space. |
||
100 | You can use wildcards like * and ? in the table names to strip multiple tables. |
||
101 | In addition you can specify pre-defined table groups, that start with an @ |
||
102 | Example: "dataflow_batch_export unimportant_module_* @log |
||
103 | |||
104 | $ n98-magerun.phar db:dump --strip="@stripped" |
||
105 | |||
106 | Available Table Groups: |
||
107 | |||
108 | * @log Log tables |
||
109 | * @dataflowtemp Temporary tables of the dataflow import/export tool |
||
110 | * @stripped Standard definition for a stripped dump (logs and dataflow) |
||
111 | * @sales Sales data (orders, invoices, creditmemos etc) |
||
112 | * @customers Customer data |
||
113 | * @trade Current trade data (customers and orders). You usally do not want those in developer systems. |
||
114 | * @development Removes logs and trade data so developers do not have to work with real customer data |
||
115 | |||
116 | Extended: https://github.com/netz98/n98-magerun/wiki/Stripped-Database-Dumps |
||
117 | |||
118 | See it in action: http://youtu.be/ttjZHY6vThs |
||
119 | |||
120 | - If you like to prepend a timestamp to the dump name the --add-time option can be used. |
||
121 | |||
122 | - The command comes with a compression function. Add i.e. `--compression=gz` to dump directly in |
||
123 | gzip compressed file. |
||
124 | |||
125 | HELP; |
||
126 | $this->setHelp($help); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function isEnabled() |
||
136 | |||
137 | /** |
||
138 | * @return array |
||
139 | * |
||
140 | * @deprecated Use database helper |
||
141 | */ |
||
142 | private function getTableDefinitions() |
||
152 | |||
153 | /** |
||
154 | * Generate help for table definitions |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getTableDefinitionHelp() |
||
183 | |||
184 | public function getHelp() |
||
190 | |||
191 | /** |
||
192 | * @param InputInterface $input |
||
193 | * @param OutputInterface $output |
||
194 | * @return int|void |
||
195 | */ |
||
196 | protected function execute(InputInterface $input, OutputInterface $output) |
||
208 | |||
209 | /** |
||
210 | * @param InputInterface $input |
||
211 | * @param OutputInterface $output |
||
212 | * @return Execs |
||
213 | */ |
||
214 | private function createExecs(InputInterface $input, OutputInterface $output) |
||
259 | |||
260 | /** |
||
261 | * @param Execs $execs |
||
262 | * @param InputInterface $input |
||
263 | * @param OutputInterface $output |
||
264 | */ |
||
265 | private function runExecs(Execs $execs, InputInterface $input, OutputInterface $output) |
||
296 | |||
297 | /** |
||
298 | * @param string $command |
||
299 | * @param InputInterface $input |
||
300 | * @param OutputInterface $output |
||
301 | * @return bool |
||
302 | */ |
||
303 | private function runExec($command, InputInterface $input, OutputInterface $output) |
||
322 | |||
323 | /** |
||
324 | * @param InputInterface $input |
||
325 | * @param OutputInterface $output |
||
326 | * @return false|array |
||
327 | */ |
||
328 | private function stripTables(InputInterface $input, OutputInterface $output) |
||
347 | |||
348 | /** |
||
349 | * Commands which filter mysql data. Piped to mysqldump command |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | protected function postDumpPipeCommands() |
||
357 | |||
358 | /** |
||
359 | * @param InputInterface $input |
||
360 | * @param OutputInterface $output |
||
361 | * @param AbstractCompressor $compressor |
||
362 | * @return string |
||
363 | */ |
||
364 | protected function getFileName( |
||
417 | |||
418 | /** |
||
419 | * @param InputInterface $input |
||
420 | * @return bool |
||
421 | */ |
||
422 | private function nonCommandOutput(InputInterface $input) |
||
429 | } |
||
430 |
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.