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 | private $_activeRowSelector; |
||
| 27 | |||
| 28 | public function __construct($identifier, $rowCount, $colCount) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | * @see \Ajax\semantic\html\collections\table\TableTrait::getTable() |
||
| 40 | */ |
||
| 41 | protected function getTable() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
| 47 | * @param string $key |
||
| 48 | * @return HtmlTableContent |
||
| 49 | */ |
||
| 50 | public function getPart($key) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns/create eventually the body of the table |
||
| 62 | * @return HtmlTableContent |
||
| 63 | */ |
||
| 64 | public function getBody() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns the number of rows (TR) |
||
| 70 | * @return int |
||
| 71 | */ |
||
| 72 | public function getRowCount() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns/create eventually the header of the table |
||
| 78 | * @return HtmlTableContent |
||
| 79 | */ |
||
| 80 | public function getHeader() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns/create eventually the footer of the table |
||
| 86 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 87 | */ |
||
| 88 | public function getFooter() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Checks if the part corresponding to $key exists |
||
| 94 | * @param string $key |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public function hasPart($key) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * |
||
| 103 | * @param int $rowCount |
||
| 104 | * @param int $colCount |
||
| 105 | * @return HtmlTableContent |
||
| 106 | */ |
||
| 107 | public function setRowCount($rowCount, $colCount) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 114 | * @param int $row |
||
| 115 | * @param int $col |
||
| 116 | * @return HtmlTD |
||
| 117 | */ |
||
| 118 | public function getCell($row, $col) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Retuns the row at $rowIndex |
||
| 124 | * @param int $rowIndex |
||
| 125 | * @return HtmlTR |
||
| 126 | */ |
||
| 127 | public function getRow($rowIndex) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds a new row and sets $values to his cols |
||
| 133 | * @param array $values |
||
| 134 | * @return HtmlTR |
||
| 135 | */ |
||
| 136 | public function addRow($values=array()) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * adds and returns a new row |
||
| 144 | * @return HtmlTR |
||
| 145 | */ |
||
| 146 | public function newRow() { |
||
| 149 | |||
| 150 | public function setValues($values=array()) { |
||
| 154 | |||
| 155 | public function setHeaderValues($values=array()) { |
||
| 158 | |||
| 159 | public function setFooterValues($values=array()) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Sets values to the col at index $colIndex |
||
| 165 | * @param int $colIndex |
||
| 166 | * @param array $values |
||
| 167 | * @return HtmlTable |
||
| 168 | */ |
||
| 169 | public function setColValues($colIndex, $values=array()) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets values to the row at index $rowIndex |
||
| 176 | * @param int $rowIndex |
||
| 177 | * @param array $values |
||
| 178 | * @return HtmlTable |
||
| 179 | */ |
||
| 180 | public function setRowValues($rowIndex, $values=array()) { |
||
| 184 | |||
| 185 | public function addColVariations($colIndex, $variations=array()) { |
||
| 188 | |||
| 189 | public function colCenter($colIndex) { |
||
| 192 | |||
| 193 | public function colRight($colIndex) { |
||
| 196 | |||
| 197 | public function colLeft($colIndex) { |
||
| 200 | |||
| 201 | private function colAlign($colIndex, $function) { |
||
| 214 | |||
| 215 | public function conditionalCellFormat($callback, $format) { |
||
| 219 | |||
| 220 | public function conditionalRowFormat($callback, $format) { |
||
| 224 | |||
| 225 | public function applyCells($callback) { |
||
| 229 | |||
| 230 | public function applyRows($callback) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * |
||
| 237 | * {@inheritDoc} |
||
| 238 | * |
||
| 239 | * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile() |
||
| 240 | */ |
||
| 241 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 249 | |||
| 250 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * |
||
| 261 | * {@inheritDoc} |
||
| 262 | * |
||
| 263 | * @see \Ajax\common\html\BaseHtml::fromDatabaseObject() |
||
| 264 | */ |
||
| 265 | public function fromDatabaseObject($object, $function) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $parts |
||
| 281 | * @return HtmlTable |
||
| 282 | */ |
||
| 283 | public function setCompileParts($parts=["tbody"]) { |
||
| 287 | |||
| 288 | public function refresh(){ |
||
| 292 | |||
| 293 | public function run(JsUtils $js){ |
||
| 299 | |||
| 300 | /** |
||
| 301 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 302 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 303 | * @param callable $callback |
||
| 304 | * @return HtmlTable |
||
| 305 | */ |
||
| 306 | public function onNewRow($callback) { |
||
| 310 | |||
| 311 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
| 315 | } |