Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
16 | class DumpCommand extends AbstractDatabaseCommand |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $tableDefinitions = null; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $commandConfig = null; |
||
27 | |||
28 | protected function configure() |
||
29 | { |
||
30 | $this |
||
31 | ->setName('db:dump') |
||
32 | ->addArgument('filename', InputArgument::OPTIONAL, 'Dump filename') |
||
33 | ->addOption('add-time', 't', InputOption::VALUE_OPTIONAL, 'Adds time to filename (only if filename was not provided)') |
||
|
|||
34 | ->addOption('compression', 'c', InputOption::VALUE_REQUIRED, 'Compress the dump file using one of the supported algorithms') |
||
35 | ->addOption('xml', null, InputOption::VALUE_NONE, 'Dump database in xml format') |
||
36 | ->addOption('hex-blob', null, InputOption::VALUE_NONE, 'Dump binary columns using hexadecimal notation (for example, "abc" becomes 0x616263)') |
||
37 | ->addOption('only-command', null, InputOption::VALUE_NONE, 'Print only mysqldump command. Do not execute') |
||
38 | ->addOption('print-only-filename', null, InputOption::VALUE_NONE, 'Execute and prints no output except the dump filename') |
||
39 | ->addOption('no-single-transaction', null, InputOption::VALUE_NONE, 'Do not use single-transaction (not recommended, this is blocking)') |
||
40 | ->addOption('human-readable', null, InputOption::VALUE_NONE, 'Use a single insert with column names per row. Useful to track database differences. Use db:import --optimize for speeding up the import.') |
||
41 | ->addOption('add-routines', null, InputOption::VALUE_NONE, 'Include stored routines in dump (procedures & functions)') |
||
42 | ->addOption('stdout', null, InputOption::VALUE_NONE, 'Dump to stdout') |
||
43 | ->addOption('strip', 's', InputOption::VALUE_OPTIONAL, 'Tables to strip (dump only structure of those tables)') |
||
44 | ->addOption('exclude', 'e', InputOption::VALUE_OPTIONAL, 'Tables to exclude from the dump') |
||
45 | ->addOption('force', 'f', InputOption::VALUE_NONE, 'Do not prompt if all options are defined') |
||
46 | ->setDescription('Dumps database with mysqldump cli client according to informations from local.xml'); |
||
47 | |||
48 | $help = <<<HELP |
||
49 | Dumps configured magento database with `mysqldump`. You must have installed |
||
50 | the MySQL client tools. |
||
51 | |||
52 | On debian systems run `apt-get install mysql-client` to do that. |
||
53 | |||
54 | The command reads app/etc/local.xml to find the correct settings. |
||
55 | |||
56 | See it in action: http://youtu.be/ttjZHY6vThs |
||
57 | |||
58 | - If you like to prepend a timestamp to the dump name the --add-time option |
||
59 | can be used. |
||
60 | |||
61 | - The command comes with a compression function. Add i.e. `--compression=gz` |
||
62 | to dump directly in gzip compressed file. |
||
63 | |||
64 | HELP; |
||
65 | $this->setHelp($help); |
||
66 | |||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | * |
||
72 | * @deprecated Use database helper |
||
73 | * @throws RuntimeException |
||
74 | */ |
||
75 | public function getTableDefinitions() |
||
76 | { |
||
77 | $this->commandConfig = $this->getCommandConfig(); |
||
78 | |||
79 | if (is_null($this->tableDefinitions)) { |
||
80 | /* @var $dbHelper DatabaseHelper */ |
||
81 | $dbHelper = $this->getHelper('database'); |
||
82 | |||
83 | $this->tableDefinitions = $dbHelper->getTableDefinitions($this->commandConfig); |
||
84 | } |
||
85 | |||
86 | return $this->tableDefinitions; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Generate help for table definitions |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getTableDefinitionHelp() |
||
95 | { |
||
96 | $messages = PHP_EOL; |
||
97 | $this->commandConfig = $this->getCommandConfig(); |
||
98 | $messages .= <<<HELP |
||
99 | <comment>Strip option</comment> |
||
100 | If you like to skip data of some tables you can use the --strip option. |
||
101 | The strip option creates only the structure of the defined tables and |
||
102 | forces `mysqldump` to skip the data. |
||
103 | |||
104 | Separate each table to strip by a space. |
||
105 | You can use wildcards like * and ? in the table names to strip multiple |
||
106 | tables. In addition you can specify pre-defined table groups, that start |
||
107 | with an |
||
108 | |||
109 | Example: "dataflow_batch_export unimportant_module_* @log |
||
110 | |||
111 | $ n98-magerun.phar db:dump --strip="@stripped" |
||
112 | |||
113 | <comment>Available Table Groups</comment> |
||
114 | |||
115 | HELP; |
||
116 | |||
117 | $definitions = $this->getTableDefinitions(); |
||
118 | $list = array(); |
||
119 | $maxNameLen = 0; |
||
120 | foreach ($definitions as $id => $definition) { |
||
121 | $name = '@' . $id; |
||
122 | $description = isset($definition['description']) ? $definition['description'] . '.' : ''; |
||
123 | $nameLen = strlen($name); |
||
124 | if ($nameLen > $maxNameLen) { |
||
125 | $maxNameLen = $nameLen; |
||
126 | } |
||
127 | $list[] = array($name, $description); |
||
128 | } |
||
129 | |||
130 | $decrSize = 78 - $maxNameLen - 3; |
||
131 | |||
132 | foreach ($list as $entry) { |
||
133 | list($name, $description) = $entry; |
||
134 | $delta = max(0, $maxNameLen - strlen($name)); |
||
135 | $spacer = $delta ? str_repeat(' ', $delta) : ''; |
||
136 | $buffer = wordwrap($description, $decrSize); |
||
137 | $buffer = strtr($buffer, array("\n" => "\n" . str_repeat(' ', 3 + $maxNameLen))); |
||
138 | $messages .= sprintf(" <info>%s</info>%s %s\n", $name, $spacer, $buffer); |
||
139 | } |
||
140 | |||
141 | return $messages; |
||
142 | } |
||
143 | |||
144 | public function getHelp() |
||
145 | { |
||
146 | return parent::getHelp() . PHP_EOL |
||
147 | . $this->getCompressionHelp() . PHP_EOL |
||
148 | . $this->getTableDefinitionHelp(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param InputInterface $input |
||
153 | * @param OutputInterface $output |
||
154 | * |
||
155 | * @return int|void |
||
156 | */ |
||
157 | protected function execute(InputInterface $input, OutputInterface $output) |
||
158 | { |
||
159 | // communicate early what is required for this command to run (is enabled) |
||
160 | $enabler = new Enabler($this); |
||
161 | $enabler->functionExists('exec'); |
||
162 | $enabler->functionExists('passthru'); |
||
163 | $enabler->operatingSystemIsNotWindows(); |
||
164 | |||
165 | $this->detectDbSettings($output); |
||
166 | |||
167 | if (!$input->getOption('stdout') && !$input->getOption('only-command') |
||
168 | && !$input->getOption('print-only-filename') |
||
169 | ) { |
||
170 | $this->writeSection($output, 'Dump MySQL Database'); |
||
171 | } |
||
172 | |||
173 | $compressor = $this->getCompressor($input->getOption('compression')); |
||
174 | $fileName = $this->getFileName($input, $output, $compressor); |
||
175 | |||
176 | $stripTables = array(); |
||
177 | View Code Duplication | if ($input->getOption('strip')) { |
|
178 | /* @var $database DatabaseHelper */ |
||
179 | $database = $this->getHelper('database'); |
||
180 | $stripTables = $database->resolveTables(explode(' ', $input->getOption('strip')), $this->getTableDefinitions()); |
||
181 | if (!$input->getOption('stdout') && !$input->getOption('only-command') |
||
182 | && !$input->getOption('print-only-filename') |
||
183 | ) { |
||
184 | $output->writeln('<comment>No-data export for: <info>' . implode(' ', $stripTables) |
||
185 | . '</info></comment>' |
||
186 | ); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | $excludeTables = array(); |
||
191 | View Code Duplication | if ($input->getOption('exclude')) { |
|
192 | $excludeTables = $this->getHelper('database')->resolveTables(explode(' ', $input->getOption('exclude')), $this->getTableDefinitions()); |
||
193 | if (!$input->getOption('stdout') && !$input->getOption('only-command') |
||
194 | && !$input->getOption('print-only-filename') |
||
195 | ) { |
||
196 | $output->writeln('<comment>Excluded: <info>' . implode(' ', $excludeTables) |
||
197 | . '</info></comment>' |
||
198 | ); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | $dumpOptions = ''; |
||
203 | if (!$input->getOption('no-single-transaction')) { |
||
204 | $dumpOptions = '--single-transaction --quick '; |
||
205 | } |
||
206 | |||
207 | if ($input->getOption('human-readable')) { |
||
208 | $dumpOptions .= '--complete-insert --skip-extended-insert '; |
||
209 | } |
||
210 | |||
211 | if ($input->getOption('add-routines')) { |
||
212 | $dumpOptions .= '--routines '; |
||
213 | } |
||
214 | |||
215 | if ($input->getOption('xml')) { |
||
216 | $dumpOptions .= '--xml '; |
||
217 | } |
||
218 | |||
219 | if ($input->getOption('hex-blob')) { |
||
220 | $dumpOptions .= '--hex-blob '; |
||
221 | } |
||
222 | |||
223 | $execs = array(); |
||
224 | |||
225 | $ignore = ''; |
||
226 | foreach (array_merge($excludeTables, $stripTables) as $tableName) { |
||
227 | $ignore .= '--ignore-table=' . $this->dbSettings['dbname'] . '.' . $tableName . ' '; |
||
228 | } |
||
229 | |||
230 | $mysqlClientToolConnectionString = $this->getHelper('database')->getMysqlClientToolConnectionString(); |
||
231 | |||
232 | if (count($stripTables) > 0) { |
||
233 | // dump structure for strip-tables |
||
234 | $exec = 'mysqldump ' . $dumpOptions . '--no-data ' . $mysqlClientToolConnectionString; |
||
235 | $exec .= ' ' . implode(' ', $stripTables); |
||
236 | $exec .= $this->postDumpPipeCommands(); |
||
237 | $exec = $compressor->getCompressingCommand($exec); |
||
238 | if (!$input->getOption('stdout')) { |
||
239 | $exec .= ' > ' . escapeshellarg($fileName); |
||
240 | } |
||
241 | $execs[] = $exec; |
||
242 | } |
||
243 | |||
244 | // dump data for all other tables |
||
245 | $exec = 'mysqldump ' . $dumpOptions . $mysqlClientToolConnectionString . ' ' . $ignore; |
||
246 | $exec .= $this->postDumpPipeCommands(); |
||
247 | $exec = $compressor->getCompressingCommand($exec); |
||
248 | if (!$input->getOption('stdout')) { |
||
249 | $exec .= (count($stripTables) > 0 ? ' >> ' : ' > ') . escapeshellarg($fileName); |
||
250 | } |
||
251 | $execs[] = $exec; |
||
252 | |||
253 | $this->runExecs($execs, $fileName, $input, $output); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param array $execs |
||
258 | * @param string $fileName |
||
259 | * @param InputInterface $input |
||
260 | * @param OutputInterface $output |
||
261 | */ |
||
262 | private function runExecs(array $execs, $fileName, InputInterface $input, OutputInterface $output) |
||
263 | { |
||
264 | if ($input->getOption('only-command') && !$input->getOption('print-only-filename')) { |
||
265 | foreach ($execs as $exec) { |
||
266 | $output->writeln($exec); |
||
267 | } |
||
268 | } else { |
||
269 | if (!$input->getOption('stdout') && !$input->getOption('only-command') |
||
270 | && !$input->getOption('print-only-filename') |
||
271 | ) { |
||
272 | $output->writeln('<comment>Start dumping database <info>' . $this->dbSettings['dbname'] |
||
273 | . '</info> to file <info>' . $fileName . '</info>' |
||
274 | ); |
||
275 | } |
||
276 | |||
277 | foreach ($execs as $exec) { |
||
278 | $commandOutput = ''; |
||
279 | if ($input->getOption('stdout')) { |
||
280 | passthru($exec, $returnValue); |
||
281 | } else { |
||
282 | Exec::run($exec, $commandOutput, $returnValue); |
||
283 | } |
||
284 | if ($returnValue > 0) { |
||
285 | $output->writeln('<error>' . $commandOutput . '</error>'); |
||
286 | $output->writeln('<error>Return Code: ' . $returnValue . '. ABORTED.</error>'); |
||
287 | |||
288 | return; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | if (!$input->getOption('stdout') && !$input->getOption('print-only-filename')) { |
||
293 | $output->writeln('<info>Finished</info>'); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | if ($input->getOption('print-only-filename')) { |
||
298 | $output->writeln($fileName); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Commands which filter mysql data. Piped to mysqldump command |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | protected function postDumpPipeCommands() |
||
311 | |||
312 | /** |
||
313 | * @param InputInterface $input |
||
314 | * @param OutputInterface $output |
||
315 | * @param AbstractCompressor $compressor |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | protected function getFileName(InputInterface $input, OutputInterface $output, AbstractCompressor $compressor) |
||
366 | } |
||
367 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.