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_Storage 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_Storage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 70 | class HTML_Table_Storage extends HTML_Common { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Value to insert into empty cells |
||
| 74 | * @var string |
||
| 75 | * @access private |
||
| 76 | */ |
||
| 77 | var $_autoFill = ' '; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Automatically adds a new row or column if a given row or column index |
||
| 81 | * does not exist |
||
| 82 | * @var bool |
||
| 83 | * @access private |
||
| 84 | */ |
||
| 85 | var $_autoGrow = true; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Array containing the table structure |
||
| 89 | * @var array |
||
| 90 | * @access private |
||
| 91 | */ |
||
| 92 | var $_structure = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Number of rows composing in the table |
||
| 96 | * @var int |
||
| 97 | * @access private |
||
| 98 | */ |
||
| 99 | var $_rows = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Number of column composing the table |
||
| 103 | * @var int |
||
| 104 | * @access private |
||
| 105 | */ |
||
| 106 | var $_cols = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Tracks the level of nested tables |
||
| 110 | * @var int |
||
| 111 | * @access private |
||
| 112 | */ |
||
| 113 | var $_nestLevel = 0; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Whether to use <thead>, <tfoot> and <tbody> or not |
||
| 117 | * @var bool |
||
| 118 | * @access private |
||
| 119 | */ |
||
| 120 | var $_useTGroups = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Class constructor |
||
| 124 | * @param int $tabOffset |
||
| 125 | * @param bool $useTGroups Whether to use <thead>, <tfoot> and |
||
| 126 | * <tbody> or not |
||
| 127 | * @access public |
||
| 128 | */ |
||
| 129 | public function __construct($tabOffset = 0, $useTGroups = false) |
||
| 130 | { |
||
| 131 | parent::__construct(null, (int)$tabOffset); |
||
| 132 | $this->_useTGroups = (boolean)$useTGroups; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sets the useTGroups value |
||
| 137 | * @param boolean $useTGroups |
||
| 138 | * @access public |
||
| 139 | */ |
||
| 140 | public function setUseTGroups($useTGroups) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the useTGroups value |
||
| 147 | * @access public |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | public function getUseTGroups() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the autoFill value |
||
| 157 | * @param mixed $fill |
||
| 158 | * @access public |
||
| 159 | */ |
||
| 160 | public function setAutoFill($fill) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the autoFill value |
||
| 167 | * @access public |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | public function getAutoFill() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Sets the autoGrow value |
||
| 177 | * @param bool $fill |
||
|
|
|||
| 178 | * @access public |
||
| 179 | */ |
||
| 180 | public function setAutoGrow($grow) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the autoGrow value |
||
| 187 | * @access public |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | public function getAutoGrow() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets the number of rows in the table |
||
| 197 | * @param int $rows |
||
| 198 | * @access public |
||
| 199 | */ |
||
| 200 | function setRowCount($rows) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the number of columns in the table |
||
| 207 | * @param int $cols |
||
| 208 | * @access public |
||
| 209 | */ |
||
| 210 | function setColCount($cols) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the number of rows in the table |
||
| 217 | * @access public |
||
| 218 | * @return int |
||
| 219 | */ |
||
| 220 | function getRowCount() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Gets the number of columns in the table |
||
| 227 | * |
||
| 228 | * If a row index is specified, the count will not take |
||
| 229 | * the spanned cells into account in the return value. |
||
| 230 | * |
||
| 231 | * @param int Row index to serve for cols count |
||
| 232 | * @access public |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | function getColCount($row = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Sets a rows type 'TH' or 'TD' |
||
| 251 | * @param int $row Row index |
||
| 252 | * @param string $type 'TH' or 'TD' |
||
| 253 | * @access public |
||
| 254 | */ |
||
| 255 | |||
| 256 | function setRowType($row, $type) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets a columns type 'TH' or 'TD' |
||
| 265 | * @param int $col Column index |
||
| 266 | * @param string $type 'TH' or 'TD' |
||
| 267 | * @access public |
||
| 268 | */ |
||
| 269 | function setColType($col, $type) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets the cell attributes for an existing cell. |
||
| 278 | * |
||
| 279 | * If the given indices do not exist and autoGrow is true then the given |
||
| 280 | * row and/or col is automatically added. If autoGrow is false then an |
||
| 281 | * error is returned. |
||
| 282 | * @param int $row Row index |
||
| 283 | * @param int $col Column index |
||
| 284 | * @param mixed $attributes Associative array or string of table |
||
| 285 | * row attributes |
||
| 286 | * @access public |
||
| 287 | * @throws PEAR_Error |
||
| 288 | */ |
||
| 289 | View Code Duplication | function setCellAttributes($row, $col, $attributes) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Updates the cell attributes passed but leaves other existing attributes |
||
| 308 | * intact |
||
| 309 | * @param int $row Row index |
||
| 310 | * @param int $col Column index |
||
| 311 | * @param mixed $attributes Associative array or string of table row |
||
| 312 | * attributes |
||
| 313 | * @access public |
||
| 314 | */ |
||
| 315 | View Code Duplication | function updateCellAttributes($row, $col, $attributes) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Returns the attributes for a given cell |
||
| 333 | * @param int $row Row index |
||
| 334 | * @param int $col Column index |
||
| 335 | * @return array |
||
| 336 | * @access public |
||
| 337 | */ |
||
| 338 | View Code Duplication | function getCellAttributes($row, $col) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Sets the cell contents for an existing cell |
||
| 353 | * |
||
| 354 | * If the given indices do not exist and autoGrow is true then the given |
||
| 355 | * row and/or col is automatically added. If autoGrow is false then an |
||
| 356 | * error is returned. |
||
| 357 | * @param int $row Row index |
||
| 358 | * @param int $col Column index |
||
| 359 | * @param mixed $contents May contain html or any object with a |
||
| 360 | * toHTML() method; if it is an array (with |
||
| 361 | * strings and/or objects), $col will be used |
||
| 362 | * as start offset and the array elements will |
||
| 363 | * be set to this and the following columns |
||
| 364 | * in $row |
||
| 365 | * @param string $type (optional) Cell type either 'TH' or 'TD' |
||
| 366 | * @access public |
||
| 367 | * @throws PEAR_Error |
||
| 368 | */ |
||
| 369 | function setCellContents($row, $col, $contents, $type = 'TD') |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Sets the cell contents for a single existing cell |
||
| 390 | * |
||
| 391 | * If the given indices do not exist and autoGrow is true then the given |
||
| 392 | * row and/or col is automatically added. If autoGrow is false then an |
||
| 393 | * error is returned. |
||
| 394 | * @param int $row Row index |
||
| 395 | * @param int $col Column index |
||
| 396 | * @param mixed $contents May contain html or any object with a |
||
| 397 | * toHTML() method; if it is an array (with |
||
| 398 | * strings and/or objects), $col will be used |
||
| 399 | * as start offset and the array elements will |
||
| 400 | * be set to this and the following columns |
||
| 401 | * in $row |
||
| 402 | * @param string $type (optional) Cell type either 'TH' or 'TD' |
||
| 403 | * @access private |
||
| 404 | * @throws PEAR_Error |
||
| 405 | */ |
||
| 406 | function _setSingleCellContents($row, $col, $contents, $type = 'TD') |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the cell contents for an existing cell |
||
| 423 | * @param int $row Row index |
||
| 424 | * @param int $col Column index |
||
| 425 | * @access public |
||
| 426 | * @return mixed |
||
| 427 | */ |
||
| 428 | View Code Duplication | function getCellContents($row, $col) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Sets the contents of a header cell |
||
| 444 | * @param int $row |
||
| 445 | * @param int $col |
||
| 446 | * @param mixed $contents |
||
| 447 | * @param mixed $attributes Associative array or string of table row |
||
| 448 | * attributes |
||
| 449 | * @access public |
||
| 450 | */ |
||
| 451 | function setHeaderContents($row, $col, $contents, $attributes = null) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Adds a table row and returns the row identifier |
||
| 461 | * @param array $contents (optional) Must be a indexed array of valid |
||
| 462 | * cell contents |
||
| 463 | * @param mixed $attributes (optional) Associative array or string of |
||
| 464 | * table row attributes. This can |
||
| 465 | * also be an array of attributes, |
||
| 466 | * in which case the attributes |
||
| 467 | * will be repeated in a loop. |
||
| 468 | * @param string $type (optional) Cell type either 'th' or 'td' |
||
| 469 | * @param bool $inTR false if attributes are to be |
||
| 470 | * applied in TD tags; true if |
||
| 471 | * attributes are to be applied in |
||
| 472 | * TR tag |
||
| 473 | * @return int |
||
| 474 | * @access public |
||
| 475 | */ |
||
| 476 | View Code Duplication | function addRow($contents = null, $attributes = null, $type = 'td', |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Sets the row attributes for an existing row |
||
| 502 | * @param int $row Row index |
||
| 503 | * @param mixed $attributes Associative array or string of table |
||
| 504 | * row attributes. This can also be an |
||
| 505 | * array of attributes, in which case the |
||
| 506 | * attributes will be repeated in a loop. |
||
| 507 | * @param bool $inTR false if attributes are to be applied |
||
| 508 | * in TD tags; true if attributes are to |
||
| 509 | * be applied in TR tag |
||
| 510 | * @access public |
||
| 511 | * @throws PEAR_Error |
||
| 512 | */ |
||
| 513 | View Code Duplication | function setRowAttributes($row, $attributes, $inTR = false) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Updates the row attributes for an existing row |
||
| 537 | * @param int $row Row index |
||
| 538 | * @param mixed $attributes Associative array or string of table |
||
| 539 | * row attributes |
||
| 540 | * @param bool $inTR false if attributes are to be applied |
||
| 541 | * in TD tags; true if attributes are to |
||
| 542 | * be applied in TR tag |
||
| 543 | * @access public |
||
| 544 | * @throws PEAR_Error |
||
| 545 | */ |
||
| 546 | View Code Duplication | function updateRowAttributes($row, $attributes = null, $inTR = false) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Returns the attributes for a given row as contained in the TR tag |
||
| 570 | * @param int $row Row index |
||
| 571 | * @return array |
||
| 572 | * @access public |
||
| 573 | */ |
||
| 574 | function getRowAttributes($row) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Alternates the row attributes starting at $start |
||
| 584 | * @param int $start Row index of row in which alternating |
||
| 585 | * begins |
||
| 586 | * @param mixed $attributes1 Associative array or string of table |
||
| 587 | * row attributes |
||
| 588 | * @param mixed $attributes2 Associative array or string of table |
||
| 589 | * row attributes |
||
| 590 | * @param bool $inTR false if attributes are to be applied |
||
| 591 | * in TD tags; true if attributes are to |
||
| 592 | * be applied in TR tag |
||
| 593 | * @param int $firstAttributes (optional) Which attributes should be |
||
| 594 | * applied to the first row, 1 or 2. |
||
| 595 | * @access public |
||
| 596 | */ |
||
| 597 | function altRowAttributes($start, $attributes1, $attributes2, $inTR = false, |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Adds a table column and returns the column identifier |
||
| 612 | * @param array $contents (optional) Must be a indexed array of valid |
||
| 613 | * cell contents |
||
| 614 | * @param mixed $attributes (optional) Associative array or string of |
||
| 615 | * table row attributes |
||
| 616 | * @param string $type (optional) Cell type either 'th' or 'td' |
||
| 617 | * @return int |
||
| 618 | * @access public |
||
| 619 | */ |
||
| 620 | View Code Duplication | function addCol($contents = null, $attributes = null, $type = 'td') |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Sets the column attributes for an existing column |
||
| 645 | * @param int $col Column index |
||
| 646 | * @param mixed $attributes (optional) Associative array or string |
||
| 647 | * of table row attributes |
||
| 648 | * @access public |
||
| 649 | */ |
||
| 650 | View Code Duplication | function setColAttributes($col, $attributes = null) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Updates the column attributes for an existing column |
||
| 665 | * @param int $col Column index |
||
| 666 | * @param mixed $attributes (optional) Associative array or string |
||
| 667 | * of table row attributes |
||
| 668 | * @access public |
||
| 669 | */ |
||
| 670 | View Code Duplication | function updateColAttributes($col, $attributes = null) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Sets the attributes for all cells |
||
| 685 | * @param mixed $attributes (optional) Associative array or |
||
| 686 | * string of table row attributes |
||
| 687 | * @access public |
||
| 688 | */ |
||
| 689 | function setAllAttributes($attributes = null) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Updates the attributes for all cells |
||
| 698 | * @param mixed $attributes (optional) Associative array or |
||
| 699 | * string of table row attributes |
||
| 700 | * @access public |
||
| 701 | */ |
||
| 702 | function updateAllAttributes($attributes = null) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Returns the table rows as HTML |
||
| 711 | * @access public |
||
| 712 | * @return string |
||
| 713 | */ |
||
| 714 | function toHtml($tabs = null, $tab = null) |
||
| 715 | { |
||
| 716 | $strHtml = ''; |
||
| 717 | if (is_null($tabs)) { |
||
| 718 | $tabs = $this->_getTabs(); |
||
| 719 | } |
||
| 720 | if (is_null($tab)) { |
||
| 721 | $tab = $this->_getTab(); |
||
| 722 | } |
||
| 723 | $lnEnd = $this->_getLineEnd(); |
||
| 724 | if ($this->_useTGroups) { |
||
| 725 | $extraTab = $tab; |
||
| 726 | } else { |
||
| 727 | $extraTab = ''; |
||
| 728 | } |
||
| 729 | if ($this->_cols > 0) { |
||
| 730 | for ($i = 0 ; $i < $this->_rows ; $i++) { |
||
| 731 | $attr = ''; |
||
| 732 | if (isset($this->_structure[$i]['attr'])) { |
||
| 733 | $attr = $this->_getAttrString($this->_structure[$i]['attr']); |
||
| 734 | } |
||
| 735 | $strHtml .= $tabs .$tab . $extraTab . '<tr'.$attr.'>' . $lnEnd; |
||
| 736 | for ($j = 0 ; $j < $this->_cols ; $j++) { |
||
| 737 | $attr = ''; |
||
| 738 | $contents = ''; |
||
| 739 | $type = 'td'; |
||
| 740 | if (isset($this->_structure[$i][$j]) && $this->_structure[$i][$j] == '__SPANNED__') { |
||
| 741 | continue; |
||
| 742 | } |
||
| 743 | if (isset($this->_structure[$i][$j]['type'])) { |
||
| 744 | $type = (strtolower($this->_structure[$i][$j]['type']) == 'th' ? 'th' : 'td'); |
||
| 745 | } |
||
| 746 | View Code Duplication | if (isset($this->_structure[$i][$j]['attr'])) { |
|
| 747 | $attr = $this->_structure[$i][$j]['attr']; |
||
| 748 | } |
||
| 749 | View Code Duplication | if (isset($this->_structure[$i][$j]['contents'])) { |
|
| 750 | $contents = $this->_structure[$i][$j]['contents']; |
||
| 751 | } |
||
| 752 | |||
| 753 | if (is_object($contents)) { |
||
| 754 | // changes indent and line end settings on nested tables |
||
| 755 | if (is_subclass_of($contents, 'html_common')) { |
||
| 756 | $contents->setTab($tab . $extraTab); |
||
| 757 | $contents->setTabOffset($this->_tabOffset + 3); |
||
| 758 | $contents->_nestLevel = $this->_nestLevel + 1; |
||
| 759 | $contents->setLineEnd($this->_getLineEnd()); |
||
| 760 | } |
||
| 761 | if (method_exists($contents, 'toHtml')) { |
||
| 762 | $contents = $contents->toHtml(); |
||
| 763 | } elseif (method_exists($contents, 'toString')) { |
||
| 764 | $contents = $contents->toString(); |
||
| 765 | } |
||
| 766 | } |
||
| 767 | if (is_array($contents)) { |
||
| 768 | $contents = implode(', ', $contents); |
||
| 769 | } |
||
| 770 | |||
| 771 | $typeContent = $tabs . $tab . $tab . $extraTab . "<$type" . $this->_getAttrString($attr) . '>'; |
||
| 772 | |||
| 773 | if (empty($contents)) { |
||
| 774 | if (isset($this->_autoFill) && $this->_autoFill) { |
||
| 775 | $contents = $this->_autoFill; |
||
| 776 | } |
||
| 777 | } else { |
||
| 778 | $typeContent .= $contents; |
||
| 779 | } |
||
| 780 | $typeContent .= "</$type>" . $lnEnd; |
||
| 781 | |||
| 782 | if (!empty($contents)) { |
||
| 783 | $strHtml .= $typeContent; |
||
| 784 | } |
||
| 785 | |||
| 786 | } |
||
| 787 | $strHtml .= $tabs . $tab . $extraTab . '</tr>' . $lnEnd; |
||
| 788 | } |
||
| 789 | } |
||
| 790 | return $strHtml; |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Checks if rows or columns are spanned |
||
| 795 | * @param int $row Row index |
||
| 796 | * @param int $col Column index |
||
| 797 | * @access private |
||
| 798 | */ |
||
| 799 | function _updateSpanGrid($row, $col) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Adjusts ends (total number of rows and columns) |
||
| 832 | * @param int $row Row index |
||
| 833 | * @param int $col Column index |
||
| 834 | * @param string $method Method name of caller |
||
| 835 | * Used to populate PEAR_Error if thrown. |
||
| 836 | * @param array $attributes Assoc array of attributes |
||
| 837 | * Default is an empty array. |
||
| 838 | * @access private |
||
| 839 | * @throws PEAR_Error |
||
| 840 | */ |
||
| 841 | function _adjustEnds($row, $col, $method, $attributes = array()) |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Tells if the parameter is an array of attribute arrays/strings |
||
| 867 | * @param mixed $attributes Variable to test |
||
| 868 | * @access private |
||
| 869 | * @return bool |
||
| 870 | */ |
||
| 871 | function _isAttributesArray($attributes) |
||
| 880 | |||
| 881 | } |
||
| 882 | ?> |
||
| 883 |
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.