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 | { |
||
21 | |||
22 | 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 $sortable_reset_pagination = FALSE; |
||
48 | |||
49 | /** |
||
50 | * @var null|callable |
||
51 | */ |
||
52 | protected $sortable_callback = NULL; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $sort; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $template_escaping = TRUE; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $header_escaping = FALSE; |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $align; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $template_variables = []; |
||
80 | |||
81 | /** |
||
82 | * @var callable |
||
83 | */ |
||
84 | protected $editable_callback; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
90 | |||
91 | /** |
||
92 | * Cached html elements |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $el_cache = []; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $default_hide = FALSE; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Render row item into template |
||
105 | * @param Row $row |
||
106 | * @return mixed |
||
107 | */ |
||
108 | public function render(Row $row) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Try to render item with custom renderer |
||
135 | * @param Row $row |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function useRenderer(Row $row) |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Should be column values escaped in latte? |
||
160 | * @param bool $template_escaping |
||
161 | */ |
||
162 | public function setTemplateEscaping($template_escaping = TRUE) |
||
168 | |||
169 | |||
170 | public function isTemplateEscaped() |
||
174 | |||
175 | /** |
||
176 | * Should be column header escaped in latte? |
||
177 | * @param bool $header_escaping |
||
178 | */ |
||
179 | public function setHeaderEscaping($header_escaping = FALSE) |
||
185 | |||
186 | public function isHeaderEscaped() |
||
190 | |||
191 | |||
192 | /** |
||
193 | * Set column sortable or not |
||
194 | * @param bool|string $sortable |
||
195 | */ |
||
196 | public function setSortable($sortable = TRUE) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Tell whether column is sortable |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isSortable() |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Shoud be the pagination reseted after sorting? |
||
216 | * @param bool $sortable_reset_pagination |
||
217 | * @return static |
||
218 | */ |
||
219 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Do reset pagination after sorting? |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function sortableResetPagination() |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Set custom ORDER BY clause |
||
239 | * @param callable $sortable_callback |
||
240 | * @return static |
||
241 | */ |
||
242 | public function setSortableCallback(callable $sortable_callback) |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Get custom ORDER BY clause |
||
252 | * @return callable|null |
||
253 | */ |
||
254 | public function getSortableCallback() |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Get column to sort by |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getSortingColumn() |
||
268 | |||
269 | |||
270 | /** |
||
271 | * Get column name |
||
272 | * @return string |
||
273 | */ |
||
274 | public function getColumnName() |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Get column value of row item |
||
282 | * @param Row $row |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function getColumnValue(Row $row) |
||
289 | |||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getName() |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Set column replacements |
||
302 | * @param array $replacements |
||
303 | * @return Column |
||
304 | */ |
||
305 | public function setReplacement(array $replacements) |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Tell whether columns has replacements |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function hasReplacements() |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Apply replacements |
||
325 | * @param Row $row |
||
326 | * @return array |
||
327 | */ |
||
328 | public function applyReplacements(Row $row) |
||
338 | |||
339 | |||
340 | /** |
||
341 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
342 | * @param callable $renderer |
||
343 | */ |
||
344 | public function setRenderer($renderer, $condition_callback = NULL) |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Set renderer callback just if condition is truthy |
||
372 | * @param callable $renderer |
||
373 | */ |
||
374 | public function setRendererOnCondition($renderer, $condition_callback) |
||
378 | |||
379 | |||
380 | /** |
||
381 | * Return custom renderer callback |
||
382 | * @return Renderer|null |
||
383 | */ |
||
384 | public function getRenderer() |
||
388 | |||
389 | |||
390 | /** |
||
391 | * Column may have its own template |
||
392 | * @param string $template |
||
393 | */ |
||
394 | public function setTemplate($template, array $template_variables = []) |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Column can have variables that will be passed to custom template scope |
||
405 | * @return array |
||
406 | */ |
||
407 | public function getTemplateVariables() |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Tell whether column has its owntemplate |
||
415 | * @return bool |
||
416 | */ |
||
417 | public function hasTemplate() |
||
421 | |||
422 | |||
423 | /** |
||
424 | * Get column template path |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getTemplate() |
||
431 | |||
432 | |||
433 | /** |
||
434 | * Tell whether data source is sorted by this collumn |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function isSortedBy() |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Tell column his sorting options |
||
445 | * @param array $sort |
||
446 | */ |
||
447 | public function setSort(array $sort) |
||
453 | |||
454 | |||
455 | /** |
||
456 | * What sorting will be applied after next click? |
||
457 | * @return array |
||
458 | */ |
||
459 | public function getSortNext() |
||
469 | |||
470 | |||
471 | /** |
||
472 | * Is sorting ascending? |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function isSortAsc() |
||
479 | |||
480 | |||
481 | /** |
||
482 | * Set column alignment |
||
483 | * @param string $align |
||
484 | */ |
||
485 | public function setAlign($align) |
||
491 | |||
492 | |||
493 | /** |
||
494 | * Has column some alignment? |
||
495 | * @return bool [description] |
||
496 | */ |
||
497 | public function hasAlign() |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Get column alignment |
||
505 | * @return string |
||
506 | */ |
||
507 | public function getAlign() |
||
511 | |||
512 | |||
513 | /** |
||
514 | * Set column content fit |
||
515 | * @param bool $fit_content |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function setFitContent($fit_content = TRUE) |
||
524 | |||
525 | |||
526 | /** |
||
527 | * Set callback that will be called after inline editing |
||
528 | * @param callable $editable_callback |
||
529 | */ |
||
530 | public function setEditableCallback(callable $editable_callback) |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Return callback that is used after inline editing |
||
540 | * @return callable |
||
541 | */ |
||
542 | public function getEditableCallback() |
||
546 | |||
547 | |||
548 | /** |
||
549 | * Is column editable? |
||
550 | * @return bool |
||
551 | */ |
||
552 | public function isEditable() |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Element is by default textarea, user can change that |
||
560 | * @param string $el_type |
||
561 | * @param array $attrs |
||
562 | * @return static |
||
563 | */ |
||
564 | public function setEditableInputType($el_type, array $attrs = []) |
||
570 | |||
571 | |||
572 | /** |
||
573 | * Change small inline edit input type to select |
||
574 | * @param array $options |
||
575 | * @return static |
||
576 | */ |
||
577 | public function setEditableInputTypeSelect(array $options = []) |
||
591 | |||
592 | |||
593 | /** |
||
594 | * [getEditableInputType description] |
||
595 | * @return array |
||
596 | */ |
||
597 | public function getEditableInputType() |
||
601 | |||
602 | |||
603 | /** |
||
604 | * Set attributes for both th and td element |
||
605 | * @param array $attrs |
||
606 | * @return static |
||
607 | */ |
||
608 | public function addAttributes(array $attrs) |
||
615 | |||
616 | |||
617 | /** |
||
618 | * Get th/td column element |
||
619 | * @param string $tag th|td |
||
620 | * @param string $key |
||
621 | * @param Row|NULL $row |
||
622 | * @return Html |
||
623 | */ |
||
624 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
667 | |||
668 | |||
669 | /** |
||
670 | * @param bool $default_hide |
||
671 | * @return static |
||
672 | */ |
||
673 | public function setDefaultHide($default_hide = TRUE) |
||
683 | |||
684 | |||
685 | public function getDefaultHide() |
||
689 | |||
690 | |||
691 | /** |
||
692 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
693 | * @param Row $row |
||
694 | * @param array $params_list |
||
695 | * @return array |
||
696 | */ |
||
697 | protected function getItemParams(Row $row, array $params_list) |
||
707 | |||
708 | |||
709 | /** |
||
710 | * @return string |
||
711 | */ |
||
712 | public function getColumn() |
||
716 | |||
717 | } |
||
718 |