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 HtmlTableContent 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 HtmlTableContent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class HtmlTableContent extends HtmlSemCollection { |
||
| 14 | protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ]; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * |
||
| 18 | * @param string $identifier |
||
| 19 | * @param string $tagName |
||
| 20 | * @param int $rowCount |
||
| 21 | * @param int $colCount |
||
| 22 | */ |
||
| 23 | public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @param int $rowCount |
||
| 32 | * @param int $colCount |
||
| 33 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 34 | */ |
||
| 35 | public function setRowCount($rowCount, $colCount) { |
||
| 42 | |||
| 43 | public function getTdTagName($tagName) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | * {@inheritDoc} |
||
| 50 | * |
||
| 51 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
| 52 | * @return HtmlTR |
||
| 53 | */ |
||
| 54 | protected function createItem($value) { |
||
| 64 | |||
| 65 | public function newRow($value) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param int $colCount |
||
| 71 | * @return HtmlTR |
||
| 72 | */ |
||
| 73 | public function addRow($colCount) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param mixed $row |
||
| 79 | * @return HtmlTR |
||
| 80 | */ |
||
| 81 | public function _addRow($row) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 87 | * @param int $row |
||
| 88 | * @param int $col |
||
| 89 | * @return \Ajax\semantic\html\content\HtmlTD |
||
| 90 | */ |
||
| 91 | public function getCell($row, $col) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * |
||
| 101 | * @param int $index |
||
| 102 | * @return \Ajax\semantic\html\content\HtmlTR |
||
| 103 | */ |
||
| 104 | public function getRow($index) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | * @param int $row |
||
| 111 | * @param int $col |
||
| 112 | * @param mixed $value |
||
| 113 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 114 | */ |
||
| 115 | public function setCellValue($row, $col, $value="") { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the cells values |
||
| 125 | * @param mixed $values |
||
| 126 | */ |
||
| 127 | public function setValues($values=array()) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds the cells values |
||
| 133 | * @param mixed $values |
||
| 134 | */ |
||
| 135 | public function addValues($values=array()) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds or sets the cells values |
||
| 141 | * @param mixed $values |
||
| 142 | * @param callable $callback |
||
| 143 | */ |
||
| 144 | protected function _addOrSetValues($values,$callback) { |
||
| 162 | |||
| 163 | public function setColValues($colIndex, $values=array()) { |
||
| 174 | |||
| 175 | public function addColVariations($colIndex, $variations=array()) { |
||
| 182 | |||
| 183 | public function setRowValues($rowIndex, $values=array()) { |
||
| 191 | |||
| 192 | private function colAlign($colIndex, $function) { |
||
| 201 | |||
| 202 | public function colCenter($colIndex) { |
||
| 205 | |||
| 206 | public function colRight($colIndex) { |
||
| 209 | |||
| 210 | public function colLeft($colIndex) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the number of rows (TR) |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | public function getRowCount() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the number of columns (TD) |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getColCount() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Removes the cell at position $rowIndex,$colIndex |
||
| 235 | * @param int $rowIndex |
||
| 236 | * @param int $colIndex |
||
| 237 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 238 | */ |
||
| 239 | public function delete($rowIndex, $colIndex=NULL) { |
||
| 250 | |||
| 251 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
| 254 | |||
| 255 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
| 258 | |||
| 259 | public function setFullWidth() { |
||
| 262 | |||
| 263 | public function sort($colIndex) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param mixed $callback |
||
| 269 | * @param string $format |
||
| 270 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 271 | */ |
||
| 272 | public function conditionalCellFormat($callback, $format) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param mixed $callback |
||
| 282 | * @param string $format |
||
| 283 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 284 | */ |
||
| 285 | public function conditionalRowFormat($callback, $format) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param mixed $callback |
||
| 295 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 296 | */ |
||
| 297 | public function applyCells($callback) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param mixed $callback |
||
| 307 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 308 | */ |
||
| 309 | public function applyRows($callback) { |
||
| 316 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.