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 | */ |
||
273 | public static function bindResults($stmt) { |
||
300 | |||
301 | /** |
||
302 | * Execute DB SQL queries using Prepared Statements. |
||
303 | * |
||
304 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
305 | * if single parameter. |
||
306 | * @param string $types Types string (ex: "isdb"). |
||
307 | * @param array $params Parameters in the same order like types string. |
||
308 | * |
||
309 | * @return mixed Statement object or FALSE if an error occurred. |
||
310 | */ |
||
311 | private static function doQuery($query, $types = "", $params = []) { |
||
336 | |||
337 | /** |
||
338 | * Execute DB SQL queries using Prepared Statements. |
||
339 | * |
||
340 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
341 | * if single parameter. |
||
342 | * @param string $types Types string (ex: "isdb"). |
||
343 | * @param array $params Parameters in the same order like types string. |
||
344 | * |
||
345 | * @return mixed Statement object or FALSE if an error occurred. |
||
346 | */ |
||
347 | public static function query($query, $types = "", $params = []) { |
||
350 | |||
351 | /** |
||
352 | * Execute update DB SQL queries using Prepared Statements. |
||
353 | * |
||
354 | * @param string $query SQL query template string or DBPreparedQuery object |
||
355 | * if single parameter. |
||
356 | * @param string $types Types string. |
||
357 | * @param array $params Parameters. |
||
358 | * |
||
359 | * @return int Returns the number of affected rows on success and |
||
360 | * -1 if the last query failed. |
||
361 | */ |
||
362 | public static function doUpdateQuery($query, $types = "", $params = []) { |
||
384 | |||
385 | /** |
||
386 | * Execute select DB SQL queries using Prepared Statements. |
||
387 | * |
||
388 | * @param mixed $query SQL query template string or DBPreparedQuery object |
||
389 | * if single parameter. |
||
390 | * @param string $types Types string (ex: "isdb"). |
||
391 | * @param array $params Parameters in the same order like types string. |
||
392 | * |
||
393 | * @return mixed Statement object or FALSE if an error occurred. |
||
394 | */ |
||
395 | public static function doSelectQuery($query, $types = "", $params = []) { |
||
403 | |||
404 | /** |
||
405 | * Returns list of database table fields. |
||
406 | * |
||
407 | * @param string $tableName Name of the table. |
||
408 | * @return array<string> List of the database table fields (syntax: array[fieldName] = fieldType) |
||
409 | */ |
||
410 | public static function getTableFieldsList($tableName) { |
||
440 | |||
441 | /** |
||
442 | * Returns printable SQL field value for table fields list generator. |
||
443 | * |
||
444 | * @param string $type SQL type of the field. |
||
445 | * @param mixed $value Field value. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | private static function getPrintableSQLValue($type, $value) { |
||
481 | |||
482 | /** |
||
483 | * Returns printable field description string for table fields list generator. |
||
484 | * |
||
485 | * @param string $field Field name. |
||
486 | * @param array $attributes List of field attributes. |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public static function getPrintableFieldString($field, $attributes) { |
||
508 | |||
509 | /** |
||
510 | * Outputs comfortable for Bean Class creation list of table fields. |
||
511 | * |
||
512 | * @param string $tableName Name of the Db table. |
||
513 | */ |
||
514 | public static function displayTableFieldsList($tableName) { |
||
526 | |||
527 | /** |
||
528 | * Returns list of fields values with default indexes. |
||
529 | * |
||
530 | * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) |
||
531 | * @param string $idFieldName Name of the primary key field. |
||
532 | * @return array<mixed> |
||
533 | */ |
||
534 | public static function createValuesList($fieldsList, $idFieldName = "") { |
||
544 | |||
545 | /** |
||
546 | * Executes SQL INSERT query to the database. |
||
547 | * |
||
548 | * @param DBObject $dbObject DBObject to insert. |
||
549 | * @param bool $ignore Ignore unique indexes or not. |
||
550 | * @param bool $debug Debug mode flag. |
||
551 | * |
||
552 | * @return int Insertion ID (primary key value) or null on debug. |
||
553 | */ |
||
554 | public static function insertDBObject($dbObject, $ignore = false, $debug = false) { |
||
579 | |||
580 | /** |
||
581 | * Executes SQL UPDATE query to the database. |
||
582 | * |
||
583 | * @param DBObject $dbObject DBObject to update. |
||
584 | * @param bool $debug Debug mode flag. |
||
585 | * |
||
586 | * @return int Returns the number of affected rows on success, and -1 if |
||
587 | * the last query failed. |
||
588 | */ |
||
589 | public static function updateDBObject($dbObject, $debug = false) { |
||
612 | |||
613 | /** |
||
614 | * Executes SQL DELETE query to the database. |
||
615 | * |
||
616 | * @param DBObject $dbObject DBObject to delete. |
||
617 | * |
||
618 | * @return int Returns the number of affected rows on success, and -1 if |
||
619 | * the last query failed. |
||
620 | */ |
||
621 | 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 | 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 | public static function selectSingleValue($query, $types = "", $params = []) { |
||
834 | |||
835 | } |
||
836 | |||
841 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.