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 Tables 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 Tables, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Tables |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var Connection |
||
| 41 | */ |
||
| 42 | protected $db; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $databaseName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array Tables |
||
| 51 | */ |
||
| 52 | protected $tables; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Work queue |
||
| 56 | */ |
||
| 57 | protected $queue; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string last error message |
||
| 61 | */ |
||
| 62 | protected $lastError; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int last error number |
||
| 66 | */ |
||
| 67 | protected $lastErrNo; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor |
||
| 71 | * |
||
| 72 | */ |
||
| 73 | 1 | public function __construct() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Return a table name, prefixed with site table prefix |
||
| 84 | * |
||
| 85 | * @param string $table table name to contain prefix |
||
| 86 | * |
||
| 87 | * @return string table name with prefix |
||
| 88 | */ |
||
| 89 | 1 | protected function name($table) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Add new column for table to the work queue |
||
| 96 | * |
||
| 97 | * @param string $table table to contain the column |
||
| 98 | * @param string $column name of column to add |
||
| 99 | * @param string $attributes column_definition |
||
| 100 | * |
||
| 101 | * @return bool true if no errors, false if errors encountered |
||
| 102 | */ |
||
| 103 | 1 | public function addColumn($table, $column, $attributes) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Add new primary key definition for table to work queue |
||
| 135 | * |
||
| 136 | * @param string $table table |
||
| 137 | * @param string $column column or comma separated list of columns |
||
| 138 | * to use as primary key |
||
| 139 | * |
||
| 140 | * @return bool true if no errors, false if errors encountered |
||
| 141 | */ |
||
| 142 | 1 | public function addPrimaryKey($table, $column) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Add new index definition for index to work queue |
||
| 166 | * |
||
| 167 | * @param string $name name of index to add |
||
| 168 | * @param string $table table indexed |
||
| 169 | * @param string $column column or a comma separated list of columns |
||
| 170 | * to use as the key |
||
| 171 | * @param bool $unique true if index is to be unique |
||
| 172 | * |
||
| 173 | * @return bool true if no errors, false if errors encountered |
||
| 174 | */ |
||
| 175 | 1 | public function addIndex($name, $table, $column, $unique = false) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Load table schema from database, or starts new empty schema if |
||
| 201 | * table does not exist |
||
| 202 | * |
||
| 203 | * @param string $table table |
||
| 204 | * |
||
| 205 | * @return bool true if no errors, false if errors encountered |
||
| 206 | */ |
||
| 207 | public function addTable($table) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * AddTable only if it exists |
||
| 239 | * |
||
| 240 | * @param string $table table |
||
| 241 | * |
||
| 242 | * @return bool true if table exists, false otherwise |
||
| 243 | */ |
||
| 244 | 1 | public function useTable($table) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get column attributes |
||
| 259 | * |
||
| 260 | * @param string $table table containing the column |
||
| 261 | * @param string $column column to alter |
||
| 262 | * |
||
| 263 | * @return string|bool attribute string, or false if error encountered |
||
| 264 | */ |
||
| 265 | 1 | public function getColumnAttributes($table, $column) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get indexes for a table |
||
| 283 | * |
||
| 284 | * @param string $table get indexes for this named table |
||
| 285 | * |
||
| 286 | * @return array|bool array of indexes, or false if error encountered |
||
| 287 | */ |
||
| 288 | 1 | public function getTableIndexes($table) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Add alter column operation to the work queue |
||
| 300 | * |
||
| 301 | * @param string $table table containing the column |
||
| 302 | * @param string $column column to alter |
||
| 303 | * @param string $attributes new column_definition |
||
| 304 | * @param string $newName new name for column, blank to keep same |
||
| 305 | * |
||
| 306 | * @return bool true if no errors, false if errors encountered |
||
| 307 | */ |
||
| 308 | 1 | public function alterColumn($table, $column, $attributes, $newName = '') |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Loads table schema from database, and adds newTable with that |
||
| 349 | * schema to the queue |
||
| 350 | * |
||
| 351 | * @param string $table existing table |
||
| 352 | * @param string $newTable new table |
||
| 353 | * @param bool $withData true to copy data, false for schema only |
||
| 354 | * |
||
| 355 | * @return bool true if no errors, false if errors encountered |
||
| 356 | */ |
||
| 357 | 1 | public function copyTable($table, $newTable, $withData = false) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Add drop column operation to the work queue |
||
| 385 | * |
||
| 386 | * @param string $table table containing the column |
||
| 387 | * @param string $column column to drop |
||
| 388 | * |
||
| 389 | * @return bool true if no errors, false if errors encountered |
||
| 390 | */ |
||
| 391 | 1 | View Code Duplication | public function dropColumn($table, $column) |
| 403 | |||
| 404 | /** |
||
| 405 | * Add drop index operation to the work queue |
||
| 406 | * |
||
| 407 | * @param string $name name of index to drop |
||
| 408 | * @param string $table table indexed |
||
| 409 | * |
||
| 410 | * @return bool true if no errors, false if errors encountered |
||
| 411 | */ |
||
| 412 | 1 | View Code Duplication | public function dropIndex($name, $table) |
| 423 | |||
| 424 | /** |
||
| 425 | * Add drop for all (non-PRIMARY) keys for a table to the work |
||
| 426 | * queue. This can be used to clean up indexes with automatic names. |
||
| 427 | * |
||
| 428 | * @param string $table table indexed |
||
| 429 | * |
||
| 430 | * @return bool true if no errors, false if errors encountered |
||
| 431 | */ |
||
| 432 | public function dropIndexes($table) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Add drop of PRIMARY key for a table to the work queue |
||
| 462 | * |
||
| 463 | * @param string $table table |
||
| 464 | * |
||
| 465 | * @return bool true if no errors, false if errors encountered |
||
| 466 | */ |
||
| 467 | 1 | View Code Duplication | public function dropPrimaryKey($table) |
| 478 | |||
| 479 | /** |
||
| 480 | * Add drop of table to the work queue |
||
| 481 | * |
||
| 482 | * @param string $table table |
||
| 483 | * |
||
| 484 | * @return bool true if no errors, false if errors encountered |
||
| 485 | */ |
||
| 486 | 1 | View Code Duplication | public function dropTable($table) |
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Add rename table operation to the work queue |
||
| 500 | * |
||
| 501 | * @param string $table table |
||
| 502 | * @param string $newName new table name |
||
| 503 | * |
||
| 504 | * @return bool true if no errors, false if errors encountered |
||
| 505 | */ |
||
| 506 | 1 | public function renameTable($table, $newName) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Add alter table table_options (ENGINE, DEFAULT CHARSET, etc.) |
||
| 524 | * to work queue |
||
| 525 | * |
||
| 526 | * @param string $table table |
||
| 527 | * @param string $options table_options |
||
| 528 | * |
||
| 529 | * @return bool true if no errors, false if errors encountered |
||
| 530 | */ |
||
| 531 | 1 | public function setTableOptions($table, $options) |
|
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * Clear the work queue |
||
| 553 | * |
||
| 554 | * @return void |
||
| 555 | */ |
||
| 556 | 2 | public function resetQueue() |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Executes the work queue |
||
| 564 | * |
||
| 565 | * @param bool $force true to force updates even if this is a 'GET' request |
||
| 566 | * |
||
| 567 | * @return bool true if no errors, false if errors encountered |
||
| 568 | */ |
||
| 569 | public function executeQueue($force = false) |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * Create a DELETE statement and add it to the work queue |
||
| 593 | * |
||
| 594 | * @param string $table table |
||
| 595 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 596 | * |
||
| 597 | * @return bool true if no errors, false if errors encountered |
||
| 598 | */ |
||
| 599 | public function delete($table, $criteria) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Create an INSERT SQL statement and add it to the work queue. |
||
| 619 | * |
||
| 620 | * @param string $table table |
||
| 621 | * @param array $columns array of 'column'=>'value' entries |
||
| 622 | * @param boolean $quoteValue true to quote values, false if caller handles quoting |
||
| 623 | * |
||
| 624 | * @return boolean true if no errors, false if errors encountered |
||
| 625 | */ |
||
| 626 | public function insert($table, $columns, $quoteValue = true) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Create an UPDATE SQL statement and add it to the work queue |
||
| 651 | * |
||
| 652 | * @param string $table table |
||
| 653 | * @param array $columns array of 'column'=>'value' entries |
||
| 654 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 655 | * @param boolean $quoteValue true to quote values, false if caller handles quoting |
||
| 656 | * |
||
| 657 | * @return boolean true if no errors, false if errors encountered |
||
| 658 | */ |
||
| 659 | public function update($table, $columns, $criteria, $quoteValue = true) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Add statement to remove all rows from a table to the work queue |
||
| 688 | * |
||
| 689 | * @param string $table table |
||
| 690 | * |
||
| 691 | * @return bool true if no errors, false if errors encountered |
||
| 692 | */ |
||
| 693 | View Code Duplication | public function truncate($table) |
|
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | /** |
||
| 708 | * return SQL to create the table |
||
| 709 | * |
||
| 710 | * This method does NOT modify the work queue |
||
| 711 | * |
||
| 712 | * @param string $table table |
||
| 713 | * @param bool $prefixed true to return with table name prefixed |
||
| 714 | * |
||
| 715 | * @return string|false string SQL to create table, or false if errors encountered |
||
| 716 | */ |
||
| 717 | protected function renderTableCreate($table, $prefixed = false) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * execute an SQL statement |
||
| 748 | * |
||
| 749 | * @param string $sql SQL statement to execute |
||
| 750 | * @param bool $force true to use force updates even in safe requests |
||
| 751 | * |
||
| 752 | * @return mixed result Statement, or false on error |
||
| 753 | * Any error message is in $this->lastError; |
||
| 754 | */ |
||
| 755 | protected function execSql($sql, $force = false) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * fetch the next row of a result set |
||
| 772 | * |
||
| 773 | * @param Statement $result as returned by query |
||
| 774 | * |
||
| 775 | * @return mixed false on error |
||
| 776 | */ |
||
| 777 | protected function fetch($result) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * get table definition from INFORMATION_SCHEMA |
||
| 784 | * |
||
| 785 | * @param string $table table |
||
| 786 | * |
||
| 787 | * @return array|bool table definition array if table exists, true if table not defined, or |
||
| 788 | * false on error. Error message in $this->lastError; |
||
| 789 | */ |
||
| 790 | protected function getTable($table) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * During processing, tables to be created are put in the queue as |
||
| 876 | * an array('createtable' => tablename) since the definition is not |
||
| 877 | * complete. This method will expand those references to the full |
||
| 878 | * ddl to create the table. |
||
| 879 | * |
||
| 880 | * @return void |
||
| 881 | */ |
||
| 882 | protected function expandQueue() |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Return message from last error encountered |
||
| 895 | * |
||
| 896 | * @return string last error message |
||
| 897 | */ |
||
| 898 | public function getLastError() |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Return code from last error encountered |
||
| 905 | * |
||
| 906 | * @return int last error number |
||
| 907 | */ |
||
| 908 | public function getLastErrNo() |
||
| 912 | |||
| 913 | /** |
||
| 914 | * dumpTables - utility function to dump raw tables array |
||
| 915 | * |
||
| 916 | * @return array tables |
||
| 917 | */ |
||
| 918 | public function dumpTables() |
||
| 922 | |||
| 923 | /** |
||
| 924 | * dumpQueue - utility function to dump the work queue |
||
| 925 | * |
||
| 926 | * @return array work queue |
||
| 927 | */ |
||
| 928 | 1 | public function dumpQueue() |
|
| 934 | |||
| 935 | /** |
||
| 936 | * addToQueue - utility function to add a statement to the work queue |
||
| 937 | * |
||
| 938 | * @param string $sql an SQL/DDL statement to add |
||
| 939 | * |
||
| 940 | * @return void |
||
| 941 | */ |
||
| 942 | 1 | public function addToQueue($sql) |
|
| 946 | |||
| 947 | /** |
||
| 948 | * Set lastError as table not established |
||
| 949 | * |
||
| 950 | * @return false |
||
| 951 | */ |
||
| 952 | protected function tableNotEstablished() |
||
| 958 | } |
||
| 959 |