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) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Connects to the database without initializing magento |
||
| 76 | * |
||
| 77 | * @param OutputInterface $output = null |
||
| 78 | * |
||
| 79 | * @return PDO |
||
| 80 | */ |
||
| 81 | public function getConnection(OutputInterface $output = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a PDO DSN for the adapter from $this->_config settings. |
||
| 92 | * |
||
| 93 | * @see Zend_Db_Adapter_Pdo_Abstract |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function dsn() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Check whether current mysql user has $privilege privilege |
||
| 103 | * |
||
| 104 | * @param string $privilege |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function mysqlUserHasPrivilege($privilege) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getMysqlClientToolConnectionString() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get mysql variable value |
||
| 134 | * |
||
| 135 | * @param string $variable |
||
| 136 | * |
||
| 137 | * @return bool|array returns array on success, false on failure |
||
| 138 | */ |
||
| 139 | public function getMysqlVariableValue($variable) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * obtain mysql variable value from the database connection. |
||
| 156 | * |
||
| 157 | * in difference to @see getMysqlVariableValue(), this method allows to specify the type of the variable as well |
||
| 158 | * as to use any variable identifier even such that need quoting. |
||
| 159 | * |
||
| 160 | * @param string $name mysql variable name |
||
| 161 | * @param string $type [optional] variable type, can be a system variable ("@@", default) or a session variable |
||
| 162 | * ("@"). |
||
| 163 | * |
||
| 164 | * @return string variable value, null if variable was not defined |
||
| 165 | * @throws RuntimeException in case a system variable is unknown (SQLSTATE[HY000]: 1193: Unknown system variable |
||
| 166 | * 'nonexistent') |
||
| 167 | */ |
||
| 168 | public function getMysqlVariable($name, $type = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array $commandConfig |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getTableDefinitions(array $commandConfig) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param array $list |
||
| 235 | * @param array $definitions |
||
| 236 | * @param array $resolved Which definitions where already resolved -> prevent endless loops |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | * @throws RuntimeException |
||
| 240 | */ |
||
| 241 | public function resolveTables(array $list, array $definitions = array(), array $resolved = array()) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get list of database tables |
||
| 296 | * |
||
| 297 | * @param bool $withoutPrefix [optional] remove prefix from the returned table names. prefix is obtained from |
||
| 298 | * magento database configuration. defaults to false. |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | * @throws RuntimeException |
||
| 302 | */ |
||
| 303 | public function getTables($withoutPrefix = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * throw a runtime exception and provide error info for the statement if available |
||
| 347 | * |
||
| 348 | * @param PDOStatement $statement |
||
| 349 | * @param string $message |
||
| 350 | * |
||
| 351 | * @throws RuntimeException |
||
| 352 | */ |
||
| 353 | private function throwRuntimeException(PDOStatement $statement, $message = "") |
||
| 367 | |||
| 368 | /** |
||
| 369 | * quote a string so that it is safe to use in a LIKE |
||
| 370 | * |
||
| 371 | * @param string $string |
||
| 372 | * @param string $escape character - single us-ascii character |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | private function quoteLike($string, $escape = '=') |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get list of db tables status |
||
| 389 | * |
||
| 390 | * @param bool $withoutPrefix |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getTablesStatus($withoutPrefix = false) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param OutputInterface $output [optional] |
||
| 425 | * |
||
| 426 | * @return array|DbSettings |
||
| 427 | */ |
||
| 428 | public function getDbSettings(OutputInterface $output = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public function getIsSocketConnect() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns the canonical name of this helper. |
||
| 455 | * |
||
| 456 | * @return string The canonical name |
||
| 457 | * |
||
| 458 | * @api |
||
| 459 | */ |
||
| 460 | public function getName() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param OutputInterface $output |
||
| 467 | */ |
||
| 468 | View Code Duplication | public function dropDatabase($output) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param OutputInterface $output |
||
| 478 | */ |
||
| 479 | public function dropTables($output) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param OutputInterface $output |
||
| 495 | */ |
||
| 496 | View Code Duplication | public function createDatabase($output) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $command example: 'VARIABLES', 'STATUS' |
||
| 506 | * @param string $variable [optional] |
||
| 507 | * |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | private function runShowCommand($command, $variable = null) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $variable [optional] |
||
| 542 | * |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | public function getGlobalVariables($variable = null) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param string $variable [optional] |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | public function getGlobalStatus($variable = null) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return Application|BaseApplication |
||
| 562 | */ |
||
| 563 | private function getApplication() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * small helper method to obtain an object of type OutputInterface |
||
| 578 | * |
||
| 579 | * @param OutputInterface|null $output |
||
| 580 | * |
||
| 581 | * @return OutputInterface |
||
| 582 | */ |
||
| 583 | private function fallbackOutput(OutputInterface $output = null) |
||
| 601 | } |
||
| 602 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.