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 | 1 | abstract class Column extends FilterableColumn |
|
20 | { |
||
21 | 1 | use Traits\TButtonRenderer; |
|
22 | 1 | use Traits\TLink; |
|
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $template; |
||
28 | |||
29 | /** |
||
30 | * @var bool|string |
||
31 | */ |
||
32 | protected $sortable = false; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $translatable_header = true; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $sortable_reset_pagination = false; |
||
43 | |||
44 | /** |
||
45 | * @var null|callable |
||
46 | */ |
||
47 | protected $sortable_callback = null; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $sort; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $template_escaping = true; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $header_escaping = false; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $align; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $template_variables = []; |
||
75 | |||
76 | /** |
||
77 | * @var callable |
||
78 | */ |
||
79 | protected $editable_callback; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $editable_element = ['textarea', ['class' => 'form-control']]; |
||
85 | |||
86 | /** |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected $default_hide = false; |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $elementCache = ['td' => null, 'th' => null]; |
||
95 | |||
96 | /** |
||
97 | * @var callable|NULL |
||
98 | */ |
||
99 | protected $editable_value_callback = null; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Render row item into template |
||
104 | * @param Row $row |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function render(Row $row) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Should be column values escaped in latte? |
||
135 | * @param bool $template_escaping |
||
136 | */ |
||
137 | public function setTemplateEscaping($template_escaping = true) |
||
143 | |||
144 | |||
145 | public function isTemplateEscaped() |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Should be column header escaped in latte? |
||
153 | * @param bool $header_escaping |
||
154 | */ |
||
155 | public function setHeaderEscaping($header_escaping = false) |
||
161 | |||
162 | |||
163 | public function isHeaderEscaped() |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Set column sortable or not |
||
171 | * @param bool|string $sortable |
||
172 | */ |
||
173 | public function setSortable($sortable = true) |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Tell whether column is sortable |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function isSortable() |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Set column translatable or not |
||
193 | */ |
||
194 | public function setTranslatableHeader($translatable_header = true) |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Tell wheter column is translatable |
||
204 | */ |
||
205 | public function isTranslatableHeader() |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Shoud be the pagination reseted after sorting? |
||
213 | * @param bool $sortable_reset_pagination |
||
214 | * @return static |
||
215 | */ |
||
216 | public function setSortableResetPagination($sortable_reset_pagination = true) |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Do reset pagination after sorting? |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function sortableResetPagination() |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Set custom ORDER BY clause |
||
236 | * @param callable $sortable_callback |
||
237 | * @return static |
||
238 | */ |
||
239 | public function setSortableCallback(callable $sortable_callback) |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Get custom ORDER BY clause |
||
249 | * @return callable|null |
||
250 | */ |
||
251 | public function getSortableCallback() |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Get column to sort by |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getSortingColumn() |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Get column name |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getColumnName() |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Get column value of row item |
||
279 | * @param Row $row |
||
280 | * @return mixed |
||
281 | */ |
||
282 | public function getColumnValue(Row $row) |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getName() |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Column may have its own template |
||
299 | * @param string $template |
||
300 | */ |
||
301 | public function setTemplate($template, array $template_variables = []) |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Column can have variables that will be passed to custom template scope |
||
312 | * @return array |
||
313 | */ |
||
314 | public function getTemplateVariables() |
||
318 | |||
319 | |||
320 | /** |
||
321 | * Tell whether column has its owntemplate |
||
322 | * @return bool |
||
323 | */ |
||
324 | public function hasTemplate() |
||
328 | |||
329 | |||
330 | /** |
||
331 | * Get column template path |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getTemplate() |
||
338 | |||
339 | |||
340 | /** |
||
341 | * Tell whether data source is sorted by this collumn |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function isSortedBy() |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Tell column his sorting options |
||
352 | * @param array $sort |
||
353 | */ |
||
354 | public function setSort(array $sort) |
||
360 | |||
361 | |||
362 | /** |
||
363 | * What sorting will be applied after next click? |
||
364 | * @return array |
||
365 | */ |
||
366 | public function getSortNext() |
||
378 | |||
379 | |||
380 | /** |
||
381 | * @return bool |
||
382 | */ |
||
383 | public function hasSortNext() |
||
389 | |||
390 | |||
391 | /** |
||
392 | * Is sorting ascending? |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function isSortAsc() |
||
399 | |||
400 | |||
401 | /** |
||
402 | * Set column alignment |
||
403 | * @param string $align |
||
404 | */ |
||
405 | public function setAlign($align) |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Has column some alignment? |
||
415 | * @return bool [description] |
||
416 | */ |
||
417 | public function hasAlign() |
||
421 | |||
422 | |||
423 | /** |
||
424 | * Get column alignment |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getAlign() |
||
431 | |||
432 | |||
433 | /** |
||
434 | * Set column content fit |
||
435 | * @param bool $fit_content |
||
436 | * @return $this |
||
437 | */ |
||
438 | public function setFitContent($fit_content = true) |
||
444 | |||
445 | |||
446 | /** |
||
447 | * Set callback that will be called after inline editing |
||
448 | * @param callable $editable_callback |
||
449 | */ |
||
450 | public function setEditableCallback(callable $editable_callback) |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Return callback that is used after inline editing |
||
460 | * @return callable |
||
461 | */ |
||
462 | public function getEditableCallback() |
||
466 | |||
467 | |||
468 | /** |
||
469 | * Is column editable? |
||
470 | * @return bool |
||
471 | */ |
||
472 | public function isEditable() |
||
476 | |||
477 | |||
478 | /** |
||
479 | * Element is by default textarea, user can change that |
||
480 | * @param string $el_type |
||
481 | * @param array $attrs |
||
482 | * @return static |
||
483 | */ |
||
484 | public function setEditableInputType($el_type, array $attrs = []) |
||
490 | |||
491 | |||
492 | /** |
||
493 | * Change small inline edit input type to select |
||
494 | * @param array $options |
||
495 | * @param array $attrs |
||
496 | * @return static |
||
497 | */ |
||
498 | public function setEditableInputTypeSelect(array $options = [], array $attrs = []) |
||
512 | |||
513 | |||
514 | /** |
||
515 | * @param callable $editable_value_callback |
||
516 | * @return static |
||
517 | */ |
||
518 | public function setEditableValueCallback(callable $editable_value_callback) |
||
524 | |||
525 | |||
526 | /** |
||
527 | * @return callable|NULL |
||
528 | */ |
||
529 | public function getEditableValueCallback() |
||
533 | |||
534 | |||
535 | /** |
||
536 | * @return array |
||
537 | */ |
||
538 | public function getEditableInputType() |
||
542 | |||
543 | |||
544 | /** |
||
545 | * Set attributes for both th and td element |
||
546 | * @param array $attrs |
||
547 | * @return static |
||
548 | */ |
||
549 | public function addAttributes(array $attrs) |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Get th/td column element |
||
560 | * @param string $tag th|td |
||
561 | * @return Html |
||
562 | */ |
||
563 | public function getElementPrototype($tag) |
||
571 | |||
572 | |||
573 | /** |
||
574 | * Method called from datagrid template, set appropriate classes and another attributes |
||
575 | * @param string $tag |
||
576 | * @param string $key |
||
577 | * @param Row|NULL $row |
||
578 | * @return Html |
||
579 | */ |
||
580 | public function getElementForRender($tag, $key, Row $row = null) |
||
619 | |||
620 | |||
621 | /** |
||
622 | * @param bool $default_hide |
||
623 | * @return static |
||
624 | */ |
||
625 | public function setDefaultHide($default_hide = true) |
||
635 | |||
636 | |||
637 | public function getDefaultHide() |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
645 | * @param Row $row |
||
646 | * @param array $params_list |
||
647 | * @return array |
||
648 | */ |
||
649 | protected function getItemParams(Row $row, array $params_list) |
||
659 | |||
660 | |||
661 | /** |
||
662 | * @return string |
||
663 | */ |
||
664 | public function getColumn() |
||
668 | } |
||
669 |