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 | * @var array |
||
77 | */ |
||
78 | protected $template_variables = []; |
||
79 | |||
80 | /** |
||
81 | * @var callable |
||
82 | */ |
||
83 | protected $editable_callback; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
89 | |||
90 | /** |
||
91 | * Cached html elements |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $el_cache = []; |
||
95 | |||
96 | /** |
||
97 | * @var bool |
||
98 | */ |
||
99 | protected $default_hide = FALSE; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Render row item into template |
||
104 | * @param Row $row |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function render(Row $row) |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Try to render item with custom renderer |
||
134 | * @param Row $row |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function useRenderer(Row $row) |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Should be column values escaped in latte? |
||
159 | * @param bool $template_escaping |
||
160 | */ |
||
161 | public function setTemplateEscaping($template_escaping = TRUE) |
||
167 | |||
168 | |||
169 | public function isTemplateEscaped() |
||
173 | |||
174 | /** |
||
175 | * Should be column header escaped in latte? |
||
176 | * @param bool $header_escaping |
||
177 | */ |
||
178 | public function setHeaderEscaping($header_escaping = FALSE) |
||
184 | |||
185 | public function isHeaderEscaped() |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Set column sortable or not |
||
193 | * @param bool|string $sortable |
||
194 | */ |
||
195 | public function setSortable($sortable = TRUE) |
||
201 | |||
202 | |||
203 | /** |
||
204 | * Tell whether column is sortable |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isSortable() |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Shoud be the pagination reseted after sorting? |
||
215 | * @param bool $sortable_reset_pagination |
||
216 | * @return static |
||
217 | */ |
||
218 | public function setSortableResetPagination($sortable_reset_pagination = TRUE) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Do reset pagination after sorting? |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function sortableResetPagination() |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Set custom ORDER BY clause |
||
238 | * @param callable $sortable_callback |
||
239 | * @return static |
||
240 | */ |
||
241 | public function setSortableCallback(callable $sortable_callback) |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Get custom ORDER BY clause |
||
251 | * @return callable|null |
||
252 | */ |
||
253 | public function getSortableCallback() |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Get column to sort by |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getSortingColumn() |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Get column name |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getColumnName() |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Get column value of row item |
||
281 | * @param Row $row |
||
282 | * @return mixed |
||
283 | */ |
||
284 | public function getColumnValue(Row $row) |
||
288 | |||
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getName() |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Set column replacements |
||
301 | * @param array $replacements |
||
302 | * @return Column |
||
303 | */ |
||
304 | public function setReplacement(array $replacements) |
||
310 | |||
311 | |||
312 | /** |
||
313 | * Tell whether columns has replacements |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function hasReplacements() |
||
320 | |||
321 | |||
322 | /** |
||
323 | * Apply replacements |
||
324 | * @param Row $row |
||
325 | * @return array |
||
326 | */ |
||
327 | public function applyReplacements(Row $row) |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Set renderer callback and (it may be optional - the condition callback will decide) |
||
341 | * @param callable $renderer |
||
342 | */ |
||
343 | public function setRenderer($renderer, $condition_callback = NULL) |
||
367 | |||
368 | |||
369 | /** |
||
370 | * Set renderer callback just if condition is truthy |
||
371 | * @param callable $renderer |
||
372 | */ |
||
373 | public function setRendererOnCondition($renderer, $condition_callback) |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Return custom renderer callback |
||
381 | * @return Renderer|null |
||
382 | */ |
||
383 | public function getRenderer() |
||
387 | |||
388 | |||
389 | /** |
||
390 | * Column may have its own template |
||
391 | * @param string $template |
||
392 | */ |
||
393 | public function setTemplate($template, array $template_variables = []) |
||
400 | |||
401 | |||
402 | /** |
||
403 | * Column can have variables that will be passed to custom template scope |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getTemplateVariables() |
||
410 | |||
411 | |||
412 | /** |
||
413 | * Tell whether column has its owntemplate |
||
414 | * @return bool |
||
415 | */ |
||
416 | public function hasTemplate() |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Get column template path |
||
424 | * @return string |
||
425 | */ |
||
426 | public function getTemplate() |
||
430 | |||
431 | |||
432 | /** |
||
433 | * Tell whether data source is sorted by this collumn |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function isSortedBy() |
||
440 | |||
441 | |||
442 | /** |
||
443 | * Tell column his sorting options |
||
444 | * @param array $sort |
||
445 | */ |
||
446 | public function setSort(array $sort) |
||
452 | |||
453 | |||
454 | /** |
||
455 | * What sorting will be applied after next click? |
||
456 | * @return array |
||
457 | */ |
||
458 | public function getSortNext() |
||
468 | |||
469 | |||
470 | /** |
||
471 | * Is sorting ascending? |
||
472 | * @return bool |
||
473 | */ |
||
474 | public function isSortAsc() |
||
478 | |||
479 | |||
480 | /** |
||
481 | * Set column alignment |
||
482 | * @param string $align |
||
483 | */ |
||
484 | public function setAlign($align) |
||
490 | |||
491 | |||
492 | /** |
||
493 | * Has column some alignment? |
||
494 | * @return bool [description] |
||
495 | */ |
||
496 | public function hasAlign() |
||
500 | |||
501 | |||
502 | /** |
||
503 | * Get column alignment |
||
504 | * @return string |
||
505 | */ |
||
506 | public function getAlign() |
||
510 | |||
511 | |||
512 | /** |
||
513 | * Set callback that will be called after inline editing |
||
514 | * @param callable $editable_callback |
||
515 | */ |
||
516 | public function setEditableCallback(callable $editable_callback) |
||
522 | |||
523 | |||
524 | /** |
||
525 | * Return callback that is used after inline editing |
||
526 | * @return callable |
||
527 | */ |
||
528 | public function getEditableCallback() |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Is column editable? |
||
536 | * @return bool |
||
537 | */ |
||
538 | public function isEditable() |
||
542 | |||
543 | |||
544 | /** |
||
545 | * Element is by default textarea, user can change that |
||
546 | * @param string $el_type |
||
547 | * @param array $attrs |
||
548 | * @return static |
||
549 | */ |
||
550 | public function setEditableInputType($el_type, array $attrs = []) |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Change small inline edit input type to select |
||
560 | * @param array $options |
||
561 | * @return static |
||
562 | */ |
||
563 | public function setEditableInputTypeSelect(array $options = []) |
||
577 | |||
578 | |||
579 | /** |
||
580 | * [getEditableInputType description] |
||
581 | * @return array |
||
582 | */ |
||
583 | public function getEditableInputType() |
||
587 | |||
588 | |||
589 | /** |
||
590 | * Set attributes for both th and td element |
||
591 | * @param array $attrs |
||
592 | * @return static |
||
593 | */ |
||
594 | public function addAttributes(array $attrs) |
||
601 | |||
602 | |||
603 | /** |
||
604 | * Get th/td column element |
||
605 | * @param string $tag th|td |
||
606 | * @param string $key |
||
607 | * @param Row|NULL $row |
||
608 | * @return Html |
||
609 | */ |
||
610 | public function getElementPrototype($tag, $key = NULL, Row $row = NULL) |
||
653 | |||
654 | |||
655 | /** |
||
656 | * @param bool $default_hide |
||
657 | * @return static |
||
658 | */ |
||
659 | public function setDefaultHide($default_hide = TRUE) |
||
669 | |||
670 | |||
671 | public function getDefaultHide() |
||
675 | |||
676 | |||
677 | /** |
||
678 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
679 | * @param Row $row |
||
680 | * @param array $params_list |
||
681 | * @return array |
||
682 | */ |
||
683 | protected function getItemParams(Row $row, array $params_list) |
||
693 | |||
694 | |||
695 | /** |
||
696 | * @return string |
||
697 | */ |
||
698 | public function getColumn() |
||
702 | |||
703 | } |
||
704 |