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 |
||
44 | class Column |
||
45 | { |
||
46 | use Column\HasHeader; |
||
47 | |||
48 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
49 | |||
50 | const ACTION_COLUMN_NAME = '__actions__'; |
||
51 | |||
52 | /** |
||
53 | * @var Grid |
||
54 | */ |
||
55 | protected $grid; |
||
56 | |||
57 | /** |
||
58 | * Name of column. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $name; |
||
63 | |||
64 | /** |
||
65 | * Label of column. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $label; |
||
70 | |||
71 | /** |
||
72 | * Original value of column. |
||
73 | * |
||
74 | * @var mixed |
||
75 | */ |
||
76 | protected $original; |
||
77 | |||
78 | /** |
||
79 | * Attributes of column. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $attributes = []; |
||
84 | |||
85 | /** |
||
86 | * Relation name. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | protected $relation = false; |
||
91 | |||
92 | /** |
||
93 | * Relation column. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $relationColumn; |
||
98 | |||
99 | /** |
||
100 | * Original grid data. |
||
101 | * |
||
102 | * @var Collection |
||
103 | */ |
||
104 | protected static $originalGridModels; |
||
105 | |||
106 | /** |
||
107 | * @var []Closure |
||
108 | */ |
||
109 | protected $displayCallbacks = []; |
||
110 | |||
111 | /** |
||
112 | * Displayers for grid column. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | public static $displayers = [ |
||
117 | 'editable' => Displayers\Editable::class, |
||
118 | 'switch' => Displayers\SwitchDisplay::class, |
||
119 | 'switchGroup' => Displayers\SwitchGroup::class, |
||
120 | 'select' => Displayers\Select::class, |
||
121 | 'image' => Displayers\Image::class, |
||
122 | 'label' => Displayers\Label::class, |
||
123 | 'button' => Displayers\Button::class, |
||
124 | 'link' => Displayers\Link::class, |
||
125 | 'badge' => Displayers\Badge::class, |
||
126 | 'progressBar' => Displayers\ProgressBar::class, |
||
127 | 'progress' => Displayers\ProgressBar::class, |
||
128 | 'radio' => Displayers\Radio::class, |
||
129 | 'checkbox' => Displayers\Checkbox::class, |
||
130 | 'orderable' => Displayers\Orderable::class, |
||
131 | 'table' => Displayers\Table::class, |
||
132 | 'expand' => Displayers\Expand::class, |
||
133 | 'modal' => Displayers\Modal::class, |
||
134 | 'carousel' => Displayers\Carousel::class, |
||
135 | 'downloadable' => Displayers\Downloadable::class, |
||
136 | 'copyable' => Displayers\Copyable::class, |
||
137 | 'qrcode' => Displayers\QRCode::class, |
||
138 | 'prefix' => Displayers\Prefix::class, |
||
139 | 'suffix' => Displayers\Suffix::class, |
||
140 | 'secret' => Displayers\Secret::class, |
||
141 | 'limit' => Displayers\Limit::class, |
||
142 | 'belongsTo' => Displayers\BelongsTo::class, |
||
143 | 'belongsToMany' => Displayers\BelongsToMany::class, |
||
144 | ]; |
||
145 | |||
146 | /** |
||
147 | * Defined columns. |
||
148 | * |
||
149 | * @var array |
||
150 | */ |
||
151 | public static $defined = []; |
||
152 | |||
153 | /** |
||
154 | * @var array |
||
155 | */ |
||
156 | protected static $htmlAttributes = []; |
||
157 | |||
158 | /** |
||
159 | * @var array |
||
160 | */ |
||
161 | protected static $rowAttributes = []; |
||
162 | |||
163 | /** |
||
164 | * @var Model |
||
165 | */ |
||
166 | protected static $model; |
||
167 | |||
168 | /** |
||
169 | * @var bool |
||
170 | */ |
||
171 | protected $searchable = false; |
||
172 | |||
173 | /** |
||
174 | * @param string $name |
||
175 | * @param string $label |
||
176 | */ |
||
177 | public function __construct($name, $label) |
||
178 | { |
||
179 | $this->name = $name; |
||
180 | |||
181 | $this->label = $this->formatLabel($label); |
||
182 | |||
183 | $this->initAttributes(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Initialize column attributes. |
||
188 | */ |
||
189 | protected function initAttributes() |
||
190 | { |
||
191 | $name = str_replace('.', '-', $this->name); |
||
192 | |||
193 | $this->setAttributes(['class' => "column-{$name}"]); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Extend column displayer. |
||
198 | * |
||
199 | * @param $name |
||
200 | * @param $displayer |
||
201 | */ |
||
202 | public static function extend($name, $displayer) |
||
203 | { |
||
204 | static::$displayers[$name] = $displayer; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Define a column globally. |
||
209 | * |
||
210 | * @param string $name |
||
211 | * @param mixed $definition |
||
212 | */ |
||
213 | public static function define($name, $definition) |
||
214 | { |
||
215 | static::$defined[$name] = $definition; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Set grid instance for column. |
||
220 | * |
||
221 | * @param Grid $grid |
||
222 | */ |
||
223 | public function setGrid(Grid $grid) |
||
224 | { |
||
225 | $this->grid = $grid; |
||
226 | |||
227 | $this->setModel($grid->model()->eloquent()); |
||
|
|||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Set model for column. |
||
232 | * |
||
233 | * @param $model |
||
234 | */ |
||
235 | public function setModel($model) |
||
236 | { |
||
237 | if (is_null(static::$model) && ($model instanceof BaseModel)) { |
||
238 | static::$model = $model->newInstance(); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Set original data for column. |
||
244 | * |
||
245 | * @param Collection $collection |
||
246 | */ |
||
247 | public static function setOriginalGridModels(Collection $collection) |
||
248 | { |
||
249 | static::$originalGridModels = $collection; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Set column attributes. |
||
254 | * |
||
255 | * @param array $attributes |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function setAttributes($attributes = [], $key = null) |
||
260 | { |
||
261 | if ($key) { |
||
262 | static::$rowAttributes[$this->name][$key] = array_merge( |
||
263 | Arr::get(static::$rowAttributes, "{$this->name}.{$key}", []), |
||
264 | $attributes |
||
265 | ); |
||
266 | |||
267 | return $this; |
||
268 | } |
||
269 | |||
270 | static::$htmlAttributes[$this->name] = array_merge( |
||
271 | Arr::get(static::$htmlAttributes, $this->name, []), |
||
272 | $attributes |
||
273 | ); |
||
274 | |||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Get column attributes. |
||
280 | * |
||
281 | * @param string $name |
||
282 | * |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public static function getAttributes($name, $key = null) |
||
286 | { |
||
287 | $rowAttributes = []; |
||
288 | |||
289 | if ($key && Arr::has(static::$rowAttributes, "{$name}.{$key}")) { |
||
290 | $rowAttributes = Arr::get(static::$rowAttributes, "{$name}.{$key}", []); |
||
291 | } |
||
292 | |||
293 | $columnAttributes = Arr::get(static::$htmlAttributes, $name, []); |
||
294 | |||
295 | return array_merge($rowAttributes, $columnAttributes); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Format attributes to html. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function formatHtmlAttributes() |
||
304 | { |
||
305 | $attrArr = []; |
||
306 | foreach (static::getAttributes($this->name) as $name => $val) { |
||
307 | $attrArr[] = "$name=\"$val\""; |
||
308 | } |
||
309 | |||
310 | return implode(' ', $attrArr); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Set style of this column. |
||
315 | * |
||
316 | * @param string $style |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function style($style) |
||
321 | { |
||
322 | return $this->setAttributes(compact('style')); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Set the width of column. |
||
327 | * |
||
328 | * @param int $width |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function width(int $width) |
||
333 | { |
||
334 | return $this->style("width: {$width}px;max-width: {$width}px;word-wrap: break-word;word-break: normal;"); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Set the color of column. |
||
339 | * |
||
340 | * @param string $color |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function color($color) |
||
345 | { |
||
346 | return $this->style("color:$color;"); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Get original column value. |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function getOriginal() |
||
355 | { |
||
356 | return $this->original; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get name of this column. |
||
361 | * |
||
362 | * @return mixed |
||
363 | */ |
||
364 | public function getName() |
||
365 | { |
||
366 | return $this->name; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return string |
||
371 | */ |
||
372 | public function getClassName() |
||
373 | { |
||
374 | $name = str_replace('.', '-', $this->getName()); |
||
375 | |||
376 | return "column-{$name}"; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Format label. |
||
381 | * |
||
382 | * @param $label |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | protected function formatLabel($label) |
||
387 | { |
||
388 | if ($label) { |
||
389 | return $label; |
||
390 | } |
||
391 | |||
392 | $label = ucfirst($this->name); |
||
393 | |||
394 | return __(str_replace(['.', '_'], ' ', $label)); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get label of the column. |
||
399 | * |
||
400 | * @return mixed |
||
401 | */ |
||
402 | public function getLabel() |
||
403 | { |
||
404 | return $this->label; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Set relation. |
||
409 | * |
||
410 | * @param string $relation |
||
411 | * @param string $relationColumn |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setRelation($relation, $relationColumn = null) |
||
416 | { |
||
417 | $this->relation = $relation; |
||
418 | $this->relationColumn = $relationColumn; |
||
419 | |||
420 | return $this; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * If this column is relation column. |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected function isRelation() |
||
429 | { |
||
430 | return (bool) $this->relation; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Mark this column as sortable. |
||
435 | * |
||
436 | * @param null|string $cast |
||
437 | * |
||
438 | * @return Column|string |
||
439 | */ |
||
440 | public function sortable($cast = null) |
||
441 | { |
||
442 | return $this->addSorter($cast); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Set cast name for sortable. |
||
447 | * |
||
448 | * @return $this |
||
449 | * |
||
450 | * @deprecated Use `$column->sortable($cast)` instead. |
||
451 | */ |
||
452 | public function cast($cast) |
||
453 | { |
||
454 | $this->cast = $cast; |
||
455 | |||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Set help message for column. |
||
461 | * |
||
462 | * @param string $help |
||
463 | * |
||
464 | * @return $this|string |
||
465 | */ |
||
466 | public function help($help = '') |
||
467 | { |
||
468 | return $this->addHelp($help); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Set column filter. |
||
473 | * |
||
474 | * @param mixed|null $builder |
||
475 | * |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function filter($builder = null) |
||
479 | { |
||
480 | return $this->addFilter(...func_get_args()); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Set column as searchable. |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function searchable() |
||
489 | { |
||
490 | $this->searchable = true; |
||
491 | |||
492 | $name = $this->getName(); |
||
493 | $query = request()->query(); |
||
494 | |||
495 | $this->prefix(function ($_, $original) use ($name, $query) { |
||
496 | Arr::set($query, $name, $original); |
||
497 | |||
498 | $url = request()->fullUrlWithQuery($query); |
||
499 | |||
500 | return "<a href=\"{$url}\"><i class=\"fa fa-search\"></i></a>"; |
||
501 | }, ' '); |
||
502 | |||
503 | return $this; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Bind search query to grid model. |
||
508 | * |
||
509 | * @param Model $model |
||
510 | */ |
||
511 | public function bindSearchQuery(Model $model) |
||
512 | { |
||
513 | if ($this->searchable && ($value = request($this->getName())) != '') { |
||
514 | $model->where($this->getName(), $value); |
||
515 | } |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Add a display callback. |
||
520 | * |
||
521 | * @param Closure $callback |
||
522 | * |
||
523 | * @return $this |
||
524 | */ |
||
525 | public function display(Closure $callback) |
||
531 | |||
532 | /** |
||
533 | * Display using display abstract. |
||
534 | * |
||
535 | * @param string $abstract |
||
536 | * @param array $arguments |
||
537 | * |
||
538 | * @return $this |
||
539 | */ |
||
540 | public function displayUsing($abstract, $arguments = []) |
||
553 | |||
554 | /** |
||
555 | * Display column using array value map. |
||
556 | * |
||
557 | * @param array $values |
||
558 | * @param null $default |
||
559 | * |
||
560 | * @return $this |
||
561 | */ |
||
562 | View Code Duplication | public function using(array $values, $default = null) |
|
572 | |||
573 | /** |
||
574 | * Replace output value with giving map. |
||
575 | * |
||
576 | * @param array $replacements |
||
577 | * |
||
578 | * @return $this |
||
579 | */ |
||
580 | public function replace(array $replacements) |
||
590 | |||
591 | /** |
||
592 | * @param string|Closure $input |
||
593 | * @param string $seperator |
||
594 | * |
||
595 | * @return $this |
||
596 | */ |
||
597 | public function repeat($input, $seperator = '') |
||
598 | { |
||
599 | if (is_string($input)) { |
||
600 | $input = function () use ($input) { |
||
601 | return $input; |
||
602 | }; |
||
603 | } |
||
604 | |||
605 | if ($input instanceof Closure) { |
||
606 | return $this->display(function ($value) use ($input, $seperator) { |
||
607 | $callable = $input->bindTo($this); |
||
608 | return join($seperator, array_fill(0, (int) $value, $callable($value))); |
||
609 | }); |
||
610 | } |
||
611 | |||
612 | return $this; |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Render this column with the given view. |
||
617 | * |
||
618 | * @param string $view |
||
619 | * |
||
620 | * @return $this |
||
621 | */ |
||
622 | public function view($view) |
||
630 | |||
631 | /** |
||
632 | * Hide this column by default. |
||
633 | * |
||
634 | * @return $this |
||
635 | */ |
||
636 | public function hide() |
||
642 | |||
643 | /** |
||
644 | * Add column to total-row. |
||
645 | * |
||
646 | * @param null $display |
||
647 | * |
||
648 | * @return $this |
||
649 | */ |
||
650 | public function totalRow($display = null) |
||
656 | |||
657 | /** |
||
658 | * Convert file size to a human readable format like `100mb`. |
||
659 | * |
||
660 | * @return $this |
||
661 | */ |
||
662 | public function filesize() |
||
668 | |||
669 | /** |
||
670 | * Display the fields in the email format as gavatar. |
||
671 | * |
||
672 | * @param int $size |
||
673 | * |
||
674 | * @return $this |
||
675 | */ |
||
676 | public function gravatar($size = 30) |
||
688 | |||
689 | /** |
||
690 | * Display field as a loading icon. |
||
691 | * |
||
692 | * @param array $values |
||
693 | * @param array $others |
||
694 | * |
||
695 | * @return $this |
||
696 | */ |
||
697 | public function loading($values = [], $others = []) |
||
709 | |||
710 | /** |
||
711 | * Display column as an font-awesome icon based on it's value. |
||
712 | * |
||
713 | * @param array $setting |
||
714 | * @param string $default |
||
715 | * |
||
716 | * @return $this |
||
717 | */ |
||
718 | public function icon(array $setting, $default = '') |
||
732 | |||
733 | /** |
||
734 | * Return a human readable format time. |
||
735 | * |
||
736 | * @param null $locale |
||
737 | * |
||
738 | * @return $this |
||
739 | */ |
||
740 | public function diffForHumans($locale = null) |
||
750 | |||
751 | /** |
||
752 | * Returns a string formatted according to the given format string. |
||
753 | * |
||
754 | * @param string $format |
||
755 | * |
||
756 | * @return $this |
||
757 | */ |
||
758 | public function date($format) |
||
764 | |||
765 | /** |
||
766 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
767 | * |
||
768 | * @param array $map |
||
769 | * @param bool $default |
||
770 | * |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function bool(array $map = [], $default = false) |
||
781 | |||
782 | /** |
||
783 | * Display column as a default value if empty. |
||
784 | * |
||
785 | * @param string $default |
||
786 | * @return $this |
||
787 | */ |
||
788 | public function default($default = '-') |
||
794 | |||
795 | /** |
||
796 | * Display column using a grid row action. |
||
797 | * |
||
798 | * @param string $action |
||
799 | * |
||
800 | * @return $this |
||
801 | */ |
||
802 | public function action($action) |
||
821 | |||
822 | /** |
||
823 | * Add a `dot` before column text. |
||
824 | * |
||
825 | * @param array $options |
||
826 | * @param string $default |
||
827 | * |
||
828 | * @return $this |
||
829 | */ |
||
830 | public function dot($options = [], $default = '') |
||
842 | |||
843 | /** |
||
844 | * @param string $selectable |
||
845 | * |
||
846 | * @return $this |
||
847 | */ |
||
848 | View Code Duplication | public function belongsTo($selectable) |
|
856 | |||
857 | /** |
||
858 | * @param string $selectable |
||
859 | * |
||
860 | * @return $this |
||
861 | */ |
||
862 | View Code Duplication | public function belongsToMany($selectable) |
|
870 | |||
871 | /** |
||
872 | * If has display callbacks. |
||
873 | * |
||
874 | * @return bool |
||
875 | */ |
||
876 | protected function hasDisplayCallbacks() |
||
880 | |||
881 | /** |
||
882 | * Call all of the "display" callbacks column. |
||
883 | * |
||
884 | * @param mixed $value |
||
885 | * @param int $key |
||
886 | * |
||
887 | * @return mixed |
||
888 | */ |
||
889 | protected function callDisplayCallbacks($value, $key) |
||
907 | |||
908 | /** |
||
909 | * Set original grid data to column. |
||
910 | * |
||
911 | * @param Closure $callback |
||
912 | * @param int $key |
||
913 | * |
||
914 | * @return Closure |
||
915 | */ |
||
916 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
922 | |||
923 | /** |
||
924 | * Fill all data to every column. |
||
925 | * |
||
926 | * @param array $data |
||
927 | * |
||
928 | * @return mixed |
||
929 | */ |
||
930 | public function fill(array $data) |
||
951 | |||
952 | /** |
||
953 | * If current column is a defined column. |
||
954 | * |
||
955 | * @return bool |
||
956 | */ |
||
957 | protected function isDefinedColumn() |
||
961 | |||
962 | /** |
||
963 | * Use a defined column. |
||
964 | * |
||
965 | * @throws \Exception |
||
966 | */ |
||
967 | protected function useDefinedColumn() |
||
994 | |||
995 | /** |
||
996 | * Convert characters to HTML entities recursively. |
||
997 | * |
||
998 | * @param array|string $item |
||
999 | * |
||
1000 | * @return mixed |
||
1001 | */ |
||
1002 | protected function htmlEntityEncode($item) |
||
1014 | |||
1015 | /** |
||
1016 | * Find a displayer to display column. |
||
1017 | * |
||
1018 | * @param string $abstract |
||
1019 | * @param array $arguments |
||
1020 | * |
||
1021 | * @return $this |
||
1022 | */ |
||
1023 | protected function resolveDisplayer($abstract, $arguments) |
||
1031 | |||
1032 | /** |
||
1033 | * Call Illuminate/Support displayer. |
||
1034 | * |
||
1035 | * @param string $abstract |
||
1036 | * @param array $arguments |
||
1037 | * |
||
1038 | * @return $this |
||
1039 | */ |
||
1040 | protected function callSupportDisplayer($abstract, $arguments) |
||
1054 | |||
1055 | /** |
||
1056 | * Call Builtin displayer. |
||
1057 | * |
||
1058 | * @param string $abstract |
||
1059 | * @param array $arguments |
||
1060 | * |
||
1061 | * @return $this |
||
1062 | */ |
||
1063 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
1085 | |||
1086 | /** |
||
1087 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
1088 | * |
||
1089 | * Allow fluent calls on the Column object. |
||
1090 | * |
||
1091 | * @param string $method |
||
1092 | * @param array $arguments |
||
1093 | * |
||
1094 | * @return $this |
||
1095 | */ |
||
1096 | public function __call($method, $arguments) |
||
1109 | } |
||
1110 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: