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 |
||
| 21 | class HtmlTable extends HtmlSemDoubleElement { |
||
| 22 | use TableTrait; |
||
| 23 | private $_colCount; |
||
| 24 | private $_compileParts; |
||
| 25 | private $_footer; |
||
| 26 | private $_afterCompileEvents; |
||
| 27 | private $_activeRowSelector; |
||
| 28 | |||
| 29 | public function __construct($identifier, $rowCount, $colCount) { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot |
||
| 40 | * @param string $key |
||
| 41 | * @return HtmlTableContent |
||
| 42 | */ |
||
| 43 | public function getPart($key) { |
||
| 44 | if (\array_key_exists($key, $this->content) === false) { |
||
| 45 | $this->content[$key]=new HtmlTableContent("", $key); |
||
| 46 | if ($key !== "tbody") { |
||
| 47 | $this->content[$key]->setRowCount(1, $this->_colCount); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | return $this->content[$key]; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function _getFirstPart(){ |
||
| 54 | if(isset($this->content["thead"])){ |
||
| 55 | return $this->content["thead"]; |
||
| 56 | } |
||
| 57 | return $this->content["tbody"]; |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns/create eventually the body of the table |
||
| 63 | * @return HtmlTableContent |
||
| 64 | */ |
||
| 65 | public function getBody() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the number of rows (TR) |
||
| 71 | * @return int |
||
| 72 | */ |
||
| 73 | public function getRowCount() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns/create eventually the header of the table |
||
| 79 | * @return HtmlTableContent |
||
| 80 | */ |
||
| 81 | public function getHeader() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns/create eventually the footer of the table |
||
| 87 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 88 | */ |
||
| 89 | public function getFooter() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Checks if the part corresponding to $key exists |
||
| 95 | * @param string $key |
||
| 96 | * @return boolean |
||
| 97 | */ |
||
| 98 | public function hasPart($key) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | * @param int $rowCount |
||
| 105 | * @param int $colCount |
||
| 106 | * @return HtmlTableContent |
||
| 107 | */ |
||
| 108 | public function setRowCount($rowCount, $colCount) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 115 | * @param int $row |
||
| 116 | * @param int $col |
||
| 117 | * @return HtmlTD |
||
| 118 | */ |
||
| 119 | public function getCell($row, $col) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Retuns the row at $rowIndex |
||
| 125 | * @param int $rowIndex |
||
| 126 | * @return HtmlTR |
||
| 127 | */ |
||
| 128 | public function getRow($rowIndex) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Adds a new row and sets $values to his cols |
||
| 134 | * @param array $values |
||
| 135 | * @return HtmlTR |
||
| 136 | */ |
||
| 137 | public function addRow($values=array()) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * adds and returns a new row |
||
| 145 | * @return HtmlTR |
||
| 146 | */ |
||
| 147 | public function newRow() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Sets the tbody values |
||
| 153 | * @param array $values values in an array of array |
||
| 154 | * @return HtmlTable |
||
| 155 | */ |
||
| 156 | public function setValues($values=array()) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets the header values |
||
| 163 | * @param array $values |
||
| 164 | * @return HtmlTableContent |
||
| 165 | */ |
||
| 166 | public function setHeaderValues($values=array()) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Sets the footer values |
||
| 172 | * @param array $values |
||
| 173 | * @return HtmlTableContent |
||
| 174 | */ |
||
| 175 | public function setFooterValues($values=array()) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Sets values to the col at index $colIndex |
||
| 181 | * @param int $colIndex |
||
| 182 | * @param array $values |
||
| 183 | * @return HtmlTable |
||
| 184 | */ |
||
| 185 | public function setColValues($colIndex, $values=array()) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets values to the row at index $rowIndex |
||
| 192 | * @param int $rowIndex |
||
| 193 | * @param array $values |
||
| 194 | * @return HtmlTable |
||
| 195 | */ |
||
| 196 | public function setRowValues($rowIndex, $values=array()) { |
||
| 200 | |||
| 201 | public function addColVariations($colIndex, $variations=array()) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the col alignment to center |
||
| 207 | * @param int $colIndex |
||
| 208 | * @return HtmlTable |
||
| 209 | */ |
||
| 210 | public function colCenter($colIndex) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Sets the col alignment to right |
||
| 216 | * @param int $colIndex |
||
| 217 | * @return HtmlTable |
||
| 218 | */ |
||
| 219 | public function colRight($colIndex) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets col alignment to left |
||
| 225 | * @param int $colIndex |
||
| 226 | * @return HtmlTable |
||
| 227 | */ |
||
| 228 | public function colLeft($colIndex) { |
||
| 231 | |||
| 232 | public function setColAlignment($colIndex,$alignment){ |
||
| 248 | |||
| 249 | private function colAlign($colIndex, $function) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Applies a format on each cell when $callback returns true |
||
| 265 | * @param callable $callback function with the cell as parameter, must return a boolean |
||
| 266 | * @param string $format css class to apply |
||
| 267 | * @return HtmlTable |
||
| 268 | */ |
||
| 269 | public function conditionalCellFormat($callback, $format) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Applies a format on each row when $callback returns true |
||
| 276 | * @param callable $callback function with the row as parameter, must return a boolean |
||
| 277 | * @param string $format css class to apply |
||
| 278 | * @return HtmlTable |
||
| 279 | */ |
||
| 280 | public function conditionalRowFormat($callback, $format) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Applies a callback function on each cell |
||
| 287 | * @param callable $callback |
||
| 288 | * @return HtmlTable |
||
| 289 | */ |
||
| 290 | public function applyCells($callback) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Applies a callback function on each row |
||
| 297 | * @param callable $callback |
||
| 298 | * @return HtmlTable |
||
| 299 | */ |
||
| 300 | public function applyRows($callback) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * |
||
| 307 | * {@inheritDoc} |
||
| 308 | * |
||
| 309 | * @see HtmlSemDoubleElement::compile() |
||
| 310 | */ |
||
| 311 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 319 | |||
| 320 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * {@inheritDoc} |
||
| 333 | * |
||
| 334 | * @see BaseHtml::fromDatabaseObject() |
||
| 335 | */ |
||
| 336 | public function fromDatabaseObject($object, $function) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Sets the parts of the Table to compile |
||
| 352 | * @param array $parts array of thead,tbody,tfoot |
||
| 353 | * @return HtmlTable |
||
| 354 | */ |
||
| 355 | public function setCompileParts($parts=["tbody"]) { |
||
| 359 | |||
| 360 | public function refresh(){ |
||
| 364 | |||
| 365 | public function run(JsUtils $js){ |
||
| 371 | |||
| 372 | /** |
||
| 373 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 374 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 375 | * @param callable $callback |
||
| 376 | * @return HtmlTable |
||
| 377 | */ |
||
| 378 | public function onNewRow($callback) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Defines how a row is selectable |
||
| 385 | * @param string $class |
||
| 386 | * @param string $event |
||
| 387 | * @param boolean $multiple |
||
| 388 | * @return HtmlTable |
||
| 389 | */ |
||
| 390 | public function setActiveRowSelector($class="active",$event="click",$multiple=false){ |
||
| 394 | |||
| 395 | public function hideColumn($colIndex){ |
||
| 405 | |||
| 406 | public function setColWidth($colIndex,$width){ |
||
| 412 | |||
| 413 | public function setColWidths($widths){ |
||
| 427 | |||
| 428 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
| 433 | } |
||
| 434 |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.