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 DBCore 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 DBCore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DBCore { |
||
| 18 | /** |
||
| 19 | * An array containing all the opened connections. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $connections = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The incremented index of connections. |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $index = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Current connection index. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $currIndex = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Instance of a class. |
||
| 41 | * |
||
| 42 | * @var DBCore |
||
| 43 | */ |
||
| 44 | protected static $instance; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns an instance of this class. |
||
| 48 | * |
||
| 49 | * @return DBCore |
||
| 50 | */ |
||
| 51 | public static function getInstance() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Reset the internal static instance. |
||
| 61 | */ |
||
| 62 | public static function resetInstance() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Reset this instance of the manager. |
||
| 71 | */ |
||
| 72 | public function reset() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Seves a new connection to DBCore->connections. |
||
| 83 | * |
||
| 84 | * @param mysqli Object $connResource An object which represents the connection to a MySQL Server. |
||
| 85 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
| 86 | * |
||
| 87 | * @throws DBCoreException If trying to save a connection with an existing name. |
||
| 88 | */ |
||
| 89 | public static function connection($connResource = null, $connName = null) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Seves a new connection to DBCore->connections. |
||
| 98 | * |
||
| 99 | * @param mysqli Object $connResource An object which represents the connection to a MySQL Server. |
||
| 100 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
| 101 | * |
||
| 102 | * @throws DBCoreException If trying to save a connection with an existing name. |
||
| 103 | */ |
||
| 104 | public function openConnection($connResource, $connName = null) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the connection instance for the passed name. |
||
| 120 | * |
||
| 121 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
| 122 | * |
||
| 123 | * @return mysqli Object |
||
| 124 | * |
||
| 125 | * @throws DBCoreException If trying to get a non-existent connection. |
||
| 126 | */ |
||
| 127 | public function getConnection($connName) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the name of the passed connection instance. |
||
| 137 | * |
||
| 138 | * @param mysqli Object $connResource Connection object to be searched for. |
||
| 139 | * |
||
| 140 | * @return string The name of the connection. |
||
| 141 | */ |
||
| 142 | public function getConnectionName($connResource) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Closes the specified connection. |
||
| 148 | * |
||
| 149 | * @param mixed $connection Connection object or its name. |
||
| 150 | */ |
||
| 151 | public function closeConnection($connection) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns all opened connections. |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function getConnections() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets the current connection to $key. |
||
| 183 | * |
||
| 184 | * @param mixed $key The connection key |
||
| 185 | * |
||
| 186 | * @throws DBCoreException |
||
| 187 | */ |
||
| 188 | public function setCurrentConnection($key) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Whether or not the DBCore contains specified connection. |
||
| 197 | * |
||
| 198 | * @param mixed $key The connection key |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function contains($key) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the number of opened connections. |
||
| 208 | * |
||
| 209 | * @return int |
||
| 210 | */ |
||
| 211 | public function count() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns an ArrayIterator that iterates through all connections. |
||
| 217 | * |
||
| 218 | * @return ArrayIterator |
||
| 219 | */ |
||
| 220 | public function getIterator() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the current connection instance. |
||
| 226 | * |
||
| 227 | * @throws DBCoreException If there are no open connections |
||
| 228 | * |
||
| 229 | * @return mysqli Object |
||
| 230 | */ |
||
| 231 | public function getCurrentConnection() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check database errors. |
||
| 242 | * |
||
| 243 | * @param object $dbObj |
||
| 244 | */ |
||
| 245 | private static function checkDbError($dbObj) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Bind parameters to the statment with dynamic number of parameters. |
||
| 253 | * |
||
| 254 | * @param resource $stmt Statement. |
||
| 255 | * @param string $types Types string. |
||
| 256 | * @param array $params Parameters. |
||
| 257 | */ |
||
| 258 | private static function bindParameters($stmt, $types, $params) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return parameters from the statment with dynamic number of parameters. |
||
| 270 | * |
||
| 271 | * @param resource $stmt Statement. |
||
| 272 | * @param array $params Parameters. |
||
|
|
|||
| 273 | */ |
||
| 274 | public static function bindResults($stmt) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Execute DB SQL queries using Prepared Statements. |
||
| 304 | * |
||
| 305 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 306 | * if single parameter. |
||
| 307 | * @param string $types Types string (ex: "isdb"). |
||
| 308 | * @param array $params Parameters in the same order like types string. |
||
| 309 | * |
||
| 310 | * @return mixed Statement object or FALSE if an error occurred. |
||
| 311 | */ |
||
| 312 | private static function doQuery($query, $types = "", $params = []) { |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Execute update DB SQL queries using Prepared Statements. |
||
| 341 | * |
||
| 342 | * @param string $query SQL query template string or DBPreparedQuery object |
||
| 343 | * if single parameter. |
||
| 344 | * @param string $types Types string. |
||
| 345 | * @param array $params Parameters. |
||
| 346 | * |
||
| 347 | * @return int Returns the number of affected rows on success and |
||
| 348 | * -1 if the last query failed. |
||
| 349 | */ |
||
| 350 | public static function doUpdateQuery($query, $types = "", $params = []) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Execute select DB SQL queries using Prepared Statements. |
||
| 375 | * |
||
| 376 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 377 | * if single parameter. |
||
| 378 | * @param string $types Types string (ex: "isdb"). |
||
| 379 | * @param array $params Parameters in the same order like types string. |
||
| 380 | * |
||
| 381 | * @return mixed Statement object or FALSE if an error occurred. |
||
| 382 | */ |
||
| 383 | public static function doSelectQuery($query, $types = "", $params = []) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns list of database table fields. |
||
| 394 | * |
||
| 395 | * @param string $tableName Name of the table. |
||
| 396 | * @return array<string> List of the database table fields (syntax: array[fieldName] = fieldType) |
||
| 397 | */ |
||
| 398 | public static function getTableFieldsList($tableName) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Returns printable SQL field value for table fields list generator. |
||
| 431 | * |
||
| 432 | * @param string $type SQL type of the field. |
||
| 433 | * @param mixed $value Field value. |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | private static function getPrintableSQLValue($type, $value) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns printable field description string for table fields list generator. |
||
| 472 | * |
||
| 473 | * @param string $field Field name. |
||
| 474 | * @param array $attributes List of field attributes. |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public static function getPrintableFieldString($field, $attributes) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Outputs comfortable for Bean Class creation list of table fields. |
||
| 499 | * |
||
| 500 | * @param string $tableName Name of the Db table. |
||
| 501 | */ |
||
| 502 | public static function displayTableFieldsList($tableName) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Returns list of fields values with default indexes. |
||
| 517 | * |
||
| 518 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 519 | * @param string $idFieldName Name of the primary key field. |
||
| 520 | * @return array<mixed> |
||
| 521 | */ |
||
| 522 | private static function createValuesList($fieldsList, $idFieldName = "") { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Executes SQL INSERT query to the database. |
||
| 535 | * |
||
| 536 | * @param DBObject $dbObject DBObject to insert. |
||
| 537 | * @param bool $debug Debug mode flag. |
||
| 538 | * |
||
| 539 | * @return int Insertion ID (primary key value) or null on debug. |
||
| 540 | */ |
||
| 541 | public static function insertDBObject($dbObject, $debug = false) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Executes SQL UPDATE query to the database. |
||
| 569 | * |
||
| 570 | * @param DBObject $dbObject DBObject to update. |
||
| 571 | * @param bool $debug Debug mode flag. |
||
| 572 | * |
||
| 573 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 574 | * the last query failed. |
||
| 575 | */ |
||
| 576 | public static function updateDBObject($dbObject, $debug = false) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Executes SQL DELETE query to the database. |
||
| 602 | * |
||
| 603 | * @param DBObject $dbObject DBObject to delete. |
||
| 604 | * |
||
| 605 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 606 | * the last query failed. |
||
| 607 | */ |
||
| 608 | public static function deleteDBObject($dbObject) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Returns DBObject from ResultSet. |
||
| 627 | * |
||
| 628 | * @param DBObject $dbObject |
||
| 629 | * @param array $resultSet Associated by table names arrays of selected |
||
| 630 | * fields. |
||
| 631 | * |
||
| 632 | * @return DBObject |
||
| 633 | */ |
||
| 634 | public static function selectDBObjectFromResultSet($dbObject, $resultSet) { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Returns DB object by database query statement. |
||
| 642 | * |
||
| 643 | * @param resource $stmt Database query statement. |
||
| 644 | * @param string $className Name of the DB object class. |
||
| 645 | * @return DBObject |
||
| 646 | */ |
||
| 647 | public static function selectDBObjectFromStatement($stmt, $className) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Selects DBObject from database. |
||
| 671 | * |
||
| 672 | * @param string $query SQL query. |
||
| 673 | * @param string $types Types string (ex: "isdb"). |
||
| 674 | * @param array $params Parameters in the same order like types string. |
||
| 675 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
| 676 | * |
||
| 677 | * @return DBObject Selected DBObject or NULL otherwise. |
||
| 678 | */ |
||
| 679 | View Code Duplication | public static function selectDBObject($query, $types, $params, $instance) { |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Returns list of DB objects by database query statement. |
||
| 693 | * |
||
| 694 | * @param resource $stmt Database query statement. |
||
| 695 | * @param mixed $className Instance of the some DBObject class or it's class name. |
||
| 696 | * |
||
| 697 | * @return array<DBObject> |
||
| 698 | */ |
||
| 699 | public static function selectDBObjectsFromStatement($stmt, $className) { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Selects DBObject list from database. |
||
| 726 | * |
||
| 727 | * @param string $query SQL query. |
||
| 728 | * @param string $types Types string (ex: "isdb"). |
||
| 729 | * @param array $params Parameters in the same order like types string. |
||
| 730 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
| 731 | * |
||
| 732 | * @return DBObject Selected DBObject or NULL otherwise. |
||
| 733 | */ |
||
| 734 | View Code Duplication | public static function selectDBObjects($query, $types, $params, $instance) { |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Executes SQL query with single record and return this record. |
||
| 748 | * |
||
| 749 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 750 | * if single parameter. |
||
| 751 | * @param string $types Types string (ex: "isdb"). |
||
| 752 | * @param array $params Parameters in the same order like types string. |
||
| 753 | * |
||
| 754 | * @return array Selected record with table names as keys or NULL if no |
||
| 755 | * data selected. |
||
| 756 | * @throws DBCoreException If no one or more than one records selected. |
||
| 757 | */ |
||
| 758 | View Code Duplication | public static function selectSingleRecord($query, $types = "", $params = []) { |
|
| 782 | |||
| 783 | /** |
||
| 784 | * Executes SQL query with single record and value result and return this value. |
||
| 785 | * |
||
| 786 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 787 | * if single parameter. |
||
| 788 | * @param string $types Types string (ex: "isdb"). |
||
| 789 | * @param array $params Parameters in the same order like types string. |
||
| 790 | * |
||
| 791 | * @return mixed |
||
| 792 | * @throws DBCoreException If no one or more than one records selected. |
||
| 793 | */ |
||
| 794 | View Code Duplication | public static function selectSingleValue($query, $types = "", $params = []) { |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Calls DBCore magic methods like: |
||
| 822 | * get[User]By[Id]($userId) |
||
| 823 | * get[User]By[Email]($email) |
||
| 824 | * get[Users]() |
||
| 825 | * delete[Users]($ids) |
||
| 826 | * delete[User]($userId) |
||
| 827 | * set[User]Activation($activationFieldName, $flagValue). |
||
| 828 | * |
||
| 829 | * @param string $methodName Name of the magic method. |
||
| 830 | * @param array $methodParams List of dynamic parameters. |
||
| 831 | * |
||
| 832 | * @return mixed |
||
| 833 | * @throws DBCoreException |
||
| 834 | */ |
||
| 835 | public static function __callStatic($methodName, $methodParams) { |
||
| 945 | |||
| 946 | } |
||
| 947 | |||
| 952 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.