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 DatabaseHelper 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 DatabaseHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DatabaseHelper extends AbstractHelper |
||
22 | { |
||
23 | /** |
||
24 | * @var array|DbSettings |
||
25 | */ |
||
26 | protected $dbSettings = null; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | * @deprecated since 1.97.9, use $dbSettings->isSocketConnect() |
||
31 | */ |
||
32 | protected $isSocketConnect = false; |
||
33 | |||
34 | /** |
||
35 | * @var PDO |
||
36 | */ |
||
37 | protected $_connection = null; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $_tables; |
||
43 | |||
44 | /** |
||
45 | * @param OutputInterface $output |
||
46 | * |
||
47 | * @throws RuntimeException |
||
48 | */ |
||
49 | public function detectDbSettings(OutputInterface $output) |
||
72 | |||
73 | /** |
||
74 | * Connects to the database without initializing magento |
||
75 | * |
||
76 | * @param OutputInterface $output = null |
||
77 | * |
||
78 | * @return PDO |
||
79 | */ |
||
80 | public function getConnection(OutputInterface $output = null) |
||
88 | |||
89 | /** |
||
90 | * Creates a PDO DSN for the adapter from $this->_config settings. |
||
91 | * |
||
92 | * @see Zend_Db_Adapter_Pdo_Abstract |
||
93 | * @return string |
||
94 | */ |
||
95 | public function dsn() |
||
99 | |||
100 | /** |
||
101 | * Check whether current mysql user has $privilege privilege |
||
102 | * |
||
103 | * @param string $privilege |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function mysqlUserHasPrivilege($privilege) |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getMysqlClientToolConnectionString() |
||
130 | |||
131 | /** |
||
132 | * Get mysql variable value |
||
133 | * |
||
134 | * @param string $variable |
||
135 | * |
||
136 | * @return bool|array returns array on success, false on failure |
||
137 | */ |
||
138 | public function getMysqlVariableValue($variable) |
||
152 | |||
153 | /** |
||
154 | * obtain mysql variable value from the database connection. |
||
155 | * |
||
156 | * in difference to @see getMysqlVariableValue(), this method allows to specify the type of the variable as well |
||
157 | * as to use any variable identifier even such that need quoting. |
||
158 | * |
||
159 | * @param string $name mysql variable name |
||
160 | * @param string $type [optional] variable type, can be a system variable ("@@", default) or a session variable |
||
161 | * ("@"). |
||
162 | * |
||
163 | * @return string variable value, null if variable was not defined |
||
164 | * @throws RuntimeException in case a system variable is unknown (SQLSTATE[HY000]: 1193: Unknown system variable |
||
165 | * 'nonexistent') |
||
166 | */ |
||
167 | public function getMysqlVariable($name, $type = null) |
||
200 | |||
201 | /** |
||
202 | * @param array $commandConfig |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getTableDefinitions(array $commandConfig) |
||
231 | |||
232 | /** |
||
233 | * @param array $list |
||
234 | * @param array $definitions |
||
235 | * @param array $resolved Which definitions where already resolved -> prevent endless loops |
||
236 | * |
||
237 | * @return array |
||
238 | * @throws RuntimeException |
||
239 | */ |
||
240 | public function resolveTables(array $list, array $definitions = array(), array $resolved = array()) |
||
285 | |||
286 | /** |
||
287 | * Get list of database tables |
||
288 | * |
||
289 | * @param bool $withoutPrefix [optional] remove prefix from the returned table names. prefix is obtained from |
||
290 | * magento database configuration. defaults to false. |
||
291 | * |
||
292 | * @return array |
||
293 | * @throws RuntimeException |
||
294 | */ |
||
295 | public function getTables($withoutPrefix = null) |
||
337 | |||
338 | /** |
||
339 | * throw a runtime exception and provide error info for the statement if available |
||
340 | * |
||
341 | * @param PDOStatement $statement |
||
342 | * @param string $message |
||
343 | * |
||
344 | * @throws RuntimeException |
||
345 | */ |
||
346 | private function throwRuntimeException(PDOStatement $statement, $message = "") |
||
360 | |||
361 | /** |
||
362 | * quote a string so that it is safe to use in a LIKE |
||
363 | * |
||
364 | * @param string $string |
||
365 | * @param string $escape character - single us-ascii character |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | private function quoteLike($string, $escape = '=') |
||
379 | |||
380 | /** |
||
381 | * Get list of db tables status |
||
382 | * |
||
383 | * @param bool $withoutPrefix |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | public function getTablesStatus($withoutPrefix = false) |
||
415 | |||
416 | /** |
||
417 | * @param OutputInterface $output [optional] |
||
418 | * |
||
419 | * @return array|DbSettings |
||
420 | */ |
||
421 | public function getDbSettings(OutputInterface $output = null) |
||
437 | |||
438 | /** |
||
439 | * @return boolean |
||
440 | */ |
||
441 | public function getIsSocketConnect() |
||
445 | |||
446 | /** |
||
447 | * Returns the canonical name of this helper. |
||
448 | * |
||
449 | * @return string The canonical name |
||
450 | * |
||
451 | * @api |
||
452 | */ |
||
453 | public function getName() |
||
457 | |||
458 | /** |
||
459 | * @param OutputInterface $output |
||
460 | */ |
||
461 | View Code Duplication | public function dropDatabase($output) |
|
468 | |||
469 | /** |
||
470 | * @param OutputInterface $output |
||
471 | */ |
||
472 | public function dropTables($output) |
||
485 | |||
486 | /** |
||
487 | * @param OutputInterface $output |
||
488 | */ |
||
489 | View Code Duplication | public function createDatabase($output) |
|
496 | |||
497 | /** |
||
498 | * @param string $command example: 'VARIABLES', 'STATUS' |
||
499 | * @param string $variable [optional] |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | private function runShowCommand($command, $variable = null) |
||
532 | |||
533 | /** |
||
534 | * @param string $variable [optional] |
||
535 | * |
||
536 | * @return array |
||
537 | */ |
||
538 | public function getGlobalVariables($variable = null) |
||
542 | |||
543 | /** |
||
544 | * @param string $variable [optional] |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | public function getGlobalStatus($variable = null) |
||
552 | |||
553 | /** |
||
554 | * @return Application|BaseApplication |
||
555 | */ |
||
556 | private function getApplication() |
||
568 | |||
569 | /** |
||
570 | * small helper method to obtain an object of type OutputInterface |
||
571 | * |
||
572 | * @param OutputInterface|null $output |
||
573 | * |
||
574 | * @return OutputInterface |
||
575 | */ |
||
576 | private function fallbackOutput(OutputInterface $output = null) |
||
593 | } |
||
594 |
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.