Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
42 | class Column |
||
43 | { |
||
44 | use Column\HasHeader; |
||
45 | |||
46 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
47 | |||
48 | const ACTION_COLUMN_NAME = '__actions__'; |
||
49 | |||
50 | /** |
||
51 | * @var Grid |
||
52 | */ |
||
53 | protected $grid; |
||
54 | |||
55 | /** |
||
56 | * Name of column. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $name; |
||
61 | |||
62 | /** |
||
63 | * Label of column. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $label; |
||
68 | |||
69 | /** |
||
70 | * Original value of column. |
||
71 | * |
||
72 | * @var mixed |
||
73 | */ |
||
74 | protected $original; |
||
75 | |||
76 | /** |
||
77 | * Attributes of column. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $attributes = []; |
||
82 | |||
83 | /** |
||
84 | * Relation name. |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $relation = false; |
||
89 | |||
90 | /** |
||
91 | * Relation column. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $relationColumn; |
||
96 | |||
97 | /** |
||
98 | * Original grid data. |
||
99 | * |
||
100 | * @var Collection |
||
101 | */ |
||
102 | protected static $originalGridModels; |
||
103 | |||
104 | /** |
||
105 | * @var []Closure |
||
106 | */ |
||
107 | protected $displayCallbacks = []; |
||
108 | |||
109 | /** |
||
110 | * Displayers for grid column. |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | public static $displayers = [ |
||
115 | 'editable' => Displayers\Editable::class, |
||
116 | 'switch' => Displayers\SwitchDisplay::class, |
||
117 | 'switchGroup' => Displayers\SwitchGroup::class, |
||
118 | 'select' => Displayers\Select::class, |
||
119 | 'image' => Displayers\Image::class, |
||
120 | 'label' => Displayers\Label::class, |
||
121 | 'button' => Displayers\Button::class, |
||
122 | 'link' => Displayers\Link::class, |
||
123 | 'badge' => Displayers\Badge::class, |
||
124 | 'progressBar' => Displayers\ProgressBar::class, |
||
125 | 'progress' => Displayers\ProgressBar::class, |
||
126 | 'radio' => Displayers\Radio::class, |
||
127 | 'checkbox' => Displayers\Checkbox::class, |
||
128 | 'orderable' => Displayers\Orderable::class, |
||
129 | 'table' => Displayers\Table::class, |
||
130 | 'expand' => Displayers\Expand::class, |
||
131 | 'modal' => Displayers\Modal::class, |
||
132 | 'carousel' => Displayers\Carousel::class, |
||
133 | 'downloadable'=> Displayers\Downloadable::class, |
||
134 | 'copyable' => Displayers\Copyable::class, |
||
135 | 'qrcode' => Displayers\QRCode::class, |
||
136 | 'prefix' => Displayers\Prefix::class, |
||
137 | 'suffix' => Displayers\Suffix::class, |
||
138 | ]; |
||
139 | |||
140 | /** |
||
141 | * Defined columns. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | public static $defined = []; |
||
146 | |||
147 | /** |
||
148 | * @var array |
||
149 | */ |
||
150 | protected static $htmlAttributes = []; |
||
151 | |||
152 | /** |
||
153 | * @var array |
||
154 | */ |
||
155 | protected static $rowAttributes = []; |
||
156 | |||
157 | /** |
||
158 | * @var Model |
||
159 | */ |
||
160 | protected static $model; |
||
161 | |||
162 | /** |
||
163 | * @var bool |
||
164 | */ |
||
165 | protected $searchable = false; |
||
166 | |||
167 | /** |
||
168 | * @param string $name |
||
169 | * @param string $label |
||
170 | */ |
||
171 | public function __construct($name, $label) |
||
179 | |||
180 | /** |
||
181 | * Initialize column attributes. |
||
182 | */ |
||
183 | protected function initAttributes() |
||
189 | |||
190 | /** |
||
191 | * Extend column displayer. |
||
192 | * |
||
193 | * @param $name |
||
194 | * @param $displayer |
||
195 | */ |
||
196 | public static function extend($name, $displayer) |
||
200 | |||
201 | /** |
||
202 | * Define a column globally. |
||
203 | * |
||
204 | * @param string $name |
||
205 | * @param mixed $definition |
||
206 | */ |
||
207 | public static function define($name, $definition) |
||
211 | |||
212 | /** |
||
213 | * Set grid instance for column. |
||
214 | * |
||
215 | * @param Grid $grid |
||
216 | */ |
||
217 | public function setGrid(Grid $grid) |
||
223 | |||
224 | /** |
||
225 | * Set model for column. |
||
226 | * |
||
227 | * @param $model |
||
228 | */ |
||
229 | public function setModel($model) |
||
235 | |||
236 | /** |
||
237 | * Set original data for column. |
||
238 | * |
||
239 | * @param Collection $collection |
||
240 | */ |
||
241 | public static function setOriginalGridModels(Collection $collection) |
||
245 | |||
246 | /** |
||
247 | * Set column attributes. |
||
248 | * |
||
249 | * @param array $attributes |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setAttributes($attributes = [], $key = null) |
||
271 | |||
272 | /** |
||
273 | * Get column attributes. |
||
274 | * |
||
275 | * @param string $name |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | public static function getAttributes($name, $key = null) |
||
291 | |||
292 | /** |
||
293 | * Format attributes to html. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | public function formatHtmlAttributes() |
||
306 | |||
307 | /** |
||
308 | * Set style of this column. |
||
309 | * |
||
310 | * @param string $style |
||
311 | * |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function style($style) |
||
318 | |||
319 | /** |
||
320 | * Set the width of column. |
||
321 | * |
||
322 | * @param int $width |
||
323 | * |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function width(int $width) |
||
330 | |||
331 | /** |
||
332 | * Set the color of column. |
||
333 | * |
||
334 | * @param string $color |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function color($color) |
||
342 | |||
343 | /** |
||
344 | * Get original column value. |
||
345 | * |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function getOriginal() |
||
352 | |||
353 | /** |
||
354 | * Get name of this column. |
||
355 | * |
||
356 | * @return mixed |
||
357 | */ |
||
358 | public function getName() |
||
362 | |||
363 | /** |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getClassName() |
||
372 | |||
373 | /** |
||
374 | * Format label. |
||
375 | * |
||
376 | * @param $label |
||
377 | * |
||
378 | * @return mixed |
||
379 | */ |
||
380 | protected function formatLabel($label) |
||
390 | |||
391 | /** |
||
392 | * Get label of the column. |
||
393 | * |
||
394 | * @return mixed |
||
395 | */ |
||
396 | public function getLabel() |
||
400 | |||
401 | /** |
||
402 | * Set relation. |
||
403 | * |
||
404 | * @param string $relation |
||
405 | * @param string $relationColumn |
||
406 | * |
||
407 | * @return $this |
||
408 | */ |
||
409 | public function setRelation($relation, $relationColumn = null) |
||
416 | |||
417 | /** |
||
418 | * If this column is relation column. |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | protected function isRelation() |
||
426 | |||
427 | /** |
||
428 | * Mark this column as sortable. |
||
429 | * |
||
430 | * @param null|string $cast |
||
431 | * |
||
432 | * @return Column|string |
||
433 | */ |
||
434 | public function sortable($cast = null) |
||
438 | |||
439 | /** |
||
440 | * Set cast name for sortable. |
||
441 | * |
||
442 | * @return $this |
||
443 | * |
||
444 | * @deprecated Use `$column->sortable($cast)` instead. |
||
445 | */ |
||
446 | public function cast($cast) |
||
452 | |||
453 | /** |
||
454 | * Set help message for column. |
||
455 | * |
||
456 | * @param string $help |
||
457 | * |
||
458 | * @return $this|string |
||
459 | */ |
||
460 | public function help($help = '') |
||
464 | |||
465 | /** |
||
466 | * Set column filter. |
||
467 | * |
||
468 | * @param null $builder |
||
469 | * |
||
470 | * @return $this |
||
471 | */ |
||
472 | public function filter($builder = null) |
||
476 | |||
477 | /** |
||
478 | * Set column as searchable. |
||
479 | * |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function searchable() |
||
499 | |||
500 | /** |
||
501 | * Bind search query to grid model. |
||
502 | * |
||
503 | * @param Model $model |
||
504 | */ |
||
505 | public function bindSearchQuery(Model $model) |
||
513 | |||
514 | /** |
||
515 | * Add a display callback. |
||
516 | * |
||
517 | * @param Closure $callback |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function display(Closure $callback) |
||
527 | |||
528 | /** |
||
529 | * Display using display abstract. |
||
530 | * |
||
531 | * @param string $abstract |
||
532 | * @param array $arguments |
||
533 | * |
||
534 | * @return $this |
||
535 | */ |
||
536 | public function displayUsing($abstract, $arguments = []) |
||
549 | |||
550 | /** |
||
551 | * Display column using array value map. |
||
552 | * |
||
553 | * @param array $values |
||
554 | * @param null $default |
||
555 | * |
||
556 | * @return $this |
||
557 | */ |
||
558 | View Code Duplication | public function using(array $values, $default = null) |
|
568 | |||
569 | /** |
||
570 | * Replace output value with giving map. |
||
571 | * |
||
572 | * @param array $replacements |
||
573 | * |
||
574 | * @return $this |
||
575 | */ |
||
576 | public function replace(array $replacements) |
||
586 | |||
587 | /** |
||
588 | * Render this column with the given view. |
||
589 | * |
||
590 | * @param string $view |
||
591 | * |
||
592 | * @return $this |
||
593 | */ |
||
594 | public function view($view) |
||
602 | |||
603 | /** |
||
604 | * Hide this column by default. |
||
605 | * |
||
606 | * @return $this |
||
607 | */ |
||
608 | public function hide() |
||
614 | |||
615 | /** |
||
616 | * Add column to total-row. |
||
617 | * |
||
618 | * @param null $display |
||
619 | * |
||
620 | * @return $this |
||
621 | */ |
||
622 | public function totalRow($display = null) |
||
628 | |||
629 | /** |
||
630 | * Convert file size to a human readable format like `100mb`. |
||
631 | * |
||
632 | * @return $this |
||
633 | */ |
||
634 | public function filesize() |
||
640 | |||
641 | /** |
||
642 | * Display the fields in the email format as gavatar. |
||
643 | * |
||
644 | * @param int $size |
||
645 | * |
||
646 | * @return $this |
||
647 | */ |
||
648 | public function gravatar($size = 30) |
||
660 | |||
661 | /** |
||
662 | * Display field as a loading icon. |
||
663 | * |
||
664 | * @param array $values |
||
665 | * @param array $others |
||
666 | * |
||
667 | * @return $this |
||
668 | */ |
||
669 | public function loading($values = [], $others = []) |
||
681 | |||
682 | /** |
||
683 | * Display column as an font-awesome icon based on it's value. |
||
684 | * |
||
685 | * @param array $setting |
||
686 | * @param string $default |
||
687 | * |
||
688 | * @return $this |
||
689 | */ |
||
690 | public function icon(array $setting, $default = '') |
||
704 | |||
705 | /** |
||
706 | * Return a human readable format time. |
||
707 | * |
||
708 | * @param null $locale |
||
709 | * |
||
710 | * @return $this |
||
711 | */ |
||
712 | public function diffForHumans($locale = null) |
||
722 | |||
723 | /** |
||
724 | * Returns a string formatted according to the given format string. |
||
725 | * |
||
726 | * @param string $format |
||
727 | * |
||
728 | * @return $this |
||
729 | */ |
||
730 | public function date($format) |
||
736 | |||
737 | /** |
||
738 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
739 | * |
||
740 | * @param array $map |
||
741 | * @param bool $default |
||
742 | * |
||
743 | * @return $this |
||
744 | */ |
||
745 | public function bool(array $map = [], $default = false) |
||
753 | |||
754 | /** |
||
755 | * Display column using a grid row action. |
||
756 | * |
||
757 | * @param string $action |
||
758 | * |
||
759 | * @return $this |
||
760 | */ |
||
761 | public function action($action) |
||
780 | |||
781 | /** |
||
782 | * Add a `dot` before column text. |
||
783 | * |
||
784 | * @param array $options |
||
785 | * @param string $default |
||
786 | * |
||
787 | * @return $this |
||
788 | */ |
||
789 | public function dot($options = [], $default = '') |
||
801 | |||
802 | /** |
||
803 | * If has display callbacks. |
||
804 | * |
||
805 | * @return bool |
||
806 | */ |
||
807 | protected function hasDisplayCallbacks() |
||
811 | |||
812 | /** |
||
813 | * Call all of the "display" callbacks column. |
||
814 | * |
||
815 | * @param mixed $value |
||
816 | * @param int $key |
||
817 | * |
||
818 | * @return mixed |
||
819 | */ |
||
820 | protected function callDisplayCallbacks($value, $key) |
||
838 | |||
839 | /** |
||
840 | * Set original grid data to column. |
||
841 | * |
||
842 | * @param Closure $callback |
||
843 | * @param int $key |
||
844 | * |
||
845 | * @return Closure |
||
846 | */ |
||
847 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
853 | |||
854 | /** |
||
855 | * Fill all data to every column. |
||
856 | * |
||
857 | * @param array $data |
||
858 | * |
||
859 | * @return mixed |
||
860 | */ |
||
861 | public function fill(array $data) |
||
882 | |||
883 | /** |
||
884 | * If current column is a defined column. |
||
885 | * |
||
886 | * @return bool |
||
887 | */ |
||
888 | protected function isDefinedColumn() |
||
892 | |||
893 | /** |
||
894 | * Use a defined column. |
||
895 | * |
||
896 | * @throws \Exception |
||
897 | */ |
||
898 | protected function useDefinedColumn() |
||
925 | |||
926 | /** |
||
927 | * Convert characters to HTML entities recursively. |
||
928 | * |
||
929 | * @param array|string $item |
||
930 | * |
||
931 | * @return mixed |
||
932 | */ |
||
933 | protected function htmlEntityEncode($item) |
||
945 | |||
946 | /** |
||
947 | * Find a displayer to display column. |
||
948 | * |
||
949 | * @param string $abstract |
||
950 | * @param array $arguments |
||
951 | * |
||
952 | * @return $this |
||
953 | */ |
||
954 | protected function resolveDisplayer($abstract, $arguments) |
||
962 | |||
963 | /** |
||
964 | * Call Illuminate/Support displayer. |
||
965 | * |
||
966 | * @param string $abstract |
||
967 | * @param array $arguments |
||
968 | * |
||
969 | * @return $this |
||
970 | */ |
||
971 | protected function callSupportDisplayer($abstract, $arguments) |
||
985 | |||
986 | /** |
||
987 | * Call Builtin displayer. |
||
988 | * |
||
989 | * @param string $abstract |
||
990 | * @param array $arguments |
||
991 | * |
||
992 | * @return $this |
||
993 | */ |
||
994 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
1016 | |||
1017 | /** |
||
1018 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
1019 | * |
||
1020 | * Allow fluent calls on the Column object. |
||
1021 | * |
||
1022 | * @param string $method |
||
1023 | * @param array $arguments |
||
1024 | * |
||
1025 | * @return $this |
||
1026 | */ |
||
1027 | public function __call($method, $arguments) |
||
1040 | } |
||
1041 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.