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 |
||
| 41 | class Column |
||
| 42 | { |
||
| 43 | use Column\HasHeader; |
||
| 44 | |||
| 45 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 46 | |||
| 47 | const ACTION_COLUMN_NAME = '__actions__'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Grid |
||
| 51 | */ |
||
| 52 | protected $grid; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Name of column. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $name; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Label of column. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $label; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Original value of column. |
||
| 70 | * |
||
| 71 | * @var mixed |
||
| 72 | */ |
||
| 73 | protected $original; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Attributes of column. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $attributes = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Relation name. |
||
| 84 | * |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $relation = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Relation column. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $relationColumn; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Original grid data. |
||
| 98 | * |
||
| 99 | * @var Collection |
||
| 100 | */ |
||
| 101 | protected static $originalGridModels; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var []Closure |
||
| 105 | */ |
||
| 106 | protected $displayCallbacks = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Displayers for grid column. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | public static $displayers = [ |
||
| 114 | 'editable' => Displayers\Editable::class, |
||
| 115 | 'switch' => Displayers\SwitchDisplay::class, |
||
| 116 | 'switchGroup' => Displayers\SwitchGroup::class, |
||
| 117 | 'select' => Displayers\Select::class, |
||
| 118 | 'image' => Displayers\Image::class, |
||
| 119 | 'label' => Displayers\Label::class, |
||
| 120 | 'button' => Displayers\Button::class, |
||
| 121 | 'link' => Displayers\Link::class, |
||
| 122 | 'badge' => Displayers\Badge::class, |
||
| 123 | 'progressBar' => Displayers\ProgressBar::class, |
||
| 124 | 'progress' => Displayers\ProgressBar::class, |
||
| 125 | 'radio' => Displayers\Radio::class, |
||
| 126 | 'checkbox' => Displayers\Checkbox::class, |
||
| 127 | 'orderable' => Displayers\Orderable::class, |
||
| 128 | 'table' => Displayers\Table::class, |
||
| 129 | 'expand' => Displayers\Expand::class, |
||
| 130 | 'modal' => Displayers\Modal::class, |
||
| 131 | 'carousel' => Displayers\Carousel::class, |
||
| 132 | 'downloadable'=> Displayers\Downloadable::class, |
||
| 133 | 'copyable' => Displayers\Copyable::class, |
||
| 134 | 'qrcode' => Displayers\QRCode::class, |
||
| 135 | 'prefix' => Displayers\Prefix::class, |
||
| 136 | 'suffix' => Displayers\Suffix::class, |
||
| 137 | ]; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Defined columns. |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | public static $defined = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | protected static $htmlAttributes = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected static $rowAttributes = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var Model |
||
| 158 | */ |
||
| 159 | protected static $model; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $name |
||
| 163 | * @param string $label |
||
| 164 | */ |
||
| 165 | public function __construct($name, $label) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Initialize column attributes. |
||
| 176 | */ |
||
| 177 | protected function initAttributes() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Extend column displayer. |
||
| 186 | * |
||
| 187 | * @param $name |
||
| 188 | * @param $displayer |
||
| 189 | */ |
||
| 190 | public static function extend($name, $displayer) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Define a column globally. |
||
| 197 | * |
||
| 198 | * @param string $name |
||
| 199 | * @param mixed $definition |
||
| 200 | */ |
||
| 201 | public static function define($name, $definition) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set grid instance for column. |
||
| 208 | * |
||
| 209 | * @param Grid $grid |
||
| 210 | */ |
||
| 211 | public function setGrid(Grid $grid) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set model for column. |
||
| 220 | * |
||
| 221 | * @param $model |
||
| 222 | */ |
||
| 223 | public function setModel($model) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set original data for column. |
||
| 232 | * |
||
| 233 | * @param Collection $collection |
||
| 234 | */ |
||
| 235 | public static function setOriginalGridModels(Collection $collection) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set column attributes. |
||
| 242 | * |
||
| 243 | * @param array $attributes |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function setAttributes($attributes = [], $key = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get column attributes. |
||
| 268 | * |
||
| 269 | * @param string $name |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public static function getAttributes($name, $key = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Format attributes to html. |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function formatHtmlAttributes() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set style of this column. |
||
| 303 | * |
||
| 304 | * @param string $style |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function style($style) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set the width of column. |
||
| 315 | * |
||
| 316 | * @param int $width |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function width(int $width) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set the color of column. |
||
| 327 | * |
||
| 328 | * @param string $color |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function color($color) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get original column value. |
||
| 339 | * |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | public function getOriginal() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get name of this column. |
||
| 349 | * |
||
| 350 | * @return mixed |
||
| 351 | */ |
||
| 352 | public function getName() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getClassName() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Format label. |
||
| 369 | * |
||
| 370 | * @param $label |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | protected function formatLabel($label) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get label of the column. |
||
| 387 | * |
||
| 388 | * @return mixed |
||
| 389 | */ |
||
| 390 | public function getLabel() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set relation. |
||
| 397 | * |
||
| 398 | * @param string $relation |
||
| 399 | * @param string $relationColumn |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function setRelation($relation, $relationColumn = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * If this column is relation column. |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | protected function isRelation() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Mark this column as sortable. |
||
| 423 | * |
||
| 424 | * @param null|string $cast |
||
| 425 | * |
||
| 426 | * @return Column|string |
||
| 427 | */ |
||
| 428 | public function sortable($cast = null) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set cast name for sortable. |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | * |
||
| 438 | * @deprecated Use `$column->sortable($cast)` instead. |
||
| 439 | */ |
||
| 440 | public function cast($cast) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set help message for column. |
||
| 449 | * |
||
| 450 | * @param string $help |
||
| 451 | * |
||
| 452 | * @return $this|string |
||
| 453 | */ |
||
| 454 | public function help($help = '') |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set column filter. |
||
| 461 | * |
||
| 462 | * @param null $builder |
||
| 463 | * |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function filter($builder = null) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Add a display callback. |
||
| 473 | * |
||
| 474 | * @param Closure $callback |
||
| 475 | * |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function display(Closure $callback) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Display using display abstract. |
||
| 487 | * |
||
| 488 | * @param string $abstract |
||
| 489 | * @param array $arguments |
||
| 490 | * |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | public function displayUsing($abstract, $arguments = []) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Display column using array value map. |
||
| 509 | * |
||
| 510 | * @param array $values |
||
| 511 | * @param null $default |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | View Code Duplication | public function using(array $values, $default = null) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Replace output value with giving map. |
||
| 528 | * |
||
| 529 | * @param array $replacements |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function replace(array $replacements) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Render this column with the given view. |
||
| 546 | * |
||
| 547 | * @param string $view |
||
| 548 | * |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | public function view($view) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Hide this column by default. |
||
| 562 | * |
||
| 563 | * @return $this |
||
| 564 | */ |
||
| 565 | public function hide() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Add column to total-row. |
||
| 574 | * |
||
| 575 | * @param null $display |
||
| 576 | * |
||
| 577 | * @return $this |
||
| 578 | */ |
||
| 579 | public function totalRow($display = null) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Convert file size to a human readable format like `100mb`. |
||
| 588 | * |
||
| 589 | * @return $this |
||
| 590 | */ |
||
| 591 | public function filesize() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Display the fields in the email format as gavatar. |
||
| 600 | * |
||
| 601 | * @param int $size |
||
| 602 | * |
||
| 603 | * @return $this |
||
| 604 | */ |
||
| 605 | public function gravatar($size = 30) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Display field as a loading icon. |
||
| 620 | * |
||
| 621 | * @param array $values |
||
| 622 | * @param array $others |
||
| 623 | * |
||
| 624 | * @return $this |
||
| 625 | */ |
||
| 626 | public function loading($values = [], $others = []) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Display column as an font-awesome icon based on it's value. |
||
| 641 | * |
||
| 642 | * @param array $setting |
||
| 643 | * @param string $default |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | public function icon(array $setting, $default = '') |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Return a human readable format time. |
||
| 664 | * |
||
| 665 | * @param null $locale |
||
| 666 | * |
||
| 667 | * @return $this |
||
| 668 | */ |
||
| 669 | public function diffForHumans($locale = null) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Returns a string formatted according to the given format string. |
||
| 682 | * |
||
| 683 | * @param string $format |
||
| 684 | * |
||
| 685 | * @return $this |
||
| 686 | */ |
||
| 687 | public function date($format) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
| 696 | * |
||
| 697 | * @param array $map |
||
| 698 | * |
||
| 699 | * @return $this |
||
| 700 | */ |
||
| 701 | public function bool(array $map = []) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * If has display callbacks. |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | protected function hasDisplayCallbacks() |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Call all of the "display" callbacks column. |
||
| 722 | * |
||
| 723 | * @param mixed $value |
||
| 724 | * @param int $key |
||
| 725 | * |
||
| 726 | * @return mixed |
||
| 727 | */ |
||
| 728 | protected function callDisplayCallbacks($value, $key) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Set original grid data to column. |
||
| 749 | * |
||
| 750 | * @param Closure $callback |
||
| 751 | * @param int $key |
||
| 752 | * |
||
| 753 | * @return Closure |
||
| 754 | */ |
||
| 755 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Fill all data to every column. |
||
| 764 | * |
||
| 765 | * @param array $data |
||
| 766 | * |
||
| 767 | * @return mixed |
||
| 768 | */ |
||
| 769 | public function fill(array $data) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * If current column is a defined column. |
||
| 793 | * |
||
| 794 | * @return bool |
||
| 795 | */ |
||
| 796 | protected function isDefinedColumn() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Use a defined column. |
||
| 803 | * |
||
| 804 | * @throws \Exception |
||
| 805 | */ |
||
| 806 | protected function useDefinedColumn() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Convert characters to HTML entities recursively. |
||
| 836 | * |
||
| 837 | * @param array|string $item |
||
| 838 | * |
||
| 839 | * @return mixed |
||
| 840 | */ |
||
| 841 | protected function htmlEntityEncode($item) |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Find a displayer to display column. |
||
| 856 | * |
||
| 857 | * @param string $abstract |
||
| 858 | * @param array $arguments |
||
| 859 | * |
||
| 860 | * @return $this |
||
| 861 | */ |
||
| 862 | protected function resolveDisplayer($abstract, $arguments) |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Call Illuminate/Support displayer. |
||
| 873 | * |
||
| 874 | * @param string $abstract |
||
| 875 | * @param array $arguments |
||
| 876 | * |
||
| 877 | * @return $this |
||
| 878 | */ |
||
| 879 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Call Builtin displayer. |
||
| 896 | * |
||
| 897 | * @param string $abstract |
||
| 898 | * @param array $arguments |
||
| 899 | * |
||
| 900 | * @return $this |
||
| 901 | */ |
||
| 902 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 927 | * |
||
| 928 | * Allow fluent calls on the Column object. |
||
| 929 | * |
||
| 930 | * @param string $method |
||
| 931 | * @param array $arguments |
||
| 932 | * |
||
| 933 | * @return $this |
||
| 934 | */ |
||
| 935 | public function __call($method, $arguments) |
||
| 948 | } |
||
| 949 |
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.