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 Ublaboo\DataGrid\Object |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $column; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $name; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $replacements = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Renderer|NULL |
||
| 40 | */ |
||
| 41 | protected $renderer; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $template; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $is_sortable = FALSE; |
||
| 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 DataGrid |
||
| 80 | */ |
||
| 81 | protected $grid; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Cached html elements |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $el_cache = []; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * @param DataGrid $grid |
||
| 92 | * @param string $column |
||
| 93 | * @param string $name |
||
| 94 | */ |
||
| 95 | public function __construct(DataGrid $grid, $column, $name) |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Render row item into template |
||
| 105 | * @param Row $row |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | public function render(Row $row) |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Try to render item with custom renderer |
||
| 131 | * @param Row $row |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | public function useRenderer(Row $row) |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Should be column values escaped in latte? |
||
| 156 | * @param boolean $template_escaping |
||
| 157 | */ |
||
| 158 | public function setTemplateEscaping($template_escaping = TRUE) |
||
| 164 | |||
| 165 | |||
| 166 | public function isTemplateEscaped() |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Set column sortable or not |
||
| 174 | * @param bool $sortable |
||
| 175 | */ |
||
| 176 | public function setSortable($sortable = TRUE) |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Tell whether column is sortable |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public function isSortable() |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Get column name |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getColumnName() |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Get column value of row item |
||
| 206 | * @param Row $row |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | public function getColumnValue(Row $row) |
||
| 213 | |||
| 214 | |||
| 215 | public function getName() |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Set column replacements |
||
| 223 | * @param array $replacements |
||
| 224 | * @return Column |
||
| 225 | */ |
||
| 226 | public function setReplacement(array $replacements) |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Tell whether columns has replacements |
||
| 236 | * @return boolean |
||
| 237 | */ |
||
| 238 | public function hasReplacements() |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Apply replacements |
||
| 246 | * @param Row $row |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function applyReplacements(Row $row) |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 263 | * @param callable $renderer |
||
| 264 | */ |
||
| 265 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Set renderer callback just if condition is truthy |
||
| 293 | * @param callable $renderer |
||
| 294 | */ |
||
| 295 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Return custom renderer callback |
||
| 303 | * @return Renderer|null |
||
| 304 | */ |
||
| 305 | public function getRenderer() |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Column may have its own template |
||
| 313 | * @param string $template |
||
| 314 | */ |
||
| 315 | public function setTemplate($template, array $template_variables = []) |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Column can have variables that will be passed to custom template scope |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function getTemplateVariables() |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Tell whether column has its owntemplate |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function hasTemplate() |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Get column template path |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getTemplate() |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Tell whether data source is sorted by this collumn |
||
| 356 | * @return boolean |
||
| 357 | */ |
||
| 358 | public function isSortedBy() |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Tell column his sorting options |
||
| 366 | * @param array $sort |
||
| 367 | */ |
||
| 368 | public function setSort(array $sort) |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * What sorting will be applied after next click? |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function getSortNext() |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Is sorting ascending? |
||
| 394 | * @return boolean |
||
| 395 | */ |
||
| 396 | public function isSortAsc() |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Set column alignment |
||
| 404 | * @param string $align |
||
| 405 | */ |
||
| 406 | public function setAlign($align) |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Has column some alignment? |
||
| 416 | * @return boolean [description] |
||
| 417 | */ |
||
| 418 | public function hasAlign() |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Get column alignment |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getAlign() |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Set callback that will be called after inline editing |
||
| 436 | * @param callable $editable_callback |
||
| 437 | */ |
||
| 438 | public function setEditableCallback(callable $editable_callback) |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Return callback that is used after inline editing |
||
| 448 | * @return callable |
||
| 449 | */ |
||
| 450 | public function getEditableCallback() |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Is column editable? |
||
| 458 | * @return boolean |
||
| 459 | */ |
||
| 460 | public function isEditable() |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Set attributes for both th and td element |
||
| 468 | * @param array $attrs |
||
| 469 | * @return static |
||
| 470 | */ |
||
| 471 | public function addAttributes(array $attrs) |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * Get th/td column element |
||
| 482 | * @param string $tag th|td |
||
| 483 | * @param string $key |
||
| 484 | * @param Row|NULL $row |
||
| 485 | * @return Html |
||
| 486 | */ |
||
| 487 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Create link to custom destination |
||
| 529 | * @param string $href |
||
| 530 | * @param array $params |
||
| 531 | * @return string |
||
| 532 | * @throws DataGridHasToBeAttachedToPresenterComponentException |
||
| 533 | * @throws InvalidArgumentException |
||
| 534 | */ |
||
| 535 | protected function createLink($href, $params) |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 555 | * @param Row $row |
||
| 556 | * @param array $params_list |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | protected function getItemParams(Row $row, array $params_list) |
||
| 569 | |||
| 570 | } |
||
| 571 |