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 | * Execute DB SQL queries using Prepared Statements. |
||
| 340 | * |
||
| 341 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 342 | * if single parameter. |
||
| 343 | * @param string $types Types string (ex: "isdb"). |
||
| 344 | * @param array $params Parameters in the same order like types string. |
||
| 345 | * |
||
| 346 | * @return mixed Statement object or FALSE if an error occurred. |
||
| 347 | */ |
||
| 348 | public static function query($query, $types = "", $params = []) { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Execute update DB SQL queries using Prepared Statements. |
||
| 355 | * |
||
| 356 | * @param string $query SQL query template string or DBPreparedQuery object |
||
| 357 | * if single parameter. |
||
| 358 | * @param string $types Types string. |
||
| 359 | * @param array $params Parameters. |
||
| 360 | * |
||
| 361 | * @return int Returns the number of affected rows on success and |
||
| 362 | * -1 if the last query failed. |
||
| 363 | */ |
||
| 364 | public static function doUpdateQuery($query, $types = "", $params = []) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Execute select DB SQL queries using Prepared Statements. |
||
| 389 | * |
||
| 390 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 391 | * if single parameter. |
||
| 392 | * @param string $types Types string (ex: "isdb"). |
||
| 393 | * @param array $params Parameters in the same order like types string. |
||
| 394 | * |
||
| 395 | * @return mixed Statement object or FALSE if an error occurred. |
||
| 396 | */ |
||
| 397 | public static function doSelectQuery($query, $types = "", $params = []) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns list of database table fields. |
||
| 408 | * |
||
| 409 | * @param string $tableName Name of the table. |
||
| 410 | * @return array<string> List of the database table fields (syntax: array[fieldName] = fieldType) |
||
| 411 | */ |
||
| 412 | public static function getTableFieldsList($tableName) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns printable SQL field value for table fields list generator. |
||
| 445 | * |
||
| 446 | * @param string $type SQL type of the field. |
||
| 447 | * @param mixed $value Field value. |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | private static function getPrintableSQLValue($type, $value) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns printable field description string for table fields list generator. |
||
| 486 | * |
||
| 487 | * @param string $field Field name. |
||
| 488 | * @param array $attributes List of field attributes. |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public static function getPrintableFieldString($field, $attributes) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Outputs comfortable for Bean Class creation list of table fields. |
||
| 513 | * |
||
| 514 | * @param string $tableName Name of the Db table. |
||
| 515 | */ |
||
| 516 | public static function displayTableFieldsList($tableName) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns list of fields values with default indexes. |
||
| 531 | * |
||
| 532 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
| 533 | * @param string $idFieldName Name of the primary key field. |
||
| 534 | * @return array<mixed> |
||
| 535 | */ |
||
| 536 | private static function createValuesList($fieldsList, $idFieldName = "") { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Executes SQL INSERT query to the database. |
||
| 549 | * |
||
| 550 | * @param DBObject $dbObject DBObject to insert. |
||
| 551 | * @param bool $debug Debug mode flag. |
||
| 552 | * |
||
| 553 | * @return int Insertion ID (primary key value) or null on debug. |
||
| 554 | */ |
||
| 555 | public static function insertDBObject($dbObject, $debug = false) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Executes SQL UPDATE query to the database. |
||
| 583 | * |
||
| 584 | * @param DBObject $dbObject DBObject to update. |
||
| 585 | * @param bool $debug Debug mode flag. |
||
| 586 | * |
||
| 587 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 588 | * the last query failed. |
||
| 589 | */ |
||
| 590 | public static function updateDBObject($dbObject, $debug = false) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Executes SQL DELETE query to the database. |
||
| 616 | * |
||
| 617 | * @param DBObject $dbObject DBObject to delete. |
||
| 618 | * |
||
| 619 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 620 | * the last query failed. |
||
| 621 | */ |
||
| 622 | public static function deleteDBObject($dbObject) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Returns DBObject from ResultSet. |
||
| 641 | * |
||
| 642 | * @param DBObject $dbObject |
||
| 643 | * @param array $resultSet Associated by table names arrays of selected |
||
| 644 | * fields. |
||
| 645 | * |
||
| 646 | * @return DBObject |
||
| 647 | */ |
||
| 648 | public static function selectDBObjectFromResultSet($dbObject, $resultSet) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Returns DB object by database query statement. |
||
| 656 | * |
||
| 657 | * @param resource $stmt Database query statement. |
||
| 658 | * @param string $className Name of the DB object class. |
||
| 659 | * @return DBObject |
||
| 660 | */ |
||
| 661 | public static function selectDBObjectFromStatement($stmt, $className) { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Selects DBObject from database. |
||
| 685 | * |
||
| 686 | * @param string $query SQL query. |
||
| 687 | * @param string $types Types string (ex: "isdb"). |
||
| 688 | * @param array $params Parameters in the same order like types string. |
||
| 689 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
| 690 | * |
||
| 691 | * @return DBObject Selected DBObject or NULL otherwise. |
||
| 692 | */ |
||
| 693 | View Code Duplication | public static function selectDBObject($query, $types, $params, $instance) { |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Returns list of DB objects by database query statement. |
||
| 707 | * |
||
| 708 | * @param resource $stmt Database query statement. |
||
| 709 | * @param mixed $className Instance of the some DBObject class or it's class name. |
||
| 710 | * |
||
| 711 | * @return array<DBObject> |
||
| 712 | */ |
||
| 713 | public static function selectDBObjectsFromStatement($stmt, $className) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Selects DBObject list from database. |
||
| 740 | * |
||
| 741 | * @param string $query SQL query. |
||
| 742 | * @param string $types Types string (ex: "isdb"). |
||
| 743 | * @param array $params Parameters in the same order like types string. |
||
| 744 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
| 745 | * |
||
| 746 | * @return DBObject Selected DBObject or NULL otherwise. |
||
| 747 | */ |
||
| 748 | View Code Duplication | public static function selectDBObjects($query, $types, $params, $instance) { |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Executes SQL query with single record and return this record. |
||
| 762 | * |
||
| 763 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 764 | * if single parameter. |
||
| 765 | * @param string $types Types string (ex: "isdb"). |
||
| 766 | * @param array $params Parameters in the same order like types string. |
||
| 767 | * |
||
| 768 | * @return array Selected record with table names as keys or NULL if no |
||
| 769 | * data selected. |
||
| 770 | * @throws DBCoreException If no one or more than one records selected. |
||
| 771 | */ |
||
| 772 | View Code Duplication | public static function selectSingleRecord($query, $types = "", $params = []) { |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Executes SQL query with single record and value result and return this value. |
||
| 799 | * |
||
| 800 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
| 801 | * if single parameter. |
||
| 802 | * @param string $types Types string (ex: "isdb"). |
||
| 803 | * @param array $params Parameters in the same order like types string. |
||
| 804 | * |
||
| 805 | * @return mixed |
||
| 806 | * @throws DBCoreException If no one or more than one records selected. |
||
| 807 | */ |
||
| 808 | View Code Duplication | public static function selectSingleValue($query, $types = "", $params = []) { |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Calls DBCore magic methods like: |
||
| 836 | * get[User]By[Id]($userId) |
||
| 837 | * get[User]By[Email]($email) |
||
| 838 | * get[Users]() |
||
| 839 | * delete[Users]($ids) |
||
| 840 | * delete[User]($userId) |
||
| 841 | * set[User]Activation($activationFieldName, $flagValue). |
||
| 842 | * |
||
| 843 | * @param string $methodName Name of the magic method. |
||
| 844 | * @param array $methodParams List of dynamic parameters. |
||
| 845 | * |
||
| 846 | * @return mixed |
||
| 847 | * @throws DBCoreException |
||
| 848 | */ |
||
| 849 | public static function __callStatic($methodName, $methodParams) { |
||
| 959 | |||
| 960 | } |
||
| 961 | |||
| 966 |
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.