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 DebugBarDatabaseNewProxy 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 DebugBarDatabaseNewProxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class DebugBarDatabaseNewProxy extends SS_Database |
||
|
|
|||
| 9 | { |
||
| 10 | /** @var MySQLDatabase */ |
||
| 11 | protected $realConn; |
||
| 12 | protected $findSource; |
||
| 13 | |||
| 14 | /** @var array */ |
||
| 15 | protected $queries; |
||
| 16 | protected $connector; |
||
| 17 | protected $schemaManager; |
||
| 18 | protected $queryBuilder; |
||
| 19 | protected $showQueries = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param MySQLDatabase|array $realConn |
||
| 23 | */ |
||
| 24 | public function __construct($realConn) |
||
| 25 | { |
||
| 26 | // Some service hooks pass $databaseConfig to constructor |
||
| 27 | if (is_array($realConn)) { |
||
| 28 | $realConn = DB::connect($realConn); |
||
| 29 | } |
||
| 30 | $this->realConn = $realConn; |
||
| 31 | $this->connector = $realConn->getConnector(); |
||
| 32 | $this->schemaManager = $realConn->getSchemaManager(); |
||
| 33 | $this->queryBuilder = $realConn->getQueryBuilder(); |
||
| 34 | $this->queries = array(); |
||
| 35 | $this->findSource = DebugBar::config()->find_source; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getShowQueries() |
||
| 42 | |||
| 43 | public function setShowQueries($showQueries) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the current connector |
||
| 51 | * |
||
| 52 | * @return DBConnector |
||
| 53 | */ |
||
| 54 | public function getConnector() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Injector injection point for connector dependency |
||
| 61 | * |
||
| 62 | * @param DBConnector $connector |
||
| 63 | */ |
||
| 64 | public function setConnector(DBConnector $connector) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the current schema manager |
||
| 72 | * |
||
| 73 | * @return DBSchemaManager |
||
| 74 | */ |
||
| 75 | public function getSchemaManager() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Injector injection point for schema manager |
||
| 82 | * |
||
| 83 | * @param DBSchemaManager $schemaManager |
||
| 84 | */ |
||
| 85 | public function setSchemaManager(DBSchemaManager $schemaManager) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the current query builder |
||
| 93 | * |
||
| 94 | * @return DBQueryBuilder |
||
| 95 | */ |
||
| 96 | public function getQueryBuilder() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Injector injection point for schema manager |
||
| 103 | * |
||
| 104 | * @param DBQueryBuilder $queryBuilder |
||
| 105 | */ |
||
| 106 | public function setQueryBuilder(DBQueryBuilder $queryBuilder) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Determines if the query should be previewed, and thus interrupted silently. |
||
| 114 | * If so, this function also displays the query via the debuging system. |
||
| 115 | * Subclasess should respect the results of this call for each query, and not |
||
| 116 | * execute any queries that generate a true response. |
||
| 117 | * |
||
| 118 | * @param string $sql The query to be executed |
||
| 119 | * @return boolean Flag indicating that the query was previewed |
||
| 120 | */ |
||
| 121 | View Code Duplication | protected function previewWrite($sql) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Allows the display and benchmarking of queries as they are being run |
||
| 137 | * |
||
| 138 | * @param string $sql Query to run, and single parameter to callback |
||
| 139 | * @param callable $callback Callback to execute code |
||
| 140 | * @param array $parameters |
||
| 141 | * @return mixed Result of query |
||
| 142 | */ |
||
| 143 | protected function benchmarkQuery($sql, $callback, $parameters = array()) |
||
| 249 | |||
| 250 | View Code Duplication | protected function findSource() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Execute the given SQL query. |
||
| 337 | * |
||
| 338 | * @param string $sql The SQL query to execute |
||
| 339 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 340 | * @return SS_Query |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function query($sql, $errorLevel = E_USER_ERROR) |
|
| 368 | |||
| 369 | public function oldQuery($sql, $errorLevel = E_USER_ERROR) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Execute the given SQL parameterised query with the specified arguments |
||
| 376 | * |
||
| 377 | * @param string $sql The SQL query to execute. The ? character will denote parameters. |
||
| 378 | * @param array $parameters An ordered list of arguments. |
||
| 379 | * @param int $errorLevel The level of error reporting to enable for the query |
||
| 380 | * @return SS_Query |
||
| 381 | */ |
||
| 382 | View Code Duplication | public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getQueries() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $name |
||
| 409 | * @param array $arguments |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | public function __call($name, $arguments) |
||
| 417 | |||
| 418 | public function addslashes($val) |
||
| 425 | |||
| 426 | View Code Duplication | public function alterTable( |
|
| 440 | |||
| 441 | View Code Duplication | public function comparisonClause( |
|
| 454 | |||
| 455 | public function createDatabase() |
||
| 462 | |||
| 463 | public function createField($table, $field, $spec) |
||
| 470 | |||
| 471 | View Code Duplication | public function createTable( |
|
| 483 | |||
| 484 | public function datetimeDifferenceClause($date1, $date2) |
||
| 491 | |||
| 492 | public function datetimeIntervalClause($date, $interval) |
||
| 499 | |||
| 500 | public function enumValuesForField($tableName, $fieldName) |
||
| 507 | |||
| 508 | public function fieldList($table) |
||
| 515 | |||
| 516 | public function formattedDatetimeClause($date, $format) |
||
| 523 | |||
| 524 | public function getConnect($parameters) |
||
| 531 | |||
| 532 | public function getGeneratedID($table) |
||
| 539 | |||
| 540 | public function hasTable($tableName) |
||
| 547 | |||
| 548 | public function isActive() |
||
| 555 | |||
| 556 | public function renameField($tableName, $oldName, $newName) |
||
| 563 | |||
| 564 | public function renameTable($oldTableName, $newTableName) |
||
| 571 | |||
| 572 | public function supportsTimezoneOverride() |
||
| 579 | |||
| 580 | public function supportsTransactions() |
||
| 587 | |||
| 588 | public function tableList() |
||
| 595 | |||
| 596 | public function transactionEnd($chain = false) |
||
| 603 | |||
| 604 | public function transactionRollback($savepoint = false) |
||
| 611 | |||
| 612 | public function transactionSavepoint($savepoint) |
||
| 619 | |||
| 620 | public function transactionStart($transaction_mode = false, $session_characteristics = false) |
||
| 627 | |||
| 628 | public function clearTable($table) |
||
| 635 | |||
| 636 | public function getDatabaseServer() |
||
| 643 | |||
| 644 | public function now() |
||
| 651 | |||
| 652 | public function random() |
||
| 659 | |||
| 660 | public function searchEngine( |
||
| 676 | |||
| 677 | public function supportsCollations() |
||
| 684 | } |
||
| 685 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.