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 |
||
| 39 | class Tables |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var Connection |
||
| 43 | */ |
||
| 44 | protected $db; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $databaseName; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array Tables |
||
| 53 | */ |
||
| 54 | protected $tables; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array Work queue |
||
| 58 | */ |
||
| 59 | protected $queue; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string last error message |
||
| 63 | */ |
||
| 64 | protected $lastError; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int last error number |
||
| 68 | */ |
||
| 69 | protected $lastErrNo; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor |
||
| 73 | * |
||
| 74 | */ |
||
| 75 | public function __construct() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return a table name, prefixed with site table prefix |
||
| 86 | * |
||
| 87 | * @param string $table table name to contain prefix |
||
| 88 | * |
||
| 89 | * @return string table name with prefix |
||
| 90 | */ |
||
| 91 | 1 | protected function name($table) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Add new column for table to the work queue |
||
| 98 | * |
||
| 99 | * @param string $table table to contain the column |
||
| 100 | * @param string $column name of column to add |
||
| 101 | * @param string $attributes column_definition |
||
| 102 | * |
||
| 103 | * @return bool true if no errors, false if errors encountered |
||
| 104 | */ |
||
| 105 | 1 | public function addColumn($table, $column, $attributes) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Add new primary key definition for table to work queue |
||
| 136 | * |
||
| 137 | * @param string $table table |
||
| 138 | * @param string $column column or comma separated list of columns |
||
| 139 | * to use as primary key |
||
| 140 | * |
||
| 141 | * @return bool true if no errors, false if errors encountered |
||
| 142 | */ |
||
| 143 | 1 | public function addPrimaryKey($table, $column) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Add new index definition for index to work queue |
||
| 167 | * |
||
| 168 | * @param string $name name of index to add |
||
| 169 | * @param string $table table indexed |
||
| 170 | * @param string $column column or a comma separated list of columns |
||
| 171 | * to use as the key |
||
| 172 | * @param bool $unique true if index is to be unique |
||
| 173 | * |
||
| 174 | * @return bool true if no errors, false if errors encountered |
||
| 175 | */ |
||
| 176 | 1 | public function addIndex($name, $table, $column, $unique = false) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Load table schema from database, or starts new empty schema if |
||
| 202 | * table does not exist |
||
| 203 | * |
||
| 204 | * @param string $table table |
||
| 205 | * |
||
| 206 | * @return bool true if no errors, false if errors encountered |
||
| 207 | */ |
||
| 208 | public function addTable($table) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * AddTable only if it exists |
||
| 240 | * |
||
| 241 | * @param string $table table |
||
| 242 | * |
||
| 243 | * @return bool true if table exists, false otherwise |
||
| 244 | */ |
||
| 245 | 1 | public function useTable($table) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get column attributes |
||
| 260 | * |
||
| 261 | * @param string $table table containing the column |
||
| 262 | * @param string $column column to alter |
||
| 263 | * |
||
| 264 | * @return string|bool attribute string, or false if error encountered |
||
| 265 | */ |
||
| 266 | 1 | public function getColumnAttributes($table, $column) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Get indexes for a table |
||
| 284 | * |
||
| 285 | * @param string $table get indexes for this named table |
||
| 286 | * |
||
| 287 | * @return array|bool array of indexes, or false if error encountered |
||
| 288 | */ |
||
| 289 | 1 | public function getTableIndexes($table) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Add alter column operation to the work queue |
||
| 301 | * |
||
| 302 | * @param string $table table containing the column |
||
| 303 | * @param string $column column to alter |
||
| 304 | * @param string $attributes new column_definition |
||
| 305 | * @param string $newName new name for column, blank to keep same |
||
| 306 | * @param mixed $position FIRST, string of column name to add new |
||
|
|
|||
| 307 | * column after, or null for no change |
||
| 308 | * |
||
| 309 | * @return bool true if no errors, false if errors encountered |
||
| 310 | */ |
||
| 311 | 1 | public function alterColumn($table, $column, $attributes, $newName = '') |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Loads table schema from database, and adds newTable with that |
||
| 344 | * schema to the queue |
||
| 345 | * |
||
| 346 | * @param string $table existing table |
||
| 347 | * @param string $newTable new table |
||
| 348 | * @param bool $withData true to copy data, false for schema only |
||
| 349 | * |
||
| 350 | * @return bool true if no errors, false if errors encountered |
||
| 351 | */ |
||
| 352 | 1 | public function copyTable($table, $newTable, $withData = false) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Add new index definition for index to work queue |
||
| 380 | * |
||
| 381 | * @param string $name name of index to add |
||
| 382 | * @param string $table table indexed |
||
| 383 | * @param string $column column or comma separated list of columns |
||
| 384 | * to use as the key |
||
| 385 | * @param bool $unique true if index is to be unique |
||
| 386 | * |
||
| 387 | * @return bool true if no errors, false if errors encountered |
||
| 388 | */ |
||
| 389 | public function createIndex($name, $table, $column, $unique = false) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Add drop column operation to the work queue |
||
| 408 | * |
||
| 409 | * @param string $table table containing the column |
||
| 410 | * @param string $column column to drop |
||
| 411 | * |
||
| 412 | * @return bool true if no errors, false if errors encountered |
||
| 413 | */ |
||
| 414 | 1 | View Code Duplication | public function dropColumn($table, $column) |
| 426 | |||
| 427 | /** |
||
| 428 | * Add drop index operation to the work queue |
||
| 429 | * |
||
| 430 | * @param string $name name of index to drop |
||
| 431 | * @param string $table table indexed |
||
| 432 | * |
||
| 433 | * @return bool true if no errors, false if errors encountered |
||
| 434 | */ |
||
| 435 | 1 | View Code Duplication | public function dropIndex($name, $table) |
| 446 | |||
| 447 | /** |
||
| 448 | * Add drop for all (non-PRIMARY) keys for a table to the work |
||
| 449 | * queue. This can be used to clean up indexes with automatic names. |
||
| 450 | * |
||
| 451 | * @param string $table table indexed |
||
| 452 | * |
||
| 453 | * @return bool true if no errors, false if errors encountered |
||
| 454 | */ |
||
| 455 | public function dropIndexes($table) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Add drop of PRIMARY key for a table to the work queue |
||
| 485 | * |
||
| 486 | * @param string $table table |
||
| 487 | * |
||
| 488 | * @return bool true if no errors, false if errors encountered |
||
| 489 | */ |
||
| 490 | 1 | View Code Duplication | public function dropPrimaryKey($table) |
| 501 | |||
| 502 | /** |
||
| 503 | * Add drop of table to the work queue |
||
| 504 | * |
||
| 505 | * @param string $table table |
||
| 506 | * |
||
| 507 | * @return bool true if no errors, false if errors encountered |
||
| 508 | */ |
||
| 509 | 1 | View Code Duplication | public function dropTable($table) |
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Add rename table operation to the work queue |
||
| 523 | * |
||
| 524 | * @param string $table table |
||
| 525 | * @param string $newName new table name |
||
| 526 | * |
||
| 527 | * @return bool true if no errors, false if errors encountered |
||
| 528 | */ |
||
| 529 | 1 | public function renameTable($table, $newName) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Add alter table table_options (ENGINE, DEFAULT CHARSET, etc.) |
||
| 547 | * to work queue |
||
| 548 | * |
||
| 549 | * @param string $table table |
||
| 550 | * @param array $options table_options |
||
| 551 | * |
||
| 552 | * @return bool true if no errors, false if errors encountered |
||
| 553 | */ |
||
| 554 | 1 | public function setTableOptions($table, $options) |
|
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * Clear the work queue |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | public function resetQueue() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Executes the work queue |
||
| 590 | * |
||
| 591 | * @param bool $force true to force updates even if this is a 'GET' request |
||
| 592 | * |
||
| 593 | * @return bool true if no errors, false if errors encountered |
||
| 594 | */ |
||
| 595 | public function executeQueue($force = false) |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * Create a DELETE statement and add it to the work queue |
||
| 619 | * |
||
| 620 | * @param string $table table |
||
| 621 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 622 | * |
||
| 623 | * @return bool true if no errors, false if errors encountered |
||
| 624 | */ |
||
| 625 | public function delete($table, $criteria) |
||
| 642 | |||
| 643 | /** Create an INSERT SQL statement and add it to the work queue. |
||
| 644 | * |
||
| 645 | * @param string $table table |
||
| 646 | * @param array $columns array of 'column'=>'value' entries |
||
| 647 | * |
||
| 648 | * @return boolean true if no errors, false if errors encountered |
||
| 649 | */ |
||
| 650 | public function insert($table, $columns) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Create an UPDATE SQL statement and add it to the work queue |
||
| 674 | * |
||
| 675 | * @param string $table table |
||
| 676 | * @param array $columns array of 'column'=>'value' entries |
||
| 677 | * @param string|CriteriaElement $criteria string where clause or object criteria |
||
| 678 | * |
||
| 679 | * @return boolean true if no errors, false if errors encountered |
||
| 680 | */ |
||
| 681 | public function update($table, $columns, $criteria) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Add statement to remove all rows from a table to the work queue |
||
| 710 | * |
||
| 711 | * @param string $table table |
||
| 712 | * |
||
| 713 | * @return bool true if no errors, false if errors encountered |
||
| 714 | */ |
||
| 715 | View Code Duplication | public function truncate($table) |
|
| 726 | |||
| 727 | |||
| 728 | |||
| 729 | /** |
||
| 730 | * return SQL to create the table |
||
| 731 | * |
||
| 732 | * This method does NOT modify the work queue |
||
| 733 | * |
||
| 734 | * @param string $table table |
||
| 735 | * @param bool $prefixed true to return with table name prefixed |
||
| 736 | * |
||
| 737 | * @return string|false string SQL to create table, or false if errors encountered |
||
| 738 | */ |
||
| 739 | protected function renderTableCreate($table, $prefixed = false) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * execute an SQL statement |
||
| 770 | * |
||
| 771 | * @param string $sql SQL statement to execute |
||
| 772 | * @param bool $force true to use force updates even in safe requests |
||
| 773 | * |
||
| 774 | * @return mixed result Statement, or false on error |
||
| 775 | * Any error message is in $this->lastError; |
||
| 776 | */ |
||
| 777 | protected function execSql($sql, $force = false) |
||
| 791 | |||
| 792 | /** |
||
| 793 | * fetch the next row of a result set |
||
| 794 | * |
||
| 795 | * @param Statement $result as returned by query |
||
| 796 | * |
||
| 797 | * @return mixed false on error |
||
| 798 | */ |
||
| 799 | protected function fetch($result) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * get table definition from INFORMATION_SCHEMA |
||
| 806 | * |
||
| 807 | * @param string $table table |
||
| 808 | * |
||
| 809 | * @return array|bool table definition array if table exists, true if table not defined, or |
||
| 810 | * false on error. Error message in $this->lastError; |
||
| 811 | */ |
||
| 812 | protected function getTable($table) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * During processing, tables to be created are put in the queue as |
||
| 899 | * an array('createtable' => tablename) since the definition is not |
||
| 900 | * complete. This method will expand those references to the full |
||
| 901 | * ddl to create the table. |
||
| 902 | * |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | protected function expandQueue() |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Return message from last error encountered |
||
| 918 | * |
||
| 919 | * @return string last error message |
||
| 920 | */ |
||
| 921 | public function getLastError() |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Return code from last error encountered |
||
| 928 | * |
||
| 929 | * @return int last error number |
||
| 930 | */ |
||
| 931 | public function getLastErrNo() |
||
| 935 | |||
| 936 | /** |
||
| 937 | * dumpTables - development function to dump raw tables array |
||
| 938 | * |
||
| 939 | * @return array tables |
||
| 940 | */ |
||
| 941 | public function dumpTables() |
||
| 945 | |||
| 946 | /** |
||
| 947 | * dumpQueue - development function to dump the work queue |
||
| 948 | * |
||
| 949 | * @return array work queue |
||
| 950 | */ |
||
| 951 | public function dumpQueue() |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Set lastError as table not established |
||
| 960 | * |
||
| 961 | * @return false |
||
| 962 | */ |
||
| 963 | protected function tableNotEstablished() |
||
| 969 | } |
||
| 970 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.