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 |
||
19 | class DBCore |
||
20 | { |
||
21 | /** |
||
22 | * An array containing all the opened connections. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $connections = []; |
||
27 | |||
28 | /** |
||
29 | * The incremented index of connections. |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $index = 0; |
||
34 | |||
35 | /** |
||
36 | * Current connection index. |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $currIndex = 0; |
||
41 | |||
42 | /** |
||
43 | * Instance of a class. |
||
44 | * |
||
45 | * @var DBCore |
||
46 | */ |
||
47 | protected static $instance; |
||
48 | |||
49 | /** |
||
50 | * Returns an instance of this class. |
||
51 | * |
||
52 | * @return DBCore |
||
53 | */ |
||
54 | public static function getInstance() |
||
62 | |||
63 | /** |
||
64 | * Reset the internal static instance. |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public static function resetInstance() |
||
75 | |||
76 | /** |
||
77 | * Reset this instance of the manager. |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function reset() |
||
90 | |||
91 | /** |
||
92 | * Seves a new connection to DBCore->connections. |
||
93 | * |
||
94 | * @param mysqli Object $connResource An object which represents the connection to a MySQL Server. |
||
95 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
96 | * |
||
97 | * @throws DBCoreException If trying to save a connection with an existing name. |
||
98 | */ |
||
99 | public static function connection($connResource = null, $connName = null) |
||
106 | |||
107 | /** |
||
108 | * Seves a new connection to DBCore->connections. |
||
109 | * |
||
110 | * @param mysqli Object $connResource An object which represents the connection to a MySQL Server. |
||
111 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
112 | * |
||
113 | * @throws DBCoreException If trying to save a connection with an existing name. |
||
114 | */ |
||
115 | public function openConnection($connResource, $connName = null) |
||
129 | |||
130 | /** |
||
131 | * Get the connection instance for the passed name. |
||
132 | * |
||
133 | * @param string $connName Name of the connection, if empty numeric key is used. |
||
134 | * |
||
135 | * @throws DBCoreException If trying to get a non-existent connection. |
||
136 | * |
||
137 | * @return mysqli Object |
||
138 | */ |
||
139 | public function getConnection($connName) |
||
147 | |||
148 | /** |
||
149 | * Get the name of the passed connection instance. |
||
150 | * |
||
151 | * @param mysqli Object $connResource Connection object to be searched for. |
||
152 | * |
||
153 | * @return string The name of the connection. |
||
154 | */ |
||
155 | public function getConnectionName($connResource) |
||
159 | |||
160 | /** |
||
161 | * Closes the specified connection. |
||
162 | * |
||
163 | * @param mixed $connection Connection object or its name. |
||
164 | */ |
||
165 | public function closeConnection($connection) |
||
186 | |||
187 | /** |
||
188 | * Returns all opened connections. |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | public function getConnections() |
||
196 | |||
197 | /** |
||
198 | * Sets the current connection to $key. |
||
199 | * |
||
200 | * @param mixed $key The connection key |
||
201 | * |
||
202 | * @throws DBCoreException |
||
203 | */ |
||
204 | public function setCurrentConnection($key) |
||
211 | |||
212 | /** |
||
213 | * Whether or not the DBCore contains specified connection. |
||
214 | * |
||
215 | * @param mixed $key The connection key |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function contains($key) |
||
223 | |||
224 | /** |
||
225 | * Returns the number of opened connections. |
||
226 | * |
||
227 | * @return int |
||
228 | */ |
||
229 | public function count() |
||
233 | |||
234 | /** |
||
235 | * Returns an ArrayIterator that iterates through all connections. |
||
236 | * |
||
237 | * @return ArrayIterator |
||
238 | */ |
||
239 | public function getIterator() |
||
243 | |||
244 | /** |
||
245 | * Get the current connection instance. |
||
246 | * |
||
247 | * @throws DBCoreException If there are no open connections |
||
248 | * |
||
249 | * @return mysqli Object |
||
250 | */ |
||
251 | public function getCurrentConnection() |
||
260 | |||
261 | /** |
||
262 | * Check database errors. |
||
263 | * |
||
264 | * @param object $dbObj |
||
265 | */ |
||
266 | private static function checkDbError($dbObj) |
||
272 | |||
273 | /** |
||
274 | * Bind parameters to the statment with dynamic number of parameters. |
||
275 | * |
||
276 | * @param resource $stmt Statement. |
||
277 | * @param string $types Types string. |
||
278 | * @param array $params Parameters. |
||
279 | */ |
||
280 | private static function bindParameters($stmt, $types, $params) |
||
290 | |||
291 | /** |
||
292 | * Return parameters from the statment with dynamic number of parameters. |
||
293 | * |
||
294 | * @param resource $stmt Statement. |
||
295 | * @param array $params Parameters. |
||
|
|||
296 | */ |
||
297 | public static function bindResults($stmt) |
||
323 | |||
324 | /** |
||
325 | * Execute DB SQL queries using Prepared Statements. |
||
326 | * |
||
327 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
328 | * if single parameter. |
||
329 | * @param string $types Types string (ex: "isdb"). |
||
330 | * @param array $params Parameters in the same order like types string. |
||
331 | * |
||
332 | * @return mixed Statement object or FALSE if an error occurred. |
||
333 | */ |
||
334 | private static function doQuery($query, $types = '', $params = []) |
||
358 | |||
359 | /** |
||
360 | * Execute update DB SQL queries using Prepared Statements. |
||
361 | * |
||
362 | * @param string $query SQL query template string or DBPreparedQuery object |
||
363 | * if single parameter. |
||
364 | * @param string $types Types string. |
||
365 | * @param array $params Parameters. |
||
366 | * |
||
367 | * @return int Returns the number of affected rows on success, and -1 if the last query failed. |
||
368 | */ |
||
369 | public static function doUpdateQuery($query, $types = '', $params = []) |
||
392 | |||
393 | /** |
||
394 | * Execute select DB SQL queries using Prepared Statements. |
||
395 | * |
||
396 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
397 | * if single parameter. |
||
398 | * @param string $types Types string (ex: "isdb"). |
||
399 | * @param array $params Parameters in the same order like types string. |
||
400 | * |
||
401 | * @return mixed Statement object or FALSE if an error occurred. |
||
402 | */ |
||
403 | public static function doSelectQuery($query, $types = '', $params = []) |
||
412 | |||
413 | /** |
||
414 | * Returns list of database table fields. |
||
415 | * |
||
416 | * @param string $tableName Name of the table. |
||
417 | * |
||
418 | * @return array<string> List of the database table fields (syntax: array[fieldName] = fieldType) |
||
419 | */ |
||
420 | public static function getTableFieldsList($tableName) |
||
451 | |||
452 | /** |
||
453 | * Returns printable SQL field value for table fields list generator. |
||
454 | * |
||
455 | * @param string $type SQL type of the field. |
||
456 | * @param mixed $value Field value. |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | private static function getPrintableSQLValue($type, $value) |
||
493 | |||
494 | /** |
||
495 | * Returns printable field description string for table fields list generator. |
||
496 | * |
||
497 | * @param string $field Field name. |
||
498 | * @param array $attributes List of field attributes. |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | public static function getPrintableFieldString($field, $attributes) |
||
521 | |||
522 | /** |
||
523 | * Outputs comfortable for Bean Class creation list of table fields. |
||
524 | * |
||
525 | * @param string $tableName Name of the Db table. |
||
526 | */ |
||
527 | public static function displayTableFieldsList($tableName) |
||
540 | |||
541 | /** |
||
542 | * Returns list of fields values with default indexes. |
||
543 | * |
||
544 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
545 | * @param string $idFieldName Name of the primary key field. |
||
546 | * |
||
547 | * @return array<mixed> |
||
548 | */ |
||
549 | private static function createValuesList($fieldsList, $idFieldName = '') |
||
560 | |||
561 | /** |
||
562 | * Executes SQL INSERT query to the database. |
||
563 | * |
||
564 | * @param DBObject $dbObject DBObject to insert. |
||
565 | * @param bool $debug Debug mode flag. |
||
566 | * |
||
567 | * @return int Insertion ID (primary key value) or null on debug. |
||
568 | */ |
||
569 | public static function insertDBObject($dbObject, $debug = false) |
||
595 | |||
596 | /** |
||
597 | * Executes SQL UPDATE query to the database. |
||
598 | * |
||
599 | * @param DBObject $dbObject DBObject to update. |
||
600 | * @param bool $debug Debug mode flag. |
||
601 | * |
||
602 | * @return int Returns the number of affected rows on success, and -1 if |
||
603 | * the last query failed. |
||
604 | */ |
||
605 | public static function updateDBObject($dbObject, $debug = false) |
||
629 | |||
630 | /** |
||
631 | * Executes SQL DELETE query to the database. |
||
632 | * |
||
633 | * @param DBObject $dbObject DBObject to delete. |
||
634 | * |
||
635 | * @return int Returns the number of affected rows on success, and -1 if |
||
636 | * the last query failed. |
||
637 | */ |
||
638 | public static function deleteDBObject($dbObject) |
||
655 | |||
656 | /** |
||
657 | * Returns DBObject from ResultSet. |
||
658 | * |
||
659 | * @param DBObject $dbObject |
||
660 | * @param array $resultSet Associated by table names arrays of selected |
||
661 | * fields. |
||
662 | * |
||
663 | * @return DBObject |
||
664 | */ |
||
665 | public static function selectDBObjectFromResultSet($dbObject, $resultSet) |
||
671 | |||
672 | /** |
||
673 | * Returns DB object by database query statement. |
||
674 | * |
||
675 | * @param resource $stmt Database query statement. |
||
676 | * @param string $className Name of the DB object class. |
||
677 | * |
||
678 | * @return DBObject |
||
679 | */ |
||
680 | public static function selectDBObjectFromStatement($stmt, $className) |
||
700 | |||
701 | /** |
||
702 | * Selects DBObject from database. |
||
703 | * |
||
704 | * @param string $query SQL query. |
||
705 | * @param string $types Types string (ex: "isdb"). |
||
706 | * @param array $params Parameters in the same order like types string. |
||
707 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
708 | * |
||
709 | * @return DBObject Selected DBObject or NULL otherwise. |
||
710 | */ |
||
711 | View Code Duplication | public static function selectDBObject($query, $types, $params, $instance) |
|
723 | |||
724 | /** |
||
725 | * Returns list of DB objects by database query statement. |
||
726 | * |
||
727 | * @param resource $stmt Database query statement. |
||
728 | * @param mixed $className Instance of the some DBObject class or it's class name. |
||
729 | * |
||
730 | * @return array<DBObject> |
||
731 | */ |
||
732 | public static function selectDBObjectsFromStatement($stmt, $className) |
||
757 | |||
758 | /** |
||
759 | * Selects DBObject list from database. |
||
760 | * |
||
761 | * @param string $query SQL query. |
||
762 | * @param string $types Types string (ex: "isdb"). |
||
763 | * @param array $params Parameters in the same order like types string. |
||
764 | * @param mixed $instance Instance of the some DBObject class or it's class name. |
||
765 | * |
||
766 | * @return DBObject Selected DBObject or NULL otherwise. |
||
767 | */ |
||
768 | View Code Duplication | public static function selectDBObjects($query, $types, $params, $instance) |
|
780 | |||
781 | /** |
||
782 | * Executes SQL query with single record and return this record. |
||
783 | * |
||
784 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
785 | * if single parameter. |
||
786 | * @param string $types Types string (ex: "isdb"). |
||
787 | * @param array $params Parameters in the same order like types string. |
||
788 | * |
||
789 | * @throws DBCoreException If no one or more than one records selected. |
||
790 | * |
||
791 | * @return array Selected record with table names as keys or NULL if no |
||
792 | * data selected. |
||
793 | */ |
||
794 | View Code Duplication | public static function selectSingleRecord($query, $types = '', $params = []) |
|
817 | |||
818 | /** |
||
819 | * Executes SQL query with single record and value result and return this value. |
||
820 | * |
||
821 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
822 | * if single parameter. |
||
823 | * @param string $types Types string (ex: "isdb"). |
||
824 | * @param array $params Parameters in the same order like types string. |
||
825 | * |
||
826 | * @throws DBCoreException If no one or more than one records selected. |
||
827 | * |
||
828 | * @return mixed |
||
829 | */ |
||
830 | View Code Duplication | public static function selectSingleValue($query, $types = '', $params = []) |
|
854 | |||
855 | /** |
||
856 | * Calls DBCore magic methods like: |
||
857 | * get[User]By[Id]($userId) |
||
858 | * get[User]By[Email]($email) |
||
859 | * get[Users]() |
||
860 | * delete[Users]($ids) |
||
861 | * delete[User]($userId) |
||
862 | * set[User]Activation($activationFieldName, $flagValue). |
||
863 | * |
||
864 | * @param string $methodName Name of the magic method. |
||
865 | * @param array $methodParams List of dynamic parameters. |
||
866 | * |
||
867 | * @throws DBCoreException |
||
868 | * |
||
869 | * @return mixed |
||
870 | */ |
||
871 | public static function __callStatic($methodName, $methodParams) |
||
982 | } |
||
983 | |||
990 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.