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 HTML_Table 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 HTML_Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 86 | class HTML_Table extends HTML_Common |
||
| 87 | { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Value to insert into empty cells. This is used as a default for |
||
| 91 | * newly-created tbodies. |
||
| 92 | * @var string |
||
| 93 | * @access private |
||
| 94 | */ |
||
| 95 | var $_autoFill = ' '; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Automatically adds a new row, column, or body if a given row, column, or |
||
| 99 | * body index does not exist. |
||
| 100 | * This is used as a default for newly-created tbodies. |
||
| 101 | * @var bool |
||
| 102 | * @access private |
||
| 103 | */ |
||
| 104 | var $_autoGrow = true; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Array containing the table caption |
||
| 108 | * @var array |
||
| 109 | * @access private |
||
| 110 | */ |
||
| 111 | var $_caption = array(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Array containing the table column group specifications |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | * @author Laurent Laville (pear at laurent-laville dot org) |
||
| 118 | * @access private |
||
| 119 | */ |
||
| 120 | var $_colgroup = array(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * HTML_Table_Storage object for the (t)head of the table |
||
| 124 | * @var object |
||
| 125 | * @access private |
||
| 126 | */ |
||
| 127 | var $_thead = null; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * HTML_Table_Storage object for the (t)foot of the table |
||
| 131 | * @var object |
||
| 132 | * @access private |
||
| 133 | */ |
||
| 134 | var $_tfoot = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * HTML_Table_Storage object for the (t)body of the table |
||
| 138 | * @var object |
||
| 139 | * @access private |
||
| 140 | */ |
||
| 141 | var $_tbodies = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Number of bodies in the table |
||
| 145 | * @var int |
||
| 146 | * @access private |
||
| 147 | */ |
||
| 148 | var $_tbodyCount = 0; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Whether to use <thead>, <tfoot> and <tbody> or not |
||
| 152 | * @var bool |
||
| 153 | * @access private |
||
| 154 | */ |
||
| 155 | var $_useTGroups = false; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Class constructor |
||
| 159 | * @param array $attributes Associative array of table tag |
||
| 160 | * attributes |
||
| 161 | * @param int $tabOffset Tab offset of the table |
||
| 162 | * @param bool $useTGroups Whether to use <thead>, <tfoot> and |
||
| 163 | * <tbody> or not |
||
| 164 | * @access public |
||
| 165 | */ |
||
| 166 | public function __construct($attributes = null, $tabOffset = 0, $useTGroups = false) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns the API version |
||
| 179 | * @access public |
||
| 180 | * @return double |
||
| 181 | * @deprecated |
||
| 182 | */ |
||
| 183 | function apiVersion() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns the HTML_Table_Storage object for <thead> |
||
| 190 | * @access public |
||
| 191 | * @return object |
||
| 192 | */ |
||
| 193 | View Code Duplication | function &getHeader() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the HTML_Table_Storage object for <tfoot> |
||
| 208 | * @access public |
||
| 209 | * @return object |
||
| 210 | */ |
||
| 211 | View Code Duplication | function &getFooter() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the HTML_Table_Storage object for the specified <tbody> |
||
| 226 | * (or the whole table if <t{head|foot|body}> is not used) |
||
| 227 | * @param int $body (optional) The index of the body to |
||
| 228 | * return. |
||
| 229 | * @access public |
||
| 230 | * @return object |
||
| 231 | * @throws PEAR_Error |
||
| 232 | */ |
||
| 233 | function &getBody($body = 0) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Adds a table body and returns the body identifier |
||
| 244 | * @param mixed $attributes (optional) Associative array or |
||
| 245 | * string of table body attributes |
||
| 246 | * @access public |
||
| 247 | * @return int |
||
| 248 | */ |
||
| 249 | function addBody($attributes = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Adjusts the number of bodies |
||
| 268 | * @param int $body Body index |
||
| 269 | * @param string $method Name of calling method |
||
| 270 | * @access private |
||
| 271 | * @throws PEAR_Error |
||
| 272 | */ |
||
| 273 | function _adjustTbodyCount($body, $method) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Sets the table caption |
||
| 287 | * @param string $caption |
||
| 288 | * @param mixed $attributes Associative array or string of |
||
| 289 | * table row attributes |
||
| 290 | * @access public |
||
| 291 | */ |
||
| 292 | function setCaption($caption, $attributes = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sets the table columns group specifications, or removes existing ones. |
||
| 300 | * |
||
| 301 | * @param mixed $colgroup (optional) Columns attributes |
||
| 302 | * @param mixed $attributes (optional) Associative array or string |
||
| 303 | * of table row attributes |
||
| 304 | * @author Laurent Laville (pear at laurent-laville dot org) |
||
| 305 | * @access public |
||
| 306 | */ |
||
| 307 | function setColGroup($colgroup = null, $attributes = null) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the autoFill value |
||
| 320 | * @param mixed $fill Whether autoFill should be enabled or not |
||
| 321 | * @param int $body (optional) The index of the body to set. |
||
| 322 | * Pass null to set for all bodies. |
||
| 323 | * @access public |
||
| 324 | * @throws PEAR_Error |
||
| 325 | */ |
||
| 326 | View Code Duplication | function setAutoFill($fill, $body = null) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the autoFill value |
||
| 344 | * @param int $body (optional) The index of the body to get. |
||
| 345 | * Pass null to get the default for new bodies. |
||
| 346 | * @access public |
||
| 347 | * @return mixed |
||
| 348 | * @throws PEAR_Error |
||
| 349 | */ |
||
| 350 | function getAutoFill($body = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Sets the autoGrow value |
||
| 365 | * @param bool $grow Whether autoGrow should be enabled or not |
||
| 366 | * @param int $body (optional) The index of the body to set. |
||
| 367 | * Pass null to set for all bodies. |
||
| 368 | * @access public |
||
| 369 | * @throws PEAR_Error |
||
| 370 | */ |
||
| 371 | View Code Duplication | function setAutoGrow($grow, $body = null) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the autoGrow value |
||
| 389 | * @param int $body (optional) The index of the body to get. |
||
| 390 | * Pass null to get the default for new bodies. |
||
| 391 | * @access public |
||
| 392 | * @return mixed |
||
| 393 | * @throws PEAR_Error |
||
| 394 | */ |
||
| 395 | function getAutoGrow($body = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the number of rows in the table body |
||
| 410 | * @param int $rows The number of rows |
||
| 411 | * @param int $body (optional) The index of the body to set. |
||
| 412 | * @access public |
||
| 413 | * @throws PEAR_Error |
||
| 414 | */ |
||
| 415 | function setRowCount($rows, $body = 0) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Sets the number of columns in the table |
||
| 426 | * @param int $cols The number of columns |
||
| 427 | * @param int $body (optional) The index of the body to set. |
||
| 428 | * @access public |
||
| 429 | * @throws PEAR_Error |
||
| 430 | */ |
||
| 431 | function setColCount($cols, $body = 0) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns the number of rows in the table |
||
| 442 | * @param int $body (optional) The index of the body to get. |
||
| 443 | * Pass null to get the total number of |
||
| 444 | * rows in all bodies. |
||
| 445 | * @access public |
||
| 446 | * @return int |
||
| 447 | * @throws PEAR_Error |
||
| 448 | */ |
||
| 449 | View Code Duplication | function getRowCount($body = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the number of columns in the table |
||
| 468 | * |
||
| 469 | * If a row index is specified, the count will not take |
||
| 470 | * the spanned cells into account in the return value. |
||
| 471 | * |
||
| 472 | * @param int $row Row index to serve for cols count |
||
| 473 | * @param int $body (optional) The index of the body to get. |
||
| 474 | * @access public |
||
| 475 | * @return int |
||
| 476 | * @throws PEAR_Error |
||
| 477 | */ |
||
| 478 | View Code Duplication | function getColCount($row = null, $body = 0) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Sets a rows type 'TH' or 'TD' |
||
| 489 | * @param int $row Row index |
||
| 490 | * @param string $type 'TH' or 'TD' |
||
| 491 | * @param int $body (optional) The index of the body to set. |
||
| 492 | * @access public |
||
| 493 | * @throws PEAR_Error |
||
| 494 | */ |
||
| 495 | function setRowType($row, $type, $body = 0) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Sets a columns type 'TH' or 'TD' |
||
| 506 | * @param int $col Column index |
||
| 507 | * @param string $type 'TH' or 'TD' |
||
| 508 | * @param int $body (optional) The index of the body to set. |
||
| 509 | * Pass null to set for all bodies. |
||
| 510 | * @access public |
||
| 511 | * @throws PEAR_Error |
||
| 512 | */ |
||
| 513 | View Code Duplication | function setColType($col, $type, $body = null) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Sets the cell attributes for an existing cell. |
||
| 530 | * |
||
| 531 | * If the given indices do not exist and autoGrow is true then the given |
||
| 532 | * row and/or col is automatically added. If autoGrow is false then an |
||
| 533 | * error is returned. |
||
| 534 | * @param int $row Row index |
||
| 535 | * @param int $col Column index |
||
| 536 | * @param mixed $attributes Associative array or string of |
||
| 537 | * table row attributes |
||
| 538 | * @param int $body (optional) The index of the body to set. |
||
| 539 | * @access public |
||
| 540 | * @throws PEAR_Error |
||
| 541 | */ |
||
| 542 | function setCellAttributes($row, $col, $attributes, $body = 0) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Updates the cell attributes passed but leaves other existing attributes |
||
| 556 | * intact |
||
| 557 | * @param int $row Row index |
||
| 558 | * @param int $col Column index |
||
| 559 | * @param mixed $attributes Associative array or string of table row |
||
| 560 | * attributes |
||
| 561 | * @param int $body (optional) The index of the body to set. |
||
| 562 | * @access public |
||
| 563 | * @throws PEAR_Error |
||
| 564 | */ |
||
| 565 | function updateCellAttributes($row, $col, $attributes, $body = 0) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Returns the attributes for a given cell |
||
| 579 | * @param int $row Row index |
||
| 580 | * @param int $col Column index |
||
| 581 | * @param int $body (optional) The index of the body to get. |
||
| 582 | * @return array |
||
| 583 | * @access public |
||
| 584 | * @throws PEAR_Error |
||
| 585 | */ |
||
| 586 | function getCellAttributes($row, $col, $body = 0) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Sets the cell contents for an existing cell |
||
| 597 | * |
||
| 598 | * If the given indices do not exist and autoGrow is true then the given |
||
| 599 | * row and/or col is automatically added. If autoGrow is false then an |
||
| 600 | * error is returned. |
||
| 601 | * @param int $row Row index |
||
| 602 | * @param int $col Column index |
||
| 603 | * @param mixed $contents May contain html or any object with a |
||
| 604 | * toHTML() method; it is an array (with |
||
| 605 | * strings and/or objects), $col will be |
||
| 606 | * used as start offset and the array |
||
| 607 | * elements will be set to this and the |
||
| 608 | * following columns in $row |
||
| 609 | * @param string $type (optional) Cell type either 'TH' or 'TD' |
||
| 610 | * @param int $body (optional) The index of the body to set. |
||
| 611 | * @access public |
||
| 612 | * @throws PEAR_Error |
||
| 613 | */ |
||
| 614 | function setCellContents($row, $col, $contents, $type = 'TD', $body = 0) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns the cell contents for an existing cell |
||
| 628 | * @param int $row Row index |
||
| 629 | * @param int $col Column index |
||
| 630 | * @param int $body (optional) The index of the body to get. |
||
| 631 | * @access public |
||
| 632 | * @return mixed |
||
| 633 | * @throws PEAR_Error |
||
| 634 | */ |
||
| 635 | function getCellContents($row, $col, $body = 0) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Sets the contents of a header cell |
||
| 646 | * @param int $row |
||
| 647 | * @param int $col |
||
| 648 | * @param mixed $contents |
||
| 649 | * @param mixed $attributes Associative array or string of |
||
| 650 | * table row attributes |
||
| 651 | * @param int $body (optional) The index of the body to set. |
||
| 652 | * @access public |
||
| 653 | * @throws PEAR_Error |
||
| 654 | */ |
||
| 655 | function setHeaderContents($row, $col, $contents, $attributes = null, |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Adds a table row and returns the row identifier |
||
| 667 | * @param array $contents (optional) Must be a indexed array of |
||
| 668 | * valid cell contents |
||
| 669 | * @param mixed $attributes (optional) Associative array or string |
||
| 670 | * of table row attributes. This can also |
||
| 671 | * be an array of attributes, in which |
||
| 672 | * case the attributes will be repeated |
||
| 673 | * in a loop. |
||
| 674 | * @param string $type (optional) Cell type either 'th' or 'td' |
||
| 675 | * @param bool $inTR false if attributes are to be applied |
||
| 676 | * in TD tags; true if attributes are to |
||
| 677 | * �be applied in TR tag |
||
| 678 | * @param int $body (optional) The index of the body to use. |
||
| 679 | * @return int |
||
| 680 | * @access public |
||
| 681 | * @throws PEAR_Error |
||
| 682 | */ |
||
| 683 | View Code Duplication | function addRow($contents = null, $attributes = null, $type = 'td', |
|
| 693 | |||
| 694 | /** |
||
| 695 | * Sets the row attributes for an existing row |
||
| 696 | * @param int $row Row index |
||
| 697 | * @param mixed $attributes Associative array or string of table row |
||
| 698 | * attributes. This can also be an array of |
||
| 699 | * attributes, in which case the attributes |
||
| 700 | * will be repeated in a loop. |
||
| 701 | * @param bool $inTR false if attributes are to be applied in |
||
| 702 | * TD tags; true if attributes are to be |
||
| 703 | * applied in TR tag |
||
| 704 | * @param int $body (optional) The index of the body to set. |
||
| 705 | * @access public |
||
| 706 | * @throws PEAR_Error |
||
| 707 | */ |
||
| 708 | View Code Duplication | function setRowAttributes($row, $attributes, $inTR = false, $body = 0) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Updates the row attributes for an existing row |
||
| 722 | * @param int $row Row index |
||
| 723 | * @param mixed $attributes Associative array or string of table row |
||
| 724 | * attributes |
||
| 725 | * @param bool $inTR false if attributes are to be applied in |
||
| 726 | * TD tags; true if attributes are to be |
||
| 727 | * applied in TR tag |
||
| 728 | * @param int $body (optional) The index of the body to set. |
||
| 729 | * @access public |
||
| 730 | * @throws PEAR_Error |
||
| 731 | */ |
||
| 732 | View Code Duplication | function updateRowAttributes($row, $attributes = null, $inTR = false, |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Returns the attributes for a given row as contained in the TR tag |
||
| 747 | * @param int $row Row index |
||
| 748 | * @param int $body (optional) The index of the body to get. |
||
| 749 | * @return array |
||
| 750 | * @access public |
||
| 751 | * @throws PEAR_Error |
||
| 752 | */ |
||
| 753 | function getRowAttributes($row, $body = 0) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Alternates the row attributes starting at $start |
||
| 764 | * @param int $start Row index of row in which alternating |
||
| 765 | * begins |
||
| 766 | * @param mixed $attributes1 Associative array or string of table |
||
| 767 | * row attributes |
||
| 768 | * @param mixed $attributes2 Associative array or string of table |
||
| 769 | * row attributes |
||
| 770 | * @param bool $inTR false if attributes are to be applied |
||
| 771 | * in TD tags; true if attributes are to |
||
| 772 | * be applied in TR tag |
||
| 773 | * @param int $firstAttributes (optional) Which attributes should be |
||
| 774 | * applied to the first row, 1 or 2. |
||
| 775 | * @param int $body (optional) The index of the body to set. |
||
| 776 | * Pass null to set for all bodies. |
||
| 777 | * @access public |
||
| 778 | * @throws PEAR_Error |
||
| 779 | */ |
||
| 780 | function altRowAttributes($start, $attributes1, $attributes2, $inTR = false, |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Adds a table column and returns the column identifier |
||
| 806 | * @param array $contents (optional) Must be a indexed array of |
||
| 807 | * valid cell contents |
||
| 808 | * @param mixed $attributes (optional) Associative array or string |
||
| 809 | * of table row attributes |
||
| 810 | * @param string $type (optional) Cell type either 'th' or 'td' |
||
| 811 | * @param int $body (optional) The index of the body to use. |
||
| 812 | * @return int |
||
| 813 | * @access public |
||
| 814 | * @throws PEAR_Error |
||
| 815 | */ |
||
| 816 | View Code Duplication | function addCol($contents = null, $attributes = null, $type = 'td', $body = 0) |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Sets the column attributes for an existing column |
||
| 827 | * @param int $col Column index |
||
| 828 | * @param mixed $attributes (optional) Associative array or string |
||
| 829 | * of table row attributes |
||
| 830 | * @param int $body (optional) The index of the body to set. |
||
| 831 | * Pass null to set for all bodies. |
||
| 832 | * @access public |
||
| 833 | * @throws PEAR_Error |
||
| 834 | */ |
||
| 835 | View Code Duplication | function setColAttributes($col, $attributes = null, $body = null) |
|
| 849 | |||
| 850 | /** |
||
| 851 | * Updates the column attributes for an existing column |
||
| 852 | * @param int $col Column index |
||
| 853 | * @param mixed $attributes (optional) Associative array or |
||
| 854 | * string of table row attributes |
||
| 855 | * @param int $body (optional) The index of the body to set. |
||
| 856 | * Pass null to set for all bodies. |
||
| 857 | * @access public |
||
| 858 | * @throws PEAR_Error |
||
| 859 | */ |
||
| 860 | View Code Duplication | function updateColAttributes($col, $attributes = null, $body = null) |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Sets the attributes for all cells |
||
| 877 | * @param mixed $attributes (optional) Associative array or |
||
| 878 | * string of table row attributes |
||
| 879 | * @param int $body (optional) The index of the body to set. |
||
| 880 | * Pass null to set for all bodies. |
||
| 881 | * @access public |
||
| 882 | * @throws PEAR_Error |
||
| 883 | */ |
||
| 884 | View Code Duplication | function setAllAttributes($attributes = null, $body = null) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * Updates the attributes for all cells |
||
| 901 | * @param mixed $attributes (optional) Associative array or string |
||
| 902 | * of table row attributes |
||
| 903 | * @param int $body (optional) The index of the body to set. |
||
| 904 | * Pass null to set for all bodies. |
||
| 905 | * @access public |
||
| 906 | * @throws PEAR_Error |
||
| 907 | */ |
||
| 908 | View Code Duplication | function updateAllAttributes($attributes = null, $body = null) |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Returns the table structure as HTML |
||
| 925 | * @access public |
||
| 926 | * @return string |
||
| 927 | */ |
||
| 928 | function toHtml() |
||
| 1027 | |||
| 1028 | } |
||
| 1029 | |||
| 1031 |