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 |
||
| 34 | class Tables |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var \XoopsDatabase |
||
| 38 | */ |
||
| 39 | protected $db; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $databaseName; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array Tables |
||
| 48 | */ |
||
| 49 | protected $tables; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array Work queue |
||
| 53 | */ |
||
| 54 | protected $queue; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string last error message |
||
| 58 | */ |
||
| 59 | protected $lastError; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int last error number |
||
| 63 | */ |
||
| 64 | protected $lastErrNo; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Constructor |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | public function __construct() |
||
| 71 | { |
||
| 72 | Language::load('xmf'); |
||
| 73 | |||
| 74 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 75 | $this->databaseName = XOOPS_DB_NAME; |
||
| 76 | $this->resetQueue(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Return a table name, prefixed with site table prefix |
||
| 81 | * |
||
| 82 | * @param string $table table name to contain prefix |
||
| 83 | * |
||
| 84 | * @return string table name with prefix |
||
| 85 | */ |
||
| 86 | protected function name($table) |
||
| 87 | { |
||
| 88 | return $this->db->prefix($table); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add new column for table to the work queue |
||
| 93 | * |
||
| 94 | * @param string $table table to contain the column |
||
| 95 | * @param string $column name of column to add |
||
| 96 | * @param string $attributes column_definition |
||
| 97 | * |
||
| 98 | * @return bool true if no errors, false if errors encountered |
||
| 99 | */ |
||
| 100 | public function addColumn($table, $column, $attributes) |
||
| 101 | { |
||
| 102 | $columnDef = array( |
||
| 103 | 'name' => $column, |
||
| 104 | 'attributes' => $attributes |
||
| 105 | ); |
||
| 106 | |||
| 107 | // Find table def. |
||
| 108 | if (isset($this->tables[$table])) { |
||
| 109 | $tableDef = &$this->tables[$table]; |
||
| 110 | // Is this on a table we are adding? |
||
| 111 | if (isset($tableDef['create']) && $tableDef['create']) { |
||
| 112 | array_push($tableDef['columns'], $columnDef); |
||
| 113 | } else { |
||
| 114 | View Code Duplication | foreach ($tableDef['columns'] as $col) { |
|
| 115 | if (strcasecmp($col['name'], $column) == 0) { |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | $this->queue[] = "ALTER TABLE `{$tableDef['name']}`" |
||
| 120 | . " ADD COLUMN `{$column}` {$columnDef['attributes']}"; |
||
| 121 | array_push($tableDef['columns'], $columnDef); |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | return $this->tableNotEstablished(); |
||
| 125 | } |
||
| 126 | |||
| 127 | return true; // exists or is added to queue |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add new primary key definition for table to work queue |
||
| 132 | * |
||
| 133 | * @param string $table table |
||
| 134 | * @param string $column column or comma separated list of columns |
||
| 135 | * to use as primary key |
||
| 136 | * |
||
| 137 | * @return bool true if no errors, false if errors encountered |
||
| 138 | */ |
||
| 139 | public function addPrimaryKey($table, $column) |
||
| 140 | { |
||
| 141 | $columns = str_getcsv(str_replace(' ', '', $column)); |
||
| 142 | $columnList = ''; |
||
| 143 | $firstComma = ''; |
||
| 144 | foreach ($columns as $col) { |
||
| 145 | $columnList .= "{$firstComma}`{$col}`"; |
||
| 146 | $firstComma = ', '; |
||
| 147 | } |
||
| 148 | if (isset($this->tables[$table])) { |
||
| 149 | if (isset($this->tables[$table]['create']) && $this->tables[$table]['create']) { |
||
| 150 | $this->tables[$table]['keys']['PRIMARY']['columns'] = $columnList; |
||
| 151 | } else { |
||
| 152 | $this->queue[] = "ALTER TABLE `{$this->tables[$table]['name']}` ADD PRIMARY KEY({$columnList})"; |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | return $this->tableNotEstablished(); |
||
| 156 | } |
||
| 157 | |||
| 158 | return true; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Add new index definition for index to work queue |
||
| 163 | * |
||
| 164 | * @param string $name name of index to add |
||
| 165 | * @param string $table table indexed |
||
| 166 | * @param string $column column or a comma separated list of columns |
||
| 167 | * to use as the key |
||
| 168 | * @param bool $unique true if index is to be unique |
||
| 169 | * |
||
| 170 | * @return bool true if no errors, false if errors encountered |
||
| 171 | */ |
||
| 172 | public function addIndex($name, $table, $column, $unique = false) |
||
| 173 | { |
||
| 174 | $columns = str_getcsv(str_replace(' ', '', $column)); |
||
| 175 | $columnList = ''; |
||
| 176 | $firstComma = ''; |
||
| 177 | foreach ($columns as $col) { |
||
| 178 | $columnList .= "{$firstComma}`{$col}`"; |
||
| 179 | $firstComma = ', '; |
||
| 180 | } |
||
| 181 | if (isset($this->tables[$table])) { |
||
| 182 | if (isset($this->tables[$table]['create']) && $this->tables[$table]['create']) { |
||
| 183 | $this->tables[$table]['keys'][$name]['columns'] = $columnList; |
||
| 184 | $this->tables[$table]['keys'][$name]['unique'] = (bool) $unique; |
||
| 185 | } else { |
||
| 186 | $add = ($unique ? 'ADD UNIQUE INDEX' : 'ADD INDEX'); |
||
| 187 | $this->queue[] = "ALTER TABLE `{$this->tables[$table]['name']}` {$add} `{$name}` ({$columnList})"; |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | return $this->tableNotEstablished(); |
||
| 191 | } |
||
| 192 | |||
| 193 | return true; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Load table schema from database, or starts new empty schema if |
||
| 198 | * table does not exist |
||
| 199 | * |
||
| 200 | * @param string $table table |
||
| 201 | * |
||
| 202 | * @return bool true if no errors, false if errors encountered |
||
| 203 | */ |
||
| 204 | public function addTable($table) |
||
| 205 | { |
||
| 206 | if (isset($this->tables[$table])) { |
||
| 207 | return true; |
||
| 208 | } |
||
| 209 | $tableDef = $this->getTable($table); |
||
| 210 | if (is_array($tableDef)) { |
||
| 211 | $this->tables[$table] = $tableDef; |
||
| 212 | |||
| 213 | return true; |
||
| 214 | } else { |
||
| 215 | if ($tableDef === true) { |
||
| 216 | $tableDef = array( |
||
| 217 | 'name' => $this->name($table), |
||
| 218 | 'options' => 'ENGINE=InnoDB', |
||
| 219 | 'columns' => array(), |
||
| 220 | 'keys' => array(), |
||
| 221 | 'create' => true, |
||
| 222 | ); |
||
| 223 | $this->tables[$table] = $tableDef; |
||
| 224 | |||
| 225 | $this->queue[] = array('createtable' => $table); |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } else { |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * AddTable only if it exists |
||
| 236 | * |
||
| 237 | * @param string $table table |
||
| 238 | * |
||
| 239 | * @return bool true if table exists, false otherwise |
||
| 240 | */ |
||
| 241 | public function useTable($table) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get column attributes |
||
| 256 | * |
||
| 257 | * @param string $table table containing the column |
||
| 258 | * @param string $column column to alter |
||
| 259 | * |
||
| 260 | * @return string|bool attribute string, or false if error encountered |
||
| 261 | */ |
||
| 262 | public function getColumnAttributes($table, $column) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get indexes for a table |
||
| 280 | * |
||
| 281 | * @param string $table get indexes for this named table |
||
| 282 | * |
||
| 283 | * @return array|bool array of indexes, or false if error encountered |
||
| 284 | */ |
||
| 285 | public function getTableIndexes($table) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Add alter column operation to the work queue |
||
| 297 | * |
||
| 298 | * @param string $table table containing the column |
||
| 299 | * @param string $column column to alter |
||
| 300 | * @param string $attributes new column_definition |
||
| 301 | * @param string $newName new name for column, blank to keep same |
||
| 302 | * |
||
| 303 | * @return bool true if no errors, false if errors encountered |
||
| 304 | */ |
||
| 305 | public function alterColumn($table, $column, $attributes, $newName = '') |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Loads table schema from database, and adds newTable with that |
||
| 346 | * schema to the queue |
||
| 347 | * |
||
| 348 | * @param string $table existing table |
||
| 349 | * @param string $newTable new table |
||
| 350 | * @param bool $withData true to copy data, false for schema only |
||
| 351 | * |
||
| 352 | * @return bool true if no errors, false if errors encountered |
||
| 353 | */ |
||
| 354 | public function copyTable($table, $newTable, $withData = false) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Add drop column operation to the work queue |
||
| 382 | * |
||
| 383 | * @param string $table table containing the column |
||
| 384 | * @param string $column column to drop |
||
| 385 | * |
||
| 386 | * @return bool true if no errors, false if errors encountered |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function dropColumn($table, $column) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Add drop index operation to the work queue |
||
| 403 | * |
||
| 404 | * @param string $name name of index to drop |
||
| 405 | * @param string $table table indexed |
||
| 406 | * |
||
| 407 | * @return bool true if no errors, false if errors encountered |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function dropIndex($name, $table) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Add drop for all (non-PRIMARY) keys for a table to the work |
||
| 423 | * queue. This can be used to clean up indexes with automatic names. |
||
| 424 | * |
||
| 425 | * @param string $table table indexed |
||
| 426 | * |
||
| 427 | * @return bool true if no errors, false if errors encountered |
||
| 428 | */ |
||
| 429 | public function dropIndexes($table) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Add drop of PRIMARY key for a table to the work queue |
||
| 459 | * |
||
| 460 | * @param string $table table |
||
| 461 | * |
||
| 462 | * @return bool true if no errors, false if errors encountered |
||
| 463 | */ |
||
| 464 | View Code Duplication | public function dropPrimaryKey($table) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Add drop of table to the work queue |
||
| 478 | * |
||
| 479 | * @param string $table table |
||
| 480 | * |
||
| 481 | * @return bool true if no errors, false if errors encountered |
||
| 482 | */ |
||
| 483 | View Code Duplication | public function dropTable($table) |
|
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Add rename table operation to the work queue |
||
| 497 | * |
||
| 498 | * @param string $table table |
||
| 499 | * @param string $newName new table name |
||
| 500 | * |
||
| 501 | * @return bool true if no errors, false if errors encountered |
||
| 502 | */ |
||
| 503 | public function renameTable($table, $newName) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Add alter table table_options (ENGINE, DEFAULT CHARSET, etc.) |
||
| 521 | * to work queue |
||
| 522 | * |
||
| 523 | * @param string $table table |
||
| 524 | * @param string $options table_options |
||
| 525 | * |
||
| 526 | * @return bool true if no errors, false if errors encountered |
||
| 527 | */ |
||
| 528 | public function setTableOptions($table, $options) |
||
| 545 | |||
| 546 | |||
| 547 | /** |
||
| 548 | * Clear the work queue |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | public function resetQueue() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Executes the work queue |
||
| 560 | * |
||
| 561 | * @param bool $force true to force updates even if this is a 'GET' request |
||
| 562 | * |
||
| 563 | * @return bool true if no errors, false if errors encountered |
||
| 564 | */ |
||
| 565 | public function executeQueue($force = false) |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Create a DELETE statement and add it to the work queue |
||
| 589 | * |
||
| 590 | * @param string $table table |
||
| 591 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 592 | * |
||
| 593 | * @return bool true if no errors, false if errors encountered |
||
| 594 | */ |
||
| 595 | public function delete($table, $criteria) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Create an INSERT SQL statement and add it to the work queue. |
||
| 615 | * |
||
| 616 | * @param string $table table |
||
| 617 | * @param array $columns array of 'column'=>'value' entries |
||
| 618 | * @param boolean $quoteValue true to quote values, false if caller handles quoting |
||
| 619 | * |
||
| 620 | * @return boolean true if no errors, false if errors encountered |
||
| 621 | */ |
||
| 622 | public function insert($table, $columns, $quoteValue = true) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Create an UPDATE SQL statement and add it to the work queue |
||
| 647 | * |
||
| 648 | * @param string $table table |
||
| 649 | * @param array $columns array of 'column'=>'value' entries |
||
| 650 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 651 | * @param boolean $quoteValue true to quote values, false if caller handles quoting |
||
| 652 | * |
||
| 653 | * @return boolean true if no errors, false if errors encountered |
||
| 654 | */ |
||
| 655 | public function update($table, $columns, $criteria, $quoteValue = true) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Add statement to remove all rows from a table to the work queue |
||
| 684 | * |
||
| 685 | * @param string $table table |
||
| 686 | * |
||
| 687 | * @return bool true if no errors, false if errors encountered |
||
| 688 | */ |
||
| 689 | View Code Duplication | public function truncate($table) |
|
| 700 | |||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * return SQL to create the table |
||
| 705 | * |
||
| 706 | * This method does NOT modify the work queue |
||
| 707 | * |
||
| 708 | * @param string $table table |
||
| 709 | * @param bool $prefixed true to return with table name prefixed |
||
| 710 | * |
||
| 711 | * @return string|false string SQL to create table, or false if errors encountered |
||
|
|
|||
| 712 | */ |
||
| 713 | protected function renderTableCreate($table, $prefixed = false) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * execute an SQL statement |
||
| 744 | * |
||
| 745 | * @param string $sql SQL statement to execute |
||
| 746 | * @param bool $force true to use force updates even in safe requests |
||
| 747 | * |
||
| 748 | * @return mixed result resource if no error, |
||
| 749 | * true if no error but no result |
||
| 750 | * false if error encountered. |
||
| 751 | * Any error message is in $this->lastError; |
||
| 752 | */ |
||
| 753 | protected function execSql($sql, $force = false) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * fetch the next row of a result set |
||
| 771 | * |
||
| 772 | * @param resource $result as returned by query |
||
| 773 | * |
||
| 774 | * @return mixed false on error |
||
| 775 | */ |
||
| 776 | protected function fetch($result) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * get table definition from INFORMATION_SCHEMA |
||
| 783 | * |
||
| 784 | * @param string $table table |
||
| 785 | * |
||
| 786 | * @return array|bool table definition array if table exists, true if table not defined, or |
||
| 787 | * false on error. Error message in $this->lastError; |
||
| 788 | */ |
||
| 789 | protected function getTable($table) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * During processing, tables to be created are put in the queue as |
||
| 875 | * an array('createtable' => tablename) since the definition is not |
||
| 876 | * complete. This method will expand those references to the full |
||
| 877 | * ddl to create the table. |
||
| 878 | * |
||
| 879 | * @return void |
||
| 880 | */ |
||
| 881 | protected function expandQueue() |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Return message from last error encountered |
||
| 894 | * |
||
| 895 | * @return string last error message |
||
| 896 | */ |
||
| 897 | public function getLastError() |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Return code from last error encountered |
||
| 904 | * |
||
| 905 | * @return int last error number |
||
| 906 | */ |
||
| 907 | public function getLastErrNo() |
||
| 911 | |||
| 912 | /** |
||
| 913 | * dumpTables - utility function to dump raw tables array |
||
| 914 | * |
||
| 915 | * @return array tables |
||
| 916 | */ |
||
| 917 | public function dumpTables() |
||
| 921 | |||
| 922 | /** |
||
| 923 | * dumpQueue - utility function to dump the work queue |
||
| 924 | * |
||
| 925 | * @return array work queue |
||
| 926 | */ |
||
| 927 | public function dumpQueue() |
||
| 933 | |||
| 934 | /** |
||
| 935 | * addToQueue - utility function to add a statement to the work queue |
||
| 936 | * |
||
| 937 | * @param string $sql an SQL/DDL statement to add |
||
| 938 | * |
||
| 939 | * @return void |
||
| 940 | */ |
||
| 941 | public function addToQueue($sql) |
||
| 945 | |||
| 946 | /** |
||
| 947 | * Set lastError as table not established |
||
| 948 | * |
||
| 949 | * @return false |
||
| 950 | */ |
||
| 951 | protected function tableNotEstablished() |
||
| 957 | } |
||
| 958 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.