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 |
||
| 20 | abstract class Column extends FilterableColumn |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $replacements = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Renderer|NULL |
||
| 30 | */ |
||
| 31 | protected $renderer; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $template; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var bool|string |
||
| 40 | */ |
||
| 41 | protected $sortable = FALSE; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $sortable_reset_pagination = FALSE; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var null|callable |
||
| 50 | */ |
||
| 51 | protected $sortable_callback = NULL; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $sort; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $template_escaping = TRUE; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $align; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $template_variables = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var callable |
||
| 75 | */ |
||
| 76 | protected $editable_callback; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Cached html elements |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $el_cache = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | protected $default_hide = FALSE; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Render row item into template |
||
| 97 | * @param Row $row |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function render(Row $row) |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Try to render item with custom renderer |
||
| 127 | * @param Row $row |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | public function useRenderer(Row $row) |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Should be column values escaped in latte? |
||
| 152 | * @param bool $template_escaping |
||
| 153 | */ |
||
| 154 | public function setTemplateEscaping($template_escaping = TRUE) |
||
| 160 | |||
| 161 | |||
| 162 | public function isTemplateEscaped() |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Set column sortable or not |
||
| 170 | * @param bool|string $sortable |
||
| 171 | */ |
||
| 172 | public function setSortable($sortable = TRUE) |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Tell whether column is sortable |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function isSortable() |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Shoud be the pagination reseted after sorting? |
||
| 192 | * @param bool $sortable_reset_pagination |
||
| 193 | * @return static |
||
| 194 | */ |
||
| 195 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Do reset pagination after sorting? |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function sortableResetPagination() |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Set custom ORDER BY clause |
||
| 215 | * @param callable $sortable_callback |
||
| 216 | * @return static |
||
| 217 | */ |
||
| 218 | public function setSortableCallback(callable $sortable_callback) |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Get custom ORDER BY clause |
||
| 228 | * @return callable|null |
||
| 229 | */ |
||
| 230 | public function getSortableCallback() |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Get column to sort by |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getSortingColumn() |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Get column name |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getColumnName() |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Get column value of row item |
||
| 258 | * @param Row $row |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | public function getColumnValue(Row $row) |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getName() |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Set column replacements |
||
| 278 | * @param array $replacements |
||
| 279 | * @return Column |
||
| 280 | */ |
||
| 281 | public function setReplacement(array $replacements) |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Tell whether columns has replacements |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function hasReplacements() |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Apply replacements |
||
| 301 | * @param Row $row |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function applyReplacements(Row $row) |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 318 | * @param callable $renderer |
||
| 319 | */ |
||
| 320 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Set renderer callback just if condition is truthy |
||
| 348 | * @param callable $renderer |
||
| 349 | */ |
||
| 350 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Return custom renderer callback |
||
| 358 | * @return Renderer|null |
||
| 359 | */ |
||
| 360 | public function getRenderer() |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Column may have its own template |
||
| 368 | * @param string $template |
||
| 369 | */ |
||
| 370 | public function setTemplate($template, array $template_variables = []) |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Column can have variables that will be passed to custom template scope |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getTemplateVariables() |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Tell whether column has its owntemplate |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function hasTemplate() |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * Get column template path |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getTemplate() |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Tell whether data source is sorted by this collumn |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function isSortedBy() |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Tell column his sorting options |
||
| 421 | * @param array $sort |
||
| 422 | */ |
||
| 423 | public function setSort(array $sort) |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * What sorting will be applied after next click? |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public function getSortNext() |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Is sorting ascending? |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public function isSortAsc() |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Set column alignment |
||
| 459 | * @param string $align |
||
| 460 | */ |
||
| 461 | public function setAlign($align) |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * Has column some alignment? |
||
| 471 | * @return bool [description] |
||
| 472 | */ |
||
| 473 | public function hasAlign() |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Get column alignment |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getAlign() |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Set callback that will be called after inline editing |
||
| 491 | * @param callable $editable_callback |
||
| 492 | */ |
||
| 493 | public function setEditableCallback(callable $editable_callback) |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Return callback that is used after inline editing |
||
| 503 | * @return callable |
||
| 504 | */ |
||
| 505 | public function getEditableCallback() |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * Is column editable? |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | public function isEditable() |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Element is by default textarea, user can chchge that |
||
| 523 | * @param string $el_type |
||
| 524 | * @param array $attrs |
||
| 525 | * @return static |
||
| 526 | */ |
||
| 527 | public function setEditableInputType($el_type, array $attrs = []) |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * [getEditableInputType description] |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public function getEditableInputType() |
||
| 543 | |||
| 544 | |||
| 545 | /** |
||
| 546 | * Set attributes for both th and td element |
||
| 547 | * @param array $attrs |
||
| 548 | * @return static |
||
| 549 | */ |
||
| 550 | public function addAttributes(array $attrs) |
||
| 557 | |||
| 558 | |||
| 559 | /** |
||
| 560 | * Get th/td column element |
||
| 561 | * @param string $tag th|td |
||
| 562 | * @param string $key |
||
| 563 | * @param Row|NULL $row |
||
| 564 | * @return Html |
||
| 565 | */ |
||
| 566 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
| 609 | |||
| 610 | |||
| 611 | /** |
||
| 612 | * @param bool $default_hide |
||
| 613 | * @return static |
||
| 614 | */ |
||
| 615 | public function setDefaultHide($default_hide = TRUE) |
||
| 625 | |||
| 626 | |||
| 627 | public function getDefaultHide() |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * Create link to custom destination |
||
| 635 | * @param string $href |
||
| 636 | * @param array $params |
||
| 637 | * @return string |
||
| 638 | * @throws DataGridHasToBeAttachedToPresenterComponentException |
||
| 639 | * @throws InvalidArgumentException |
||
| 640 | */ |
||
| 641 | protected function createLink($href, $params) |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 661 | * @param Row $row |
||
| 662 | * @param array $params_list |
||
| 663 | * @return array |
||
| 664 | */ |
||
| 665 | protected function getItemParams(Row $row, array $params_list) |
||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * @return string |
||
| 679 | */ |
||
| 680 | public function getColumn() |
||
| 684 | |||
| 685 | } |
||
| 686 |