Complex classes like HtmlTable 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 HtmlTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class HtmlTable extends HtmlSemDoubleElement { |
||
| 21 | use TableTrait; |
||
| 22 | private $_colCount; |
||
| 23 | private $_compileParts; |
||
| 24 | private $_footer; |
||
| 25 | private $_afterCompileEvents; |
||
| 26 | |||
| 27 | public function __construct($identifier, $rowCount, $colCount) { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritDoc} |
||
| 38 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 39 | */ |
||
| 40 | protected function getTable() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
| 46 | * @param string $key |
||
| 47 | * @return HtmlTableContent |
||
| 48 | */ |
||
| 49 | public function getPart($key) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns/create eventually the body of the table |
||
| 61 | * @return HtmlTableContent |
||
| 62 | */ |
||
| 63 | public function getBody() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the number of rows (TR) |
||
| 69 | * @return int |
||
| 70 | */ |
||
| 71 | public function getRowCount() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns/create eventually the header of the table |
||
| 77 | * @return HtmlTableContent |
||
| 78 | */ |
||
| 79 | public function getHeader() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns/create eventually the footer of the table |
||
| 85 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 86 | */ |
||
| 87 | public function getFooter() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Checks if the part corresponding to $key exists |
||
| 93 | * @param string $key |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | public function hasPart($key) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * |
||
| 102 | * @param int $rowCount |
||
| 103 | * @param int $colCount |
||
| 104 | * @return HtmlTableContent |
||
| 105 | */ |
||
| 106 | public function setRowCount($rowCount, $colCount) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 113 | * @param int $row |
||
| 114 | * @param int $col |
||
| 115 | * @return HtmlTD |
||
| 116 | */ |
||
| 117 | public function getCell($row, $col) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Retuns the row at $rowIndex |
||
| 123 | * @param int $rowIndex |
||
| 124 | * @return HtmlTR |
||
| 125 | */ |
||
| 126 | public function getRow($rowIndex) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Adds a new row and sets $values to his cols |
||
| 132 | * @param array $values |
||
| 133 | * @return HtmlTR |
||
| 134 | */ |
||
| 135 | public function addRow($values=array()) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * adds and returns a new row |
||
| 143 | * @return HtmlTR |
||
| 144 | */ |
||
| 145 | public function newRow() { |
||
| 148 | |||
| 149 | public function setValues($values=array()) { |
||
| 153 | |||
| 154 | public function setHeaderValues($values=array()) { |
||
| 157 | |||
| 158 | public function setFooterValues($values=array()) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets values to the col at index $colIndex |
||
| 164 | * @param int $colIndex |
||
| 165 | * @param array $values |
||
| 166 | * @return HtmlTable |
||
| 167 | */ |
||
| 168 | public function setColValues($colIndex, $values=array()) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets values to the row at index $rowIndex |
||
| 175 | * @param int $rowIndex |
||
| 176 | * @param array $values |
||
| 177 | * @return HtmlTable |
||
| 178 | */ |
||
| 179 | public function setRowValues($rowIndex, $values=array()) { |
||
| 183 | |||
| 184 | public function addColVariations($colIndex, $variations=array()) { |
||
| 187 | |||
| 188 | public function colCenter($colIndex) { |
||
| 191 | |||
| 192 | public function colRight($colIndex) { |
||
| 195 | |||
| 196 | public function colLeft($colIndex) { |
||
| 199 | |||
| 200 | private function colAlign($colIndex, $function) { |
||
| 213 | |||
| 214 | public function conditionalCellFormat($callback, $format) { |
||
| 218 | |||
| 219 | public function conditionalRowFormat($callback, $format) { |
||
| 223 | |||
| 224 | public function applyCells($callback) { |
||
| 228 | |||
| 229 | public function applyRows($callback) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * |
||
| 236 | * {@inheritDoc} |
||
| 237 | * |
||
| 238 | * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile() |
||
| 239 | */ |
||
| 240 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * |
||
| 255 | * {@inheritDoc} |
||
| 256 | * |
||
| 257 | * @see \Ajax\common\html\BaseHtml::fromDatabaseObject() |
||
| 258 | */ |
||
| 259 | public function fromDatabaseObject($object, $function) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param array $parts |
||
| 275 | * @return HtmlTable |
||
| 276 | */ |
||
| 277 | public function setCompileParts($parts=["tbody"]) { |
||
| 281 | |||
| 282 | public function refresh(){ |
||
| 286 | |||
| 287 | public function run(JsUtils $js){ |
||
| 293 | |||
| 294 | /** |
||
| 295 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 296 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 297 | * @param callable $callback |
||
| 298 | * @return HtmlTable |
||
| 299 | */ |
||
| 300 | public function onNewRow($callback) { |
||
| 304 | } |