Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class Column extends FilterableColumn |
||
| 20 | { |
||
| 21 | |||
| 22 | use Traits\TLink; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $replacements = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Renderer|NULL |
||
| 31 | */ |
||
| 32 | protected $renderer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $template; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool|string |
||
| 41 | */ |
||
| 42 | protected $sortable = FALSE; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $sortable_reset_pagination = FALSE; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var null|callable |
||
| 51 | */ |
||
| 52 | protected $sortable_callback = NULL; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $sort; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $template_escaping = TRUE; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $header_escaping = FALSE; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $align; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | protected $fit_content = FALSE; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $template_variables = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var callable |
||
| 87 | */ |
||
| 88 | protected $editable_callback; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Cached html elements |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $el_cache = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | protected $default_hide = FALSE; |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Render row item into template |
||
| 109 | * @param Row $row |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function render(Row $row) |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Try to render item with custom renderer |
||
| 139 | * @param Row $row |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | public function useRenderer(Row $row) |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Should be column values escaped in latte? |
||
| 164 | * @param bool $template_escaping |
||
| 165 | */ |
||
| 166 | public function setTemplateEscaping($template_escaping = TRUE) |
||
| 172 | |||
| 173 | |||
| 174 | public function isTemplateEscaped() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Should be column header escaped in latte? |
||
| 181 | * @param bool $header_escaping |
||
| 182 | */ |
||
| 183 | public function setHeaderEscaping($header_escaping = FALSE) |
||
| 189 | |||
| 190 | public function isHeaderEscaped() |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Set column sortable or not |
||
| 198 | * @param bool|string $sortable |
||
| 199 | */ |
||
| 200 | public function setSortable($sortable = TRUE) |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Tell whether column is sortable |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function isSortable() |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Shoud be the pagination reseted after sorting? |
||
| 220 | * @param bool $sortable_reset_pagination |
||
| 221 | * @return static |
||
| 222 | */ |
||
| 223 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Do reset pagination after sorting? |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function sortableResetPagination() |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Set custom ORDER BY clause |
||
| 243 | * @param callable $sortable_callback |
||
| 244 | * @return static |
||
| 245 | */ |
||
| 246 | public function setSortableCallback(callable $sortable_callback) |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Get custom ORDER BY clause |
||
| 256 | * @return callable|null |
||
| 257 | */ |
||
| 258 | public function getSortableCallback() |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Get column to sort by |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getSortingColumn() |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Get column name |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getColumnName() |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Get column value of row item |
||
| 286 | * @param Row $row |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function getColumnValue(Row $row) |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getName() |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Set column replacements |
||
| 306 | * @param array $replacements |
||
| 307 | * @return Column |
||
| 308 | */ |
||
| 309 | public function setReplacement(array $replacements) |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Tell whether columns has replacements |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function hasReplacements() |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Apply replacements |
||
| 329 | * @param Row $row |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function applyReplacements(Row $row) |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 346 | * @param callable $renderer |
||
| 347 | */ |
||
| 348 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Set renderer callback just if condition is truthy |
||
| 376 | * @param callable $renderer |
||
| 377 | */ |
||
| 378 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Return custom renderer callback |
||
| 386 | * @return Renderer|null |
||
| 387 | */ |
||
| 388 | public function getRenderer() |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Column may have its own template |
||
| 396 | * @param string $template |
||
| 397 | */ |
||
| 398 | public function setTemplate($template, array $template_variables = []) |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * Column can have variables that will be passed to custom template scope |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function getTemplateVariables() |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * Tell whether column has its owntemplate |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | public function hasTemplate() |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * Get column template path |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getTemplate() |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * Tell whether data source is sorted by this collumn |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public function isSortedBy() |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Tell column his sorting options |
||
| 449 | * @param array $sort |
||
| 450 | */ |
||
| 451 | public function setSort(array $sort) |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * What sorting will be applied after next click? |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getSortNext() |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Is sorting ascending? |
||
| 477 | * @return bool |
||
| 478 | */ |
||
| 479 | public function isSortAsc() |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * Set column alignment |
||
| 487 | * @param string $align |
||
| 488 | */ |
||
| 489 | public function setAlign($align) |
||
| 495 | |||
| 496 | |||
| 497 | /** |
||
| 498 | * Has column some alignment? |
||
| 499 | * @return bool [description] |
||
| 500 | */ |
||
| 501 | public function hasAlign() |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * Get column alignment |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getAlign() |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Set column content fit |
||
| 519 | * @param bool $fit_content |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | public function setFitContent($fit_content = TRUE) |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * Has column some fit content? |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function hasFitContent() |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * Set callback that will be called after inline editing |
||
| 542 | * @param callable $editable_callback |
||
| 543 | */ |
||
| 544 | public function setEditableCallback(callable $editable_callback) |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * Return callback that is used after inline editing |
||
| 554 | * @return callable |
||
| 555 | */ |
||
| 556 | public function getEditableCallback() |
||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * Is column editable? |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | public function isEditable() |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * Element is by default textarea, user can change that |
||
| 574 | * @param string $el_type |
||
| 575 | * @param array $attrs |
||
| 576 | * @return static |
||
| 577 | */ |
||
| 578 | public function setEditableInputType($el_type, array $attrs = []) |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Change small inline edit input type to select |
||
| 588 | * @param array $options |
||
| 589 | * @return static |
||
| 590 | */ |
||
| 591 | public function setEditableInputTypeSelect(array $options = []) |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * [getEditableInputType description] |
||
| 609 | * @return array |
||
| 610 | */ |
||
| 611 | public function getEditableInputType() |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * Set attributes for both th and td element |
||
| 619 | * @param array $attrs |
||
| 620 | * @return static |
||
| 621 | */ |
||
| 622 | public function addAttributes(array $attrs) |
||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * Get th/td column element |
||
| 633 | * @param string $tag th|td |
||
| 634 | * @param string $key |
||
| 635 | * @param Row|NULL $row |
||
| 636 | * @return Html |
||
| 637 | */ |
||
| 638 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
| 688 | |||
| 689 | |||
| 690 | /** |
||
| 691 | * @param bool $default_hide |
||
| 692 | * @return static |
||
| 693 | */ |
||
| 694 | public function setDefaultHide($default_hide = TRUE) |
||
| 704 | |||
| 705 | |||
| 706 | public function getDefaultHide() |
||
| 710 | |||
| 711 | |||
| 712 | /** |
||
| 713 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 714 | * @param Row $row |
||
| 715 | * @param array $params_list |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | protected function getItemParams(Row $row, array $params_list) |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | public function getColumn() |
||
| 737 | |||
| 738 | } |
||
| 739 |