| Total Complexity | 88 |
| Total Lines | 467 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 1 |
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.
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 |
||
| 16 | class HtmlTableContent extends HtmlSemCollection { |
||
| 17 | |||
| 18 | protected $_tdTagNames = [ |
||
| 19 | "thead" => "th", |
||
| 20 | "tbody" => "td", |
||
| 21 | "tfoot" => "th" |
||
| 22 | ]; |
||
| 23 | |||
| 24 | protected $_merged = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * |
||
| 28 | * @param string $identifier |
||
| 29 | * @param string $tagName |
||
| 30 | * @param int $rowCount |
||
| 31 | * @param int $colCount |
||
| 32 | */ |
||
| 33 | public function __construct($identifier, $tagName = "tbody", $rowCount = NULL, $colCount = NULL) { |
||
| 34 | parent::__construct($identifier, $tagName, ""); |
||
| 35 | if (isset($rowCount) && isset($colCount)) |
||
| 36 | $this->setRowCount($rowCount, $colCount); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @param int $rowCount |
||
| 42 | * @param int $colCount |
||
| 43 | * @return HtmlTableContent |
||
| 44 | */ |
||
| 45 | public function setRowCount($rowCount, $colCount) { |
||
| 46 | $count = $this->count(); |
||
| 47 | for ($i = $count; $i < $rowCount; $i ++) { |
||
| 48 | $this->addItem($colCount); |
||
| 49 | } |
||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function getTdTagName($tagName) { |
||
| 54 | return $this->_tdTagNames[$this->tagName]; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function refreshTR() { |
||
| 58 | $this->_template = "%wrapContentBefore%%content%%wrapContentAfter%"; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * |
||
| 63 | * {@inheritdoc} |
||
| 64 | * |
||
| 65 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
| 66 | * @return HtmlTR |
||
| 67 | */ |
||
| 68 | protected function createItem($value) { |
||
| 69 | $count = $this->count(); |
||
| 70 | $tr = new HtmlTR(""); |
||
| 71 | $tr->setContainer($this, $count); |
||
| 72 | $tr->setTdTagName($this->_tdTagNames[$this->tagName]); |
||
| 73 | if (isset($value) === true) { |
||
| 74 | $tr->setColCount($value); |
||
| 75 | } |
||
| 76 | return $tr; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function newRow($value) { |
||
| 80 | return $this->createItem($value); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * |
||
| 85 | * @param int $colCount |
||
| 86 | * @return HtmlTR |
||
| 87 | */ |
||
| 88 | public function addRow($colCount) { |
||
| 89 | return $this->addItem($colCount); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @param mixed $row |
||
| 95 | * @return HtmlTR |
||
| 96 | */ |
||
| 97 | public function _addRow($row) { |
||
| 98 | return $this->addItem($row); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function addMergeRow($colCount, $value = null) { |
||
| 102 | $row = $this->addRow($colCount); |
||
| 103 | $row->mergeCol(); |
||
| 104 | if (isset($value)) { |
||
| 105 | $row->setValues([ |
||
| 106 | $value |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | return $row; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * |
||
| 114 | * @return HtmlTR |
||
| 115 | */ |
||
| 116 | public function getItem($index) { |
||
| 117 | return parent::getItem($index); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 122 | * |
||
| 123 | * @param int $row |
||
| 124 | * @param int $col |
||
| 125 | * @return HtmlTD|HtmlDoubleElement |
||
| 126 | */ |
||
| 127 | public function getCell($row, $col) { |
||
| 128 | $row = $this->getItem($row); |
||
| 129 | if (isset($row) && $row instanceof HtmlCollection) { |
||
| 130 | $col = $row->getItem($col); |
||
| 131 | } else { |
||
| 132 | $col = $row; |
||
| 133 | } |
||
| 134 | return $col; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * |
||
| 139 | * @param int $index |
||
| 140 | * @return HtmlTR |
||
| 141 | */ |
||
| 142 | public function getRow($index) { |
||
| 143 | return $this->getItem($index); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * |
||
| 148 | * @param int $row |
||
| 149 | * @param int $col |
||
| 150 | * @param mixed $value |
||
| 151 | * @return HtmlTableContent |
||
| 152 | */ |
||
| 153 | public function setCellValue($row, $col, $value = "") { |
||
| 154 | $cell = $this->getCell($row, $col); |
||
| 155 | if (isset($cell) === true) { |
||
| 156 | $cell->setValue($value); |
||
| 157 | } |
||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets the cells values |
||
| 163 | * |
||
| 164 | * @param mixed $values |
||
| 165 | */ |
||
| 166 | public function setValues($values = array()) { |
||
| 167 | return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) { |
||
| 168 | $row->setValues($_values); |
||
| 169 | }); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Adds the cells values |
||
| 174 | * |
||
| 175 | * @param mixed $values |
||
| 176 | */ |
||
| 177 | public function addValues($values = array()) { |
||
| 178 | return $this->_addOrSetValues($values, function (HtmlTR $row, $_values) { |
||
| 179 | $row->addValues($_values); |
||
| 180 | }); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds or sets the cells values |
||
| 185 | * |
||
| 186 | * @param mixed $values |
||
| 187 | * @param callable $callback |
||
| 188 | */ |
||
| 189 | protected function _addOrSetValues($values, $callback) { |
||
| 190 | $count = $this->count(); |
||
| 191 | $isArray = true; |
||
| 192 | if (! \is_array($values)) { |
||
| 193 | $values = \array_fill(0, $count, $values); |
||
| 194 | $isArray = false; |
||
| 195 | } |
||
| 196 | if (JArray::dimension($values) == 1 && $isArray) |
||
| 197 | $values = [ |
||
| 198 | $values |
||
| 199 | ]; |
||
| 200 | |||
| 201 | $count = \min(\sizeof($values), $count); |
||
| 202 | |||
| 203 | for ($i = 0; $i < $count; $i ++) { |
||
| 204 | $row = $this->content[$i]; |
||
| 205 | $callback($row, $values[$i]); |
||
| 206 | } |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function setColValues($colIndex, $values = array()) { |
||
| 211 | $count = $this->count(); |
||
| 212 | if (! \is_array($values)) { |
||
| 213 | $values = \array_fill(0, $count, $values); |
||
| 214 | } |
||
| 215 | $count = \min(\sizeof($values), $count); |
||
| 216 | for ($i = 0; $i < $count; $i ++) { |
||
| 217 | $this->getCell($i, $colIndex)->setValue($values[$i]); |
||
| 218 | } |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function addColVariations($colIndex, $variations = array()) { |
||
| 223 | $count = $this->count(); |
||
| 224 | for ($i = 0; $i < $count; $i ++) { |
||
| 225 | $cell = $this->getCell($i, $colIndex); |
||
| 226 | if ($cell instanceof BaseTrait) |
||
| 227 | $cell->addVariations($variations); |
||
| 228 | } |
||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | public function addPropertyCol($colIndex, $name, $value) { |
||
| 233 | $count = $this->count(); |
||
| 234 | for ($i = 0; $i < $count; $i ++) { |
||
| 235 | $cell = $this->getCell($i, $colIndex); |
||
| 236 | if (isset($cell)) |
||
| 237 | $cell->addToProperty($name, $value); |
||
| 238 | } |
||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | public function setRowValues($rowIndex, $values = array()) { |
||
| 243 | $count = $this->count(); |
||
| 244 | if (! \is_array($values)) { |
||
| 245 | $values = \array_fill(0, $count, $values); |
||
| 246 | } |
||
| 247 | $this->getItem($rowIndex)->setValues($values); |
||
| 248 | return $this; |
||
| 249 | } |
||
| 250 | |||
| 251 | private function colAlign($colIndex, $function) { |
||
| 252 | $count = $this->count(); |
||
| 253 | for ($i = 0; $i < $count; $i ++) { |
||
| 254 | $index = $this->content[$i]->getColPosition($colIndex); |
||
| 255 | if ($index !== NULL) |
||
| 256 | $this->getCell($i, $index)->$function(); |
||
| 257 | } |
||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | private function colAlignFromRight($colIndex, $function) { |
||
| 262 | $count = $this->count(); |
||
| 263 | for ($i = 0; $i < $count; $i ++) { |
||
| 264 | $maxRow = $this->content[$i]->count(); |
||
| 265 | $index = $maxRow - $colIndex - 1; |
||
| 266 | if (($cell = $this->getCell($i, $index)) !== NULL) { |
||
| 267 | if ($cell->getColspan() == 1) |
||
|
|
|||
| 268 | $cell->$function(); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function colCenter($colIndex) { |
||
| 275 | return $this->colAlign($colIndex, "textCenterAligned"); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function colRight($colIndex) { |
||
| 279 | return $this->colAlign($colIndex, "textRightAligned"); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function colLeft($colIndex) { |
||
| 283 | return $this->colAlign($colIndex, "textLeftAligned"); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function colCenterFromRight($colIndex) { |
||
| 287 | return $this->colAlignFromRight($colIndex, "textCenterAligned"); |
||
| 288 | } |
||
| 289 | |||
| 290 | public function colRightFromRight($colIndex) { |
||
| 291 | return $this->colAlignFromRight($colIndex, "textRightAligned"); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function colLeftFromRight($colIndex) { |
||
| 295 | return $this->colAlignFromRight($colIndex, "textLeftAligned"); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the number of rows (TR) |
||
| 300 | * |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | public function getRowCount() { |
||
| 304 | return $this->count(); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns the number of columns (TD) |
||
| 309 | * |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | public function getColCount() { |
||
| 313 | $result = 0; |
||
| 314 | if ($this->count() > 0) |
||
| 315 | $result = $this->getItem(0)->count(); |
||
| 316 | return $result; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Removes the cell at position $rowIndex,$colIndex |
||
| 321 | * |
||
| 322 | * @param int $rowIndex |
||
| 323 | * @param int $colIndex |
||
| 324 | * @return HtmlTableContent |
||
| 325 | */ |
||
| 326 | public function delete($rowIndex, $colIndex = NULL) { |
||
| 327 | if (isset($colIndex)) { |
||
| 328 | $row = $this->getItem($rowIndex); |
||
| 329 | if (isset($row) === true) { |
||
| 330 | $row->delete($colIndex); |
||
| 331 | } |
||
| 332 | } else { |
||
| 333 | $this->removeItem($rowIndex); |
||
| 334 | } |
||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function toDelete($rowIndex, $colIndex) { |
||
| 343 | } |
||
| 344 | |||
| 345 | public function toRowspanned($rowIndex, $colIndex) { |
||
| 346 | $row = $this->getItem($rowIndex); |
||
| 347 | if (isset($row) === true) |
||
| 348 | $row->toRowspanned($colIndex); |
||
| 349 | return $this; |
||
| 350 | } |
||
| 351 | |||
| 352 | public function mergeCol($rowIndex = 0, $colIndex = 0) { |
||
| 353 | return $this->getItem($rowIndex)->mergeCol($colIndex); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function mergeRow($rowIndex = 0, $colIndex = 0) { |
||
| 357 | return $this->getItem($rowIndex)->mergeRow($colIndex); |
||
| 358 | } |
||
| 359 | |||
| 360 | public function setFullWidth() { |
||
| 361 | return $this->addToProperty("class", "full-width"); |
||
| 362 | } |
||
| 363 | |||
| 364 | public function sort($colIndex) { |
||
| 365 | $this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort"); |
||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * |
||
| 371 | * @param mixed $callback |
||
| 372 | * @param string $format |
||
| 373 | * @return HtmlTableContent |
||
| 374 | */ |
||
| 375 | public function conditionalCellFormat($callback, $format) { |
||
| 376 | $rows = $this->content; |
||
| 377 | foreach ($rows as $row) { |
||
| 378 | $row->conditionalCellFormat($callback, $format); |
||
| 379 | } |
||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | public function conditionalColFormat($colIndex, $callback, $format) { |
||
| 384 | $rows = $this->content; |
||
| 385 | foreach ($rows as $row) { |
||
| 386 | $cell = $row->getItem($colIndex); |
||
| 387 | $cell->conditionnalCellFormat($callback, $format); |
||
| 388 | } |
||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * |
||
| 394 | * @param mixed $callback |
||
| 395 | * @param string $format |
||
| 396 | * @return HtmlTableContent |
||
| 397 | */ |
||
| 398 | public function conditionalRowFormat($callback, $format) { |
||
| 399 | $rows = $this->content; |
||
| 400 | foreach ($rows as $row) { |
||
| 401 | $row->conditionalRowFormat($callback, $format); |
||
| 402 | } |
||
| 403 | return $this; |
||
| 404 | } |
||
| 405 | |||
| 406 | public function hideColumn($colIndex) { |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * |
||
| 417 | * @param mixed $callback |
||
| 418 | * @return HtmlTableContent |
||
| 419 | */ |
||
| 420 | public function applyCells($callback) { |
||
| 421 | $rows = $this->content; |
||
| 422 | foreach ($rows as $row) { |
||
| 423 | $row->applyCells($callback); |
||
| 424 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * |
||
| 430 | * @param mixed $callback |
||
| 431 | * @return HtmlTableContent |
||
| 432 | */ |
||
| 433 | public function applyRows($callback) { |
||
| 434 | $rows = $this->content; |
||
| 435 | foreach ($rows as $row) { |
||
| 436 | $row->apply($callback); |
||
| 437 | } |
||
| 438 | return $this; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * |
||
| 443 | * @param int $colIndex |
||
| 444 | * @param string $function |
||
| 445 | * @return \Ajax\semantic\html\content\table\HtmlTableContent |
||
| 446 | * @see https://fomantic-ui.com/collections/table.html#definition needs rowspanned class |
||
| 447 | * @since fomantic-ui 2.4.8 |
||
| 448 | */ |
||
| 449 | public function mergeIdentiqualValues($colIndex, $function = "strip_tags") { |
||
| 450 | $rows = $this->content; |
||
| 451 | $identiqual = null; |
||
| 452 | $counter = 0; |
||
| 453 | $cellToMerge = null; |
||
| 454 | $functionExists = \function_exists($function); |
||
| 455 | foreach ($rows as $row) { |
||
| 456 | $cell = $row->getItem($colIndex); |
||
| 457 | $value = $cell->getContent(); |
||
| 458 | if ($functionExists) |
||
| 459 | $value = \call_user_func($function, $value); |
||
| 460 | if ($value !== $identiqual) { |
||
| 461 | if ($counter > 0 && isset($cellToMerge)) { |
||
| 462 | $cellToMerge->setRowspanned($counter); |
||
| 463 | } |
||
| 464 | $counter = 0; |
||
| 465 | $cellToMerge = $cell; |
||
| 466 | $identiqual = $value; |
||
| 467 | } |
||
| 468 | $counter ++; |
||
| 469 | } |
||
| 470 | if ($counter > 0 && isset($cellToMerge)) { |
||
| 471 | $cellToMerge->setRowspanned($counter); |
||
| 472 | } |
||
| 473 | return $this; |
||
| 474 | } |
||
| 475 | |||
| 476 | public function _isMerged() { |
||
| 477 | return $this->_merged; |
||
| 478 | } |
||
| 479 | |||
| 480 | public function _setMerged($value) { |
||
| 483 | } |
||
| 484 | } |
||
| 485 |