Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php namespace EvolutionCMS; |
||
6 | class Database implements Interfaces\DatabaseInterface |
||
|
|||
7 | { |
||
8 | /** |
||
9 | * @var mysqli |
||
10 | */ |
||
11 | public $conn; |
||
12 | public $config; |
||
13 | public $lastQuery; |
||
14 | public $isConnected; |
||
15 | public $_dbconnectionmethod; |
||
16 | |||
17 | /** |
||
18 | * DBAPI constructor. |
||
19 | * |
||
20 | * @param string $host |
||
21 | * @param string $dbase |
||
22 | * @param string $uid |
||
23 | * @param string $pwd |
||
24 | * @param null|string $pre |
||
25 | * @param string $charset |
||
26 | * @param string $connection_method |
||
27 | */ |
||
28 | public function __construct( |
||
45 | |||
46 | /** |
||
47 | * @param null $key |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function getConfig($key = null) |
||
54 | |||
55 | /** |
||
56 | * @param string $host |
||
57 | * @param string $dbase |
||
58 | * @param string $uid |
||
59 | * @param string $pwd |
||
60 | * @return mysqli |
||
61 | */ |
||
62 | public function connect($host = '', $dbase = '', $uid = '', $pwd = '') |
||
112 | |||
113 | /** |
||
114 | * @return void |
||
115 | */ |
||
116 | public function disconnect() |
||
122 | |||
123 | /** |
||
124 | * @param array|string $s |
||
125 | * @param int $safeCount |
||
126 | * @return array|string |
||
127 | */ |
||
128 | public function escape($s, $safeCount = 0) |
||
151 | |||
152 | /** |
||
153 | * @param string|array|mysqli_result $sql |
||
154 | * @param bool $watchError |
||
155 | * @return bool|mysqli_result |
||
156 | */ |
||
157 | public function query($sql, $watchError = true) |
||
220 | |||
221 | /** |
||
222 | * @param string $from |
||
223 | * @param string $where |
||
224 | * @param string $orderBy |
||
225 | * @param string $limit |
||
226 | * @return bool|mysqli_result |
||
227 | */ |
||
228 | public function delete($from, $where = '', $orderBy = '', $limit = '') |
||
253 | |||
254 | /** |
||
255 | * @param string|array $fields |
||
256 | * @param string|array $from |
||
257 | * @param string|array $where |
||
258 | * @param string $orderBy |
||
259 | * @param string $limit |
||
260 | * @return bool|mysqli_result |
||
261 | */ |
||
262 | public function select($fields = "*", $from = "", $where = "", $orderBy = "", $limit = "") |
||
298 | |||
299 | /** |
||
300 | * @param array|string $fields |
||
301 | * @param $table |
||
302 | * @param string $where |
||
303 | * @return bool|mysqli_result |
||
304 | */ |
||
305 | public function update($fields, $table, $where = "") |
||
333 | |||
334 | /** |
||
335 | * @param string|array $fields |
||
336 | * @param string $intotable |
||
337 | * @param string $fromfields |
||
338 | * @param string $fromtable |
||
339 | * @param string $where |
||
340 | * @param string $limit |
||
341 | * @return mixed |
||
342 | */ |
||
343 | public function insert($fields, $intotable, $fromfields = "*", $fromtable = "", $where = "", $limit = "") |
||
380 | |||
381 | /** |
||
382 | * @param $fields |
||
383 | * @param $table |
||
384 | * @param string $where |
||
385 | * @return bool|mixed|mysqli_result |
||
386 | */ |
||
387 | public function save($fields, $table, $where = '') |
||
400 | |||
401 | /** |
||
402 | * @param mixed $rs |
||
403 | * @return bool |
||
404 | */ |
||
405 | public function isResult($rs) |
||
409 | |||
410 | /** |
||
411 | * @param mysqli_result $rs |
||
412 | */ |
||
413 | public function freeResult($rs) |
||
417 | |||
418 | /** |
||
419 | * @param mysqli_result $rs |
||
420 | * @return mixed |
||
421 | */ |
||
422 | public function numFields($rs) |
||
426 | |||
427 | /** |
||
428 | * @param mysqli_result $rs |
||
429 | * @param int $col |
||
430 | * @return string|null |
||
431 | */ |
||
432 | public function fieldName($rs, $col = 0) |
||
438 | |||
439 | /** |
||
440 | * @param $name |
||
441 | */ |
||
442 | public function selectDb($name) |
||
446 | |||
447 | |||
448 | /** |
||
449 | * @param null|mysqli $conn |
||
450 | * @return mixed |
||
451 | */ |
||
452 | public function getInsertId($conn = null) |
||
460 | |||
461 | /** |
||
462 | * @param null|mysqli $conn |
||
463 | * @return int |
||
464 | */ |
||
465 | public function getAffectedRows($conn = null) |
||
473 | |||
474 | /** |
||
475 | * @param null|mysqli $conn |
||
476 | * @return string |
||
477 | */ |
||
478 | public function getLastError($conn = null) |
||
486 | |||
487 | /** |
||
488 | * @param mysqli_result $ds |
||
489 | * @return int |
||
490 | */ |
||
491 | public function getRecordCount($ds) |
||
495 | |||
496 | /** |
||
497 | * @param mysqli_result $ds |
||
498 | * @param string $mode |
||
499 | * @return array|bool|mixed|object|stdClass |
||
500 | */ |
||
501 | public function getRow($ds, $mode = 'assoc') |
||
526 | |||
527 | /** |
||
528 | * @param $name |
||
529 | * @param mysqli_result|string $dsq |
||
530 | * @return array |
||
531 | */ |
||
532 | public function getColumn($name, $dsq) |
||
546 | |||
547 | /** |
||
548 | * @param mysqli_result|string $dsq |
||
549 | * @return array |
||
550 | */ |
||
551 | public function getColumnNames($dsq) |
||
566 | |||
567 | /** |
||
568 | * @param mysqli_result|string $dsq |
||
569 | * @return bool|string|int |
||
570 | */ |
||
571 | public function getValue($dsq) |
||
584 | |||
585 | /** |
||
586 | * @param string $table |
||
587 | * @return array |
||
588 | */ |
||
589 | public function getTableMetaData($table) |
||
604 | |||
605 | /** |
||
606 | * @param int $timestamp |
||
607 | * @param string $fieldType |
||
608 | * @return false|string |
||
609 | */ |
||
610 | public function prepareDate($timestamp, $fieldType = 'DATETIME') |
||
632 | |||
633 | /** |
||
634 | * @param string|mysqli_result $rs |
||
635 | * @param bool $index |
||
636 | * @return array |
||
637 | */ |
||
638 | public function makeArray($rs = '', $index = false) |
||
653 | |||
654 | /** |
||
655 | * @return string |
||
656 | */ |
||
657 | public function getVersion() |
||
661 | |||
662 | public function getTableName($table, $escape = true) |
||
667 | |||
668 | /** |
||
669 | * @param $tbl |
||
670 | * @return string |
||
671 | */ |
||
672 | public function getFullTableName($tbl) |
||
676 | |||
677 | /** |
||
678 | * @param string $tableName |
||
679 | * @param bool $force |
||
680 | * @return string |
||
681 | */ |
||
682 | public function replaceFullTableName($tableName, $force = false) |
||
697 | |||
698 | /** |
||
699 | * @param string $table_name |
||
700 | * @return bool|mysqli_result |
||
701 | */ |
||
702 | public function optimize($table_name) |
||
711 | |||
712 | /** |
||
713 | * @param string $table_name |
||
714 | * @return bool|mysqli_result |
||
715 | */ |
||
716 | public function truncate($table_name) |
||
720 | |||
721 | /** |
||
722 | * @param mysqli_result $result |
||
723 | * @param int $row_number |
||
724 | * @return bool |
||
725 | */ |
||
726 | public function dataSeek($result, $row_number) |
||
730 | |||
731 | /** |
||
732 | * @param array $fields |
||
733 | * @return string |
||
734 | */ |
||
735 | public function _getFieldsStringFromArray($fields = array()) |
||
753 | |||
754 | /** |
||
755 | * @param array $tables |
||
756 | * @return string |
||
757 | */ |
||
758 | public function _getFromStringFromArray($tables = array()) |
||
767 | } |
||
768 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.