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 | * Cached html elements |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $el_cache = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $default_hide = FALSE; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Render row item into template |
||
| 92 | * @param Row $row |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | public function render(Row $row) |
||
| 96 | { |
||
| 97 | /** |
||
| 98 | * Renderer function may be used |
||
| 99 | */ |
||
| 100 | try { |
||
| 101 | return $this->useRenderer($row); |
||
| 102 | } catch (DataGridColumnRendererException $e) { |
||
|
|
|||
| 103 | /** |
||
| 104 | * Do not use renderer |
||
| 105 | */ |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Or replacements may be applied |
||
| 110 | */ |
||
| 111 | list($do_replace, $replaced) = $this->applyReplacements($row); |
||
| 112 | if ($do_replace) { |
||
| 113 | return $replaced; |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->getColumnValue($row); |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Try to render item with custom renderer |
||
| 122 | * @param Row $row |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public function useRenderer(Row $row) |
||
| 126 | { |
||
| 127 | $renderer = $this->getRenderer(); |
||
| 128 | |||
| 129 | if (!$renderer) { |
||
| 130 | throw new DataGridColumnRendererException; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($renderer->getConditionCallback()) { |
||
| 134 | if (!call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) { |
||
| 135 | throw new DataGridColumnRendererException; |
||
| 136 | } |
||
| 137 | |||
| 138 | return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
||
| 139 | } |
||
| 140 | |||
| 141 | return call_user_func_array($renderer->getCallback(), [$row->getItem()]); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Should be column values escaped in latte? |
||
| 147 | * @param bool $template_escaping |
||
| 148 | */ |
||
| 149 | public function setTemplateEscaping($template_escaping = TRUE) |
||
| 155 | |||
| 156 | |||
| 157 | public function isTemplateEscaped() |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Set column sortable or not |
||
| 165 | * @param bool|string $sortable |
||
| 166 | */ |
||
| 167 | public function setSortable($sortable = TRUE) |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Tell whether column is sortable |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function isSortable() |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Shoud be the pagination reseted after sorting? |
||
| 187 | * @param bool $sortable_reset_pagination |
||
| 188 | * @return static |
||
| 189 | */ |
||
| 190 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * DO reset pagination after sorting? |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function sortableResetPagination() |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Set custom ORDER BY clause |
||
| 210 | * @param callable $sortable_callback |
||
| 211 | * @return static |
||
| 212 | */ |
||
| 213 | public function setSortableCallback(callable $sortable_callback) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Get custom ORDER BY clause |
||
| 223 | * @return callable|null |
||
| 224 | */ |
||
| 225 | public function getSortableCallback() |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Get column to sort by |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getSortingColumn() |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Get column name |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getColumnName() |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * Get column value of row item |
||
| 253 | * @param Row $row |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | public function getColumnValue(Row $row) |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getName() |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Set column replacements |
||
| 273 | * @param array $replacements |
||
| 274 | * @return Column |
||
| 275 | */ |
||
| 276 | public function setReplacement(array $replacements) |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Tell whether columns has replacements |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function hasReplacements() |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Apply replacements |
||
| 296 | * @param Row $row |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public function applyReplacements(Row $row) |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 313 | * @param callable $renderer |
||
| 314 | */ |
||
| 315 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * Set renderer callback just if condition is truthy |
||
| 343 | * @param callable $renderer |
||
| 344 | */ |
||
| 345 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Return custom renderer callback |
||
| 353 | * @return Renderer|null |
||
| 354 | */ |
||
| 355 | public function getRenderer() |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Column may have its own template |
||
| 363 | * @param string $template |
||
| 364 | */ |
||
| 365 | public function setTemplate($template, array $template_variables = []) |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Column can have variables that will be passed to custom template scope |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function getTemplateVariables() |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Tell whether column has its owntemplate |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function hasTemplate() |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Get column template path |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getTemplate() |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * Tell whether data source is sorted by this collumn |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function isSortedBy() |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Tell column his sorting options |
||
| 416 | * @param array $sort |
||
| 417 | */ |
||
| 418 | public function setSort(array $sort) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * What sorting will be applied after next click? |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | public function getSortNext() |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Is sorting ascending? |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function isSortAsc() |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Set column alignment |
||
| 454 | * @param string $align |
||
| 455 | */ |
||
| 456 | public function setAlign($align) |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Has column some alignment? |
||
| 466 | * @return bool [description] |
||
| 467 | */ |
||
| 468 | public function hasAlign() |
||
| 472 | |||
| 473 | |||
| 474 | /** |
||
| 475 | * Get column alignment |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function getAlign() |
||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * Set callback that will be called after inline editing |
||
| 486 | * @param callable $editable_callback |
||
| 487 | */ |
||
| 488 | public function setEditableCallback(callable $editable_callback) |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * Return callback that is used after inline editing |
||
| 498 | * @return callable |
||
| 499 | */ |
||
| 500 | public function getEditableCallback() |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Is column editable? |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function isEditable() |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Set attributes for both th and td element |
||
| 518 | * @param array $attrs |
||
| 519 | * @return static |
||
| 520 | */ |
||
| 521 | public function addAttributes(array $attrs) |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * Get th/td column element |
||
| 532 | * @param string $tag th|td |
||
| 533 | * @param string $key |
||
| 534 | * @param Row|NULL $row |
||
| 535 | * @return Html |
||
| 536 | */ |
||
| 537 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * @param bool $default_hide |
||
| 581 | * @return static |
||
| 582 | */ |
||
| 583 | public function setDefaultHide($default_hide = TRUE) |
||
| 589 | |||
| 590 | |||
| 591 | public function getDefaultHide() |
||
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * Create link to custom destination |
||
| 599 | * @param string $href |
||
| 600 | * @param array $params |
||
| 601 | * @return string |
||
| 602 | * @throws DataGridHasToBeAttachedToPresenterComponentException |
||
| 603 | * @throws InvalidArgumentException |
||
| 604 | */ |
||
| 605 | protected function createLink($href, $params) |
||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 625 | * @param Row $row |
||
| 626 | * @param array $params_list |
||
| 627 | * @return array |
||
| 628 | */ |
||
| 629 | protected function getItemParams(Row $row, array $params_list) |
||
| 639 | |||
| 640 | } |
||
| 641 |