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 | Column\InlineEditing; |
||
48 | |||
49 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
50 | |||
51 | const ACTION_COLUMN_NAME = '__actions__'; |
||
52 | |||
53 | /** |
||
54 | * @var Grid |
||
55 | */ |
||
56 | protected $grid; |
||
57 | |||
58 | /** |
||
59 | * Name of column. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $name; |
||
64 | |||
65 | /** |
||
66 | * Label of column. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $label; |
||
71 | |||
72 | /** |
||
73 | * Original value of column. |
||
74 | * |
||
75 | * @var mixed |
||
76 | */ |
||
77 | protected $original; |
||
78 | |||
79 | /** |
||
80 | * Attributes of column. |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $attributes = []; |
||
85 | |||
86 | /** |
||
87 | * Relation name. |
||
88 | * |
||
89 | * @var bool |
||
90 | */ |
||
91 | protected $relation = false; |
||
92 | |||
93 | /** |
||
94 | * Relation column. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $relationColumn; |
||
99 | |||
100 | /** |
||
101 | * Original grid data. |
||
102 | * |
||
103 | * @var Collection |
||
104 | */ |
||
105 | protected static $originalGridModels; |
||
106 | |||
107 | /** |
||
108 | * @var []Closure |
||
109 | */ |
||
110 | protected $displayCallbacks = []; |
||
111 | |||
112 | /** |
||
113 | * Displayers for grid column. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | public static $displayers = [ |
||
118 | 'editable' => Displayers\Editable::class, |
||
119 | 'switch' => Displayers\SwitchDisplay::class, |
||
120 | 'switchGroup' => Displayers\SwitchGroup::class, |
||
121 | 'select' => Displayers\Select::class, |
||
122 | 'image' => Displayers\Image::class, |
||
123 | 'label' => Displayers\Label::class, |
||
124 | 'button' => Displayers\Button::class, |
||
125 | 'link' => Displayers\Link::class, |
||
126 | 'badge' => Displayers\Badge::class, |
||
127 | 'progressBar' => Displayers\ProgressBar::class, |
||
128 | 'progress' => Displayers\ProgressBar::class, |
||
129 | 'radio' => Displayers\Radio::class, |
||
130 | 'checkbox' => Displayers\Checkbox::class, |
||
131 | 'orderable' => Displayers\Orderable::class, |
||
132 | 'table' => Displayers\Table::class, |
||
133 | 'expand' => Displayers\Expand::class, |
||
134 | 'modal' => Displayers\Modal::class, |
||
135 | 'carousel' => Displayers\Carousel::class, |
||
136 | 'downloadable' => Displayers\Downloadable::class, |
||
137 | 'copyable' => Displayers\Copyable::class, |
||
138 | 'qrcode' => Displayers\QRCode::class, |
||
139 | 'prefix' => Displayers\Prefix::class, |
||
140 | 'suffix' => Displayers\Suffix::class, |
||
141 | 'secret' => Displayers\Secret::class, |
||
142 | 'limit' => Displayers\Limit::class, |
||
143 | ]; |
||
144 | |||
145 | /** |
||
146 | * Defined columns. |
||
147 | * |
||
148 | * @var array |
||
149 | */ |
||
150 | public static $defined = []; |
||
151 | |||
152 | /** |
||
153 | * @var array |
||
154 | */ |
||
155 | protected static $htmlAttributes = []; |
||
156 | |||
157 | /** |
||
158 | * @var array |
||
159 | */ |
||
160 | protected static $rowAttributes = []; |
||
161 | |||
162 | /** |
||
163 | * @var Model |
||
164 | */ |
||
165 | protected static $model; |
||
166 | |||
167 | /** |
||
168 | * @var bool |
||
169 | */ |
||
170 | protected $searchable = false; |
||
171 | |||
172 | /** |
||
173 | * @param string $name |
||
174 | * @param string $label |
||
175 | */ |
||
176 | public function __construct($name, $label) |
||
177 | { |
||
178 | $this->name = $name; |
||
179 | |||
180 | $this->label = $this->formatLabel($label); |
||
181 | |||
182 | $this->initAttributes(); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Initialize column attributes. |
||
187 | */ |
||
188 | protected function initAttributes() |
||
189 | { |
||
190 | $name = str_replace('.', '-', $this->name); |
||
191 | |||
192 | $this->setAttributes(['class' => "column-{$name}"]); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Extend column displayer. |
||
197 | * |
||
198 | * @param $name |
||
199 | * @param $displayer |
||
200 | */ |
||
201 | public static function extend($name, $displayer) |
||
202 | { |
||
203 | static::$displayers[$name] = $displayer; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Define a column globally. |
||
208 | * |
||
209 | * @param string $name |
||
210 | * @param mixed $definition |
||
211 | */ |
||
212 | public static function define($name, $definition) |
||
213 | { |
||
214 | static::$defined[$name] = $definition; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Set grid instance for column. |
||
219 | * |
||
220 | * @param Grid $grid |
||
221 | */ |
||
222 | public function setGrid(Grid $grid) |
||
223 | { |
||
224 | $this->grid = $grid; |
||
225 | |||
226 | $this->setModel($grid->model()->eloquent()); |
||
|
|||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set model for column. |
||
231 | * |
||
232 | * @param $model |
||
233 | */ |
||
234 | public function setModel($model) |
||
235 | { |
||
236 | if (is_null(static::$model) && ($model instanceof BaseModel)) { |
||
237 | static::$model = $model->newInstance(); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Set original data for column. |
||
243 | * |
||
244 | * @param Collection $collection |
||
245 | */ |
||
246 | public static function setOriginalGridModels(Collection $collection) |
||
247 | { |
||
248 | static::$originalGridModels = $collection; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Set column attributes. |
||
253 | * |
||
254 | * @param array $attributes |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function setAttributes($attributes = [], $key = null) |
||
259 | { |
||
260 | if ($key) { |
||
261 | static::$rowAttributes[$this->name][$key] = array_merge( |
||
262 | Arr::get(static::$rowAttributes, "{$this->name}.{$key}", []), |
||
263 | $attributes |
||
264 | ); |
||
265 | |||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | static::$htmlAttributes[$this->name] = array_merge( |
||
270 | Arr::get(static::$htmlAttributes, $this->name, []), |
||
271 | $attributes |
||
272 | ); |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get column attributes. |
||
279 | * |
||
280 | * @param string $name |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | public static function getAttributes($name, $key = null) |
||
285 | { |
||
286 | $rowAttributes = []; |
||
287 | |||
288 | if ($key && Arr::has(static::$rowAttributes, "{$name}.{$key}")) { |
||
289 | $rowAttributes = Arr::get(static::$rowAttributes, "{$name}.{$key}", []); |
||
290 | } |
||
291 | |||
292 | $columnAttributes = Arr::get(static::$htmlAttributes, $name, []); |
||
293 | |||
294 | return array_merge($rowAttributes, $columnAttributes); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Format attributes to html. |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function formatHtmlAttributes() |
||
303 | { |
||
304 | $attrArr = []; |
||
305 | foreach (static::getAttributes($this->name) as $name => $val) { |
||
306 | $attrArr[] = "$name=\"$val\""; |
||
307 | } |
||
308 | |||
309 | return implode(' ', $attrArr); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Set style of this column. |
||
314 | * |
||
315 | * @param string $style |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function style($style) |
||
320 | { |
||
321 | return $this->setAttributes(compact('style')); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Set the width of column. |
||
326 | * |
||
327 | * @param int $width |
||
328 | * |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function width(int $width) |
||
332 | { |
||
333 | return $this->style("width: {$width}px;max-width: {$width}px;word-wrap: break-word;word-break: normal;"); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Set the color of column. |
||
338 | * |
||
339 | * @param string $color |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function color($color) |
||
344 | { |
||
345 | return $this->style("color:$color;"); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Get original column value. |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function getOriginal() |
||
354 | { |
||
355 | return $this->original; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Get name of this column. |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function getName() |
||
364 | { |
||
365 | return $this->name; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getClassName() |
||
372 | { |
||
373 | $name = str_replace('.', '-', $this->getName()); |
||
374 | |||
375 | return "column-{$name}"; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Format label. |
||
380 | * |
||
381 | * @param $label |
||
382 | * |
||
383 | * @return mixed |
||
384 | */ |
||
385 | protected function formatLabel($label) |
||
386 | { |
||
387 | if ($label) { |
||
388 | return $label; |
||
389 | } |
||
390 | |||
391 | $label = ucfirst($this->name); |
||
392 | |||
393 | return __(str_replace(['.', '_'], ' ', $label)); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Get label of the column. |
||
398 | * |
||
399 | * @return mixed |
||
400 | */ |
||
401 | public function getLabel() |
||
402 | { |
||
403 | return $this->label; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Set relation. |
||
408 | * |
||
409 | * @param string $relation |
||
410 | * @param string $relationColumn |
||
411 | * |
||
412 | * @return $this |
||
413 | */ |
||
414 | public function setRelation($relation, $relationColumn = null) |
||
415 | { |
||
416 | $this->relation = $relation; |
||
417 | $this->relationColumn = $relationColumn; |
||
418 | |||
419 | return $this; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * If this column is relation column. |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | protected function isRelation() |
||
428 | { |
||
429 | return (bool) $this->relation; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Mark this column as sortable. |
||
434 | * |
||
435 | * @param null|string $cast |
||
436 | * |
||
437 | * @return Column|string |
||
438 | */ |
||
439 | public function sortable($cast = null) |
||
440 | { |
||
441 | return $this->addSorter($cast); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Set cast name for sortable. |
||
446 | * |
||
447 | * @return $this |
||
448 | * |
||
449 | * @deprecated Use `$column->sortable($cast)` instead. |
||
450 | */ |
||
451 | public function cast($cast) |
||
452 | { |
||
453 | $this->cast = $cast; |
||
454 | |||
455 | return $this; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Set help message for column. |
||
460 | * |
||
461 | * @param string $help |
||
462 | * |
||
463 | * @return $this|string |
||
464 | */ |
||
465 | public function help($help = '') |
||
466 | { |
||
467 | return $this->addHelp($help); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Set column filter. |
||
472 | * |
||
473 | * @param mixed|null $builder |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function filter($builder = null) |
||
478 | { |
||
479 | return $this->addFilter(...func_get_args()); |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Set column as searchable. |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function searchable() |
||
488 | { |
||
489 | $this->searchable = true; |
||
490 | |||
491 | $name = $this->getName(); |
||
492 | $query = request()->query(); |
||
493 | |||
494 | $this->prefix(function ($_, $original) use ($name, $query) { |
||
495 | Arr::set($query, $name, $original); |
||
496 | |||
497 | $url = request()->fullUrlWithQuery($query); |
||
498 | |||
499 | return "<a href=\"{$url}\"><i class=\"fa fa-search\"></i></a>"; |
||
500 | }, ' '); |
||
501 | |||
502 | return $this; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Bind search query to grid model. |
||
507 | * |
||
508 | * @param Model $model |
||
509 | */ |
||
510 | public function bindSearchQuery(Model $model) |
||
511 | { |
||
512 | if ($this->searchable && ($value = request($this->getName())) != '') { |
||
513 | $model->where($this->getName(), $value); |
||
514 | } |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Add a display callback. |
||
519 | * |
||
520 | * @param Closure $callback |
||
521 | * |
||
522 | * @return $this |
||
523 | */ |
||
524 | public function display(Closure $callback) |
||
530 | |||
531 | /** |
||
532 | * Display using display abstract. |
||
533 | * |
||
534 | * @param string $abstract |
||
535 | * @param array $arguments |
||
536 | * |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function displayUsing($abstract, $arguments = []) |
||
552 | |||
553 | /** |
||
554 | * Display column using array value map. |
||
555 | * |
||
556 | * @param array $values |
||
557 | * @param null $default |
||
558 | * |
||
559 | * @return $this |
||
560 | */ |
||
561 | View Code Duplication | public function using(array $values, $default = null) |
|
571 | |||
572 | /** |
||
573 | * Replace output value with giving map. |
||
574 | * |
||
575 | * @param array $replacements |
||
576 | * |
||
577 | * @return $this |
||
578 | */ |
||
579 | public function replace(array $replacements) |
||
589 | |||
590 | /** |
||
591 | * @param string|Closure $input |
||
592 | * @param string $seperator |
||
593 | * |
||
594 | * @return $this |
||
595 | */ |
||
596 | public function repeat($input, $seperator = '') |
||
597 | { |
||
598 | if (is_string($input)) { |
||
599 | $input = function () use ($input) { |
||
600 | return $input; |
||
601 | }; |
||
602 | } |
||
603 | |||
604 | if ($input instanceof Closure) { |
||
605 | return $this->display(function ($value) use ($input, $seperator) { |
||
606 | return join($seperator, array_fill(0, (int) $value, $input->call($this, [$value]))); |
||
607 | }); |
||
608 | } |
||
609 | |||
610 | return $this; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Render this column with the given view. |
||
615 | * |
||
616 | * @param string $view |
||
617 | * |
||
618 | * @return $this |
||
619 | */ |
||
620 | public function view($view) |
||
628 | |||
629 | /** |
||
630 | * Hide this column by default. |
||
631 | * |
||
632 | * @return $this |
||
633 | */ |
||
634 | public function hide() |
||
640 | |||
641 | /** |
||
642 | * Add column to total-row. |
||
643 | * |
||
644 | * @param null $display |
||
645 | * |
||
646 | * @return $this |
||
647 | */ |
||
648 | public function totalRow($display = null) |
||
654 | |||
655 | /** |
||
656 | * Convert file size to a human readable format like `100mb`. |
||
657 | * |
||
658 | * @return $this |
||
659 | */ |
||
660 | public function filesize() |
||
666 | |||
667 | /** |
||
668 | * Display the fields in the email format as gavatar. |
||
669 | * |
||
670 | * @param int $size |
||
671 | * |
||
672 | * @return $this |
||
673 | */ |
||
674 | public function gravatar($size = 30) |
||
686 | |||
687 | /** |
||
688 | * Display field as a loading icon. |
||
689 | * |
||
690 | * @param array $values |
||
691 | * @param array $others |
||
692 | * |
||
693 | * @return $this |
||
694 | */ |
||
695 | public function loading($values = [], $others = []) |
||
707 | |||
708 | /** |
||
709 | * Display column as an font-awesome icon based on it's value. |
||
710 | * |
||
711 | * @param array $setting |
||
712 | * @param string $default |
||
713 | * |
||
714 | * @return $this |
||
715 | */ |
||
716 | public function icon(array $setting, $default = '') |
||
730 | |||
731 | /** |
||
732 | * Return a human readable format time. |
||
733 | * |
||
734 | * @param null $locale |
||
735 | * |
||
736 | * @return $this |
||
737 | */ |
||
738 | public function diffForHumans($locale = null) |
||
748 | |||
749 | /** |
||
750 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
751 | * |
||
752 | * @param array $map |
||
753 | * @param bool $default |
||
754 | * |
||
755 | * @return $this |
||
756 | */ |
||
757 | public function bool(array $map = [], $default = false) |
||
765 | |||
766 | /** |
||
767 | * Display column as a default value if empty. |
||
768 | * |
||
769 | * @param string $default |
||
770 | * @return $this |
||
771 | */ |
||
772 | public function default($default = '-') |
||
778 | |||
779 | /** |
||
780 | * Display column using a grid row action. |
||
781 | * |
||
782 | * @param string $action |
||
783 | * |
||
784 | * @return $this |
||
785 | */ |
||
786 | public function action($action) |
||
805 | |||
806 | /** |
||
807 | * Add a `dot` before column text. |
||
808 | * |
||
809 | * @param array $options |
||
810 | * @param string $default |
||
811 | * |
||
812 | * @return $this |
||
813 | */ |
||
814 | public function dot($options = [], $default = '') |
||
826 | |||
827 | /** |
||
828 | * If has display callbacks. |
||
829 | * |
||
830 | * @return bool |
||
831 | */ |
||
832 | protected function hasDisplayCallbacks() |
||
836 | |||
837 | /** |
||
838 | * Call all of the "display" callbacks column. |
||
839 | * |
||
840 | * @param mixed $value |
||
841 | * @param int $key |
||
842 | * |
||
843 | * @return mixed |
||
844 | */ |
||
845 | protected function callDisplayCallbacks($value, $key) |
||
863 | |||
864 | /** |
||
865 | * Set original grid data to column. |
||
866 | * |
||
867 | * @param Closure $callback |
||
868 | * @param int $key |
||
869 | * |
||
870 | * @return Closure |
||
871 | */ |
||
872 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
878 | |||
879 | /** |
||
880 | * Fill all data to every column. |
||
881 | * |
||
882 | * @param array $data |
||
883 | * |
||
884 | * @return mixed |
||
885 | */ |
||
886 | public function fill(array $data) |
||
907 | |||
908 | /** |
||
909 | * If current column is a defined column. |
||
910 | * |
||
911 | * @return bool |
||
912 | */ |
||
913 | protected function isDefinedColumn() |
||
917 | |||
918 | /** |
||
919 | * Use a defined column. |
||
920 | * |
||
921 | * @throws \Exception |
||
922 | */ |
||
923 | protected function useDefinedColumn() |
||
950 | |||
951 | /** |
||
952 | * Convert characters to HTML entities recursively. |
||
953 | * |
||
954 | * @param array|string $item |
||
955 | * |
||
956 | * @return mixed |
||
957 | */ |
||
958 | protected function htmlEntityEncode($item) |
||
970 | |||
971 | /** |
||
972 | * Find a displayer to display column. |
||
973 | * |
||
974 | * @param string $abstract |
||
975 | * @param array $arguments |
||
976 | * |
||
977 | * @return $this |
||
978 | */ |
||
979 | protected function resolveDisplayer($abstract, $arguments) |
||
987 | |||
988 | /** |
||
989 | * Call Illuminate/Support displayer. |
||
990 | * |
||
991 | * @param string $abstract |
||
992 | * @param array $arguments |
||
993 | * |
||
994 | * @return $this |
||
995 | */ |
||
996 | protected function callSupportDisplayer($abstract, $arguments) |
||
1010 | |||
1011 | /** |
||
1012 | * Call Builtin displayer. |
||
1013 | * |
||
1014 | * @param string $abstract |
||
1015 | * @param array $arguments |
||
1016 | * |
||
1017 | * @return $this |
||
1018 | */ |
||
1019 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
1041 | |||
1042 | /** |
||
1043 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
1044 | * |
||
1045 | * Allow fluent calls on the Column object. |
||
1046 | * |
||
1047 | * @param string $method |
||
1048 | * @param array $arguments |
||
1049 | * |
||
1050 | * @return $this |
||
1051 | */ |
||
1052 | public function __call($method, $arguments) |
||
1065 | } |
||
1066 |
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: