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 Ublaboo\DataGrid\Object |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $column; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $replacements = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Renderer|NULL |
||
| 39 | */ |
||
| 40 | protected $renderer; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $template; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var boolean |
||
| 49 | */ |
||
| 50 | protected $is_sortable = FALSE; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $sort; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $template_escaping = TRUE; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $align; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $template_variables; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var callable |
||
| 74 | */ |
||
| 75 | protected $editable_callback; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var DataGrid |
||
| 79 | */ |
||
| 80 | protected $grid; |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $column |
||
| 85 | * @param string $name |
||
| 86 | */ |
||
| 87 | public function __construct($column, $name) |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Render row item into template |
||
| 96 | * @param Row $row |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | public function render(Row $row) |
||
| 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) |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Should be column values escaped in latte? |
||
| 147 | * @param boolean $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 $sortable |
||
| 166 | */ |
||
| 167 | public function setSortable($sortable = TRUE) |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Tell whether column is sortable |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public function isSortable() |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Get column name |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getColumnName() |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Get column value of row item |
||
| 197 | * @param Row $row |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | public function getColumnValue(Row $row) |
||
| 204 | |||
| 205 | |||
| 206 | public function getName() |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Set column replacements |
||
| 214 | * @param array $replacements |
||
| 215 | * @return Column |
||
| 216 | */ |
||
| 217 | public function setReplacement(array $replacements) |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Tell whether columns has replacements |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | public function hasReplacements() |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Apply replacements |
||
| 237 | * @param Row $row |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function applyReplacements(Row $row) |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
| 254 | * @param callable $renderer |
||
| 255 | */ |
||
| 256 | public function setRenderer($renderer, $condition_callback = NULL) |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Set renderer callback just if condition is truthy |
||
| 284 | * @param callable $renderer |
||
| 285 | */ |
||
| 286 | public function setRendererOnCondition($renderer, $condition_callback) |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Return custom renderer callback |
||
| 294 | * @return Renderer|null |
||
| 295 | */ |
||
| 296 | public function getRenderer() |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Column may have its own template |
||
| 304 | * @param string $template |
||
| 305 | */ |
||
| 306 | public function setTemplate($template, array $template_variables = []) |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Column can have variables that will be passed to custom template scope |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function getTemplateVariables() |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * Tell whether column has its owntemplate |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public function hasTemplate() |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Get column template path |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getTemplate() |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Tell whether data source is sorted by this collumn |
||
| 347 | * @return boolean |
||
| 348 | */ |
||
| 349 | public function isSortedBy() |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Tell column his sorting options |
||
| 357 | * @param array $sort |
||
| 358 | */ |
||
| 359 | public function setSort(array $sort) |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * What sorting will be applied after next click? |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getSortNext() |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Is sorting ascending? |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | public function isSortAsc() |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Set column alignment |
||
| 395 | * @param string $align |
||
| 396 | */ |
||
| 397 | public function setAlign($align) |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Has column some alignment? |
||
| 407 | * @return boolean [description] |
||
| 408 | */ |
||
| 409 | public function hasAlign() |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Get column alignment |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function getAlign() |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Set callback that will be called after inline editing |
||
| 427 | * @param callable $editable_callback |
||
| 428 | */ |
||
| 429 | public function setEditableCallback(callable $editable_callback) |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * Return callback that is used after inline editing |
||
| 439 | * @return callable |
||
| 440 | */ |
||
| 441 | public function getEditableCallback() |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Is column editable? |
||
| 449 | * @return boolean |
||
| 450 | */ |
||
| 451 | public function isEditable() |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Create link to custom destination |
||
| 459 | * @param string $href |
||
| 460 | * @param array $params |
||
| 461 | * @return string |
||
| 462 | * @throws DataGridHasToBeAttachedToPresenterComponentException |
||
| 463 | * @throws InvalidArgumentException |
||
| 464 | */ |
||
| 465 | protected function createLink($href, $params) |
||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
| 485 | * @param Row $row |
||
| 486 | * @param array $params_list |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | protected function getItemParams(Row $row, array $params_list) |
||
| 499 | |||
| 500 | } |
||
| 501 |