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 | 1 | { |
|
| 21 | |||
| 22 | 1 | 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 $translatable_header = TRUE; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $sortable_reset_pagination = FALSE; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var null|callable |
||
| 56 | */ |
||
| 57 | protected $sortable_callback = NULL; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $sort; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $template_escaping = TRUE; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $header_escaping = FALSE; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $align; |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $template_variables = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var callable |
||
| 88 | */ |
||
| 89 | protected $editable_callback; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var bool |
||
| 98 | */ |
||
| 99 | protected $default_hide = FALSE; |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Render row item into template |
||
| 104 | * @param Row $row |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | public function render(Row $row) |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Try to render item with custom renderer |
||
| 134 | * @param Row $row |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function useRenderer(Row $row) |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Should be column values escaped in latte? |
||
| 159 | * @param bool $template_escaping |
||
| 160 | */ |
||
| 161 | public function setTemplateEscaping($template_escaping = TRUE) |
||
| 167 | |||
| 168 | |||
| 169 | public function isTemplateEscaped() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Should be column header escaped in latte? |
||
| 176 | * @param bool $header_escaping |
||
| 177 | */ |
||
| 178 | public function setHeaderEscaping($header_escaping = FALSE) |
||
| 184 | |||
| 185 | public function isHeaderEscaped() |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Set column sortable or not |
||
| 193 | * @param bool|string $sortable |
||
| 194 | */ |
||
| 195 | public function setSortable($sortable = TRUE) |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Tell whether column is sortable |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isSortable() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set column translatable or not |
||
| 214 | */ |
||
| 215 | public function setTranslatableHeader($translatable_header = TRUE) |
||
| 216 | { |
||
| 217 | 1 | $this->translatable_header = (bool) $translatable_header; |
|
| 218 | |||
| 219 | 1 | return $this; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Tell wheter column is translatable |
||
| 224 | */ |
||
| 225 | public function isTranslatableHeader() |
||
| 226 | { |
||
| 227 | 1 | return (bool) $this->translatable_header; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Shoud be the pagination reseted after sorting? |
||
| 232 | * @param bool $sortable_reset_pagination |
||
| 233 | * @return static |
||
| 234 | */ |
||
| 235 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Do reset pagination after sorting? |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function sortableResetPagination() |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Set custom ORDER BY clause |
||
| 255 | * @param callable $sortable_callback |
||
| 256 | * @return static |
||
| 257 | */ |
||
| 258 | public function setSortableCallback(callable $sortable_callback) |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * Get custom ORDER BY clause |
||
| 268 | * @return callable|null |
||
| 269 | */ |
||
| 270 | public function getSortableCallback() |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Get column to sort by |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getSortingColumn() |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Get column name |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getColumnName() |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Get column value of row item |
||
| 298 | * @param Row $row |
||
| 299 | * @return mixed |
||
| 300 | */ |
||
| 301 | public function getColumnValue(Row $row) |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getName() |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Set column replacements |
||
| 318 | * @param array $replacements |
||
| 319 | * @return Column |
||
| 320 | */ |
||
| 321 | public function setReplacement(array $replacements) |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Tell whether columns has replacements |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function hasReplacements() |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Apply replacements |
||
| 341 | * @param Row $row |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | public function applyReplacements(Row $row) |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 358 | * @param callable $renderer |
||
| 359 | */ |
||
| 360 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Set renderer callback just if condition is truthy |
||
| 388 | * @param callable $renderer |
||
| 389 | */ |
||
| 390 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Return custom renderer callback |
||
| 398 | * @return Renderer|null |
||
| 399 | */ |
||
| 400 | public function getRenderer() |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * Column may have its own template |
||
| 408 | * @param string $template |
||
| 409 | */ |
||
| 410 | public function setTemplate($template, array $template_variables = []) |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Column can have variables that will be passed to custom template scope |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getTemplateVariables() |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * Tell whether column has its owntemplate |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function hasTemplate() |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Get column template path |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function getTemplate() |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * Tell whether data source is sorted by this collumn |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function isSortedBy() |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Tell column his sorting options |
||
| 461 | * @param array $sort |
||
| 462 | */ |
||
| 463 | public function setSort(array $sort) |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * What sorting will be applied after next click? |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | public function getSortNext() |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function hasSortNext() |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Is sorting ascending? |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function isSortAsc() |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * Set column alignment |
||
| 510 | * @param string $align |
||
| 511 | */ |
||
| 512 | public function setAlign($align) |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Has column some alignment? |
||
| 522 | * @return bool [description] |
||
| 523 | */ |
||
| 524 | public function hasAlign() |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * Get column alignment |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | public function getAlign() |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * Set column content fit |
||
| 542 | * @param bool $fit_content |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | public function setFitContent($fit_content = TRUE) |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * Set callback that will be called after inline editing |
||
| 555 | * @param callable $editable_callback |
||
| 556 | */ |
||
| 557 | public function setEditableCallback(callable $editable_callback) |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * Return callback that is used after inline editing |
||
| 567 | * @return callable |
||
| 568 | */ |
||
| 569 | public function getEditableCallback() |
||
| 573 | |||
| 574 | |||
| 575 | /** |
||
| 576 | * Is column editable? |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | public function isEditable() |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Element is by default textarea, user can change that |
||
| 587 | * @param string $el_type |
||
| 588 | * @param array $attrs |
||
| 589 | * @return static |
||
| 590 | */ |
||
| 591 | public function setEditableInputType($el_type, array $attrs = []) |
||
| 597 | |||
| 598 | |||
| 599 | /** |
||
| 600 | * Change small inline edit input type to select |
||
| 601 | * @param array $options |
||
| 602 | * @return static |
||
| 603 | */ |
||
| 604 | public function setEditableInputTypeSelect(array $options = []) |
||
| 618 | |||
| 619 | |||
| 620 | /** |
||
| 621 | * [getEditableInputType description] |
||
| 622 | * @return array |
||
| 623 | */ |
||
| 624 | public function getEditableInputType() |
||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * Set attributes for both th and td element |
||
| 632 | * @param array $attrs |
||
| 633 | * @return static |
||
| 634 | */ |
||
| 635 | public function addAttributes(array $attrs) |
||
| 642 | |||
| 643 | |||
| 644 | /** |
||
| 645 | * Get th/td column element |
||
| 646 | * @param string $tag th|td |
||
| 647 | * @param string $key |
||
| 648 | * @param Row|NULL $row |
||
| 649 | * @return Html |
||
| 650 | */ |
||
| 651 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
| 687 | |||
| 688 | |||
| 689 | /** |
||
| 690 | * @param bool $default_hide |
||
| 691 | * @return static |
||
| 692 | */ |
||
| 693 | public function setDefaultHide($default_hide = TRUE) |
||
| 703 | |||
| 704 | |||
| 705 | public function getDefaultHide() |
||
| 709 | |||
| 710 | |||
| 711 | /** |
||
| 712 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 713 | * @param Row $row |
||
| 714 | * @param array $params_list |
||
| 715 | * @return array |
||
| 716 | */ |
||
| 717 | protected function getItemParams(Row $row, array $params_list) |
||
| 727 | |||
| 728 | |||
| 729 | /** |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | public function getColumn() |
||
| 736 | |||
| 737 | } |
||
| 738 |