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 | * @param string $name |
||
| 164 | * @param string $label |
||
| 165 | */ |
||
| 166 | public function __construct($name, $label) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Initialize column attributes. |
||
| 177 | */ |
||
| 178 | protected function initAttributes() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Extend column displayer. |
||
| 187 | * |
||
| 188 | * @param $name |
||
| 189 | * @param $displayer |
||
| 190 | */ |
||
| 191 | public static function extend($name, $displayer) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Define a column globally. |
||
| 198 | * |
||
| 199 | * @param string $name |
||
| 200 | * @param mixed $definition |
||
| 201 | */ |
||
| 202 | public static function define($name, $definition) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set grid instance for column. |
||
| 209 | * |
||
| 210 | * @param Grid $grid |
||
| 211 | */ |
||
| 212 | public function setGrid(Grid $grid) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set model for column. |
||
| 221 | * |
||
| 222 | * @param $model |
||
| 223 | */ |
||
| 224 | public function setModel($model) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set original data for column. |
||
| 233 | * |
||
| 234 | * @param Collection $collection |
||
| 235 | */ |
||
| 236 | public static function setOriginalGridModels(Collection $collection) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set column attributes. |
||
| 243 | * |
||
| 244 | * @param array $attributes |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function setAttributes($attributes = [], $key = null) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get column attributes. |
||
| 269 | * |
||
| 270 | * @param string $name |
||
| 271 | * |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | public static function getAttributes($name, $key = null) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Format attributes to html. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function formatHtmlAttributes() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set style of this column. |
||
| 304 | * |
||
| 305 | * @param string $style |
||
| 306 | * |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function style($style) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Set the width of column. |
||
| 316 | * |
||
| 317 | * @param int $width |
||
| 318 | * |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function width(int $width) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the color of column. |
||
| 328 | * |
||
| 329 | * @param string $color |
||
| 330 | * |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function color($color) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get original column value. |
||
| 340 | * |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | public function getOriginal() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get name of this column. |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function getName() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getClassName() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Format label. |
||
| 370 | * |
||
| 371 | * @param $label |
||
| 372 | * |
||
| 373 | * @return mixed |
||
| 374 | */ |
||
| 375 | protected function formatLabel($label) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get label of the column. |
||
| 388 | * |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | public function getLabel() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set relation. |
||
| 398 | * |
||
| 399 | * @param string $relation |
||
| 400 | * @param string $relationColumn |
||
| 401 | * |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | public function setRelation($relation, $relationColumn = null) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * If this column is relation column. |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | protected function isRelation() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Mark this column as sortable. |
||
| 424 | * |
||
| 425 | * @param null|string $cast |
||
| 426 | * |
||
| 427 | * @return Column|string |
||
| 428 | */ |
||
| 429 | public function sortable($cast = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set cast name for sortable. |
||
| 436 | * |
||
| 437 | * @return $this |
||
| 438 | * |
||
| 439 | * @deprecated Use `$column->sortable($cast)` instead. |
||
| 440 | */ |
||
| 441 | public function cast($cast) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Set help message for column. |
||
| 450 | * |
||
| 451 | * @param string $help |
||
| 452 | * |
||
| 453 | * @return $this|string |
||
| 454 | */ |
||
| 455 | public function help($help = '') |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Set column filter. |
||
| 462 | * |
||
| 463 | * @param null $builder |
||
| 464 | * |
||
| 465 | * @return $this |
||
| 466 | */ |
||
| 467 | public function filter($builder = null) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Add a display callback. |
||
| 474 | * |
||
| 475 | * @param Closure $callback |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | public function display(Closure $callback) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Display using display abstract. |
||
| 488 | * |
||
| 489 | * @param string $abstract |
||
| 490 | * @param array $arguments |
||
| 491 | * |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | public function displayUsing($abstract, $arguments = []) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Display column using array value map. |
||
| 510 | * |
||
| 511 | * @param array $values |
||
| 512 | * @param null $default |
||
| 513 | * |
||
| 514 | * @return $this |
||
| 515 | */ |
||
| 516 | View Code Duplication | public function using(array $values, $default = null) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Replace output value with giving map. |
||
| 529 | * |
||
| 530 | * @param array $replacements |
||
| 531 | * |
||
| 532 | * @return $this |
||
| 533 | */ |
||
| 534 | public function replace(array $replacements) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Render this column with the given view. |
||
| 547 | * |
||
| 548 | * @param string $view |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | public function view($view) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Hide this column by default. |
||
| 563 | * |
||
| 564 | * @return $this |
||
| 565 | */ |
||
| 566 | public function hide() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Add column to total-row. |
||
| 575 | * |
||
| 576 | * @param null $display |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function totalRow($display = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Convert file size to a human readable format like `100mb`. |
||
| 589 | * |
||
| 590 | * @return $this |
||
| 591 | */ |
||
| 592 | public function filesize() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Display the fields in the email format as gavatar. |
||
| 601 | * |
||
| 602 | * @param int $size |
||
| 603 | * |
||
| 604 | * @return $this |
||
| 605 | */ |
||
| 606 | public function gravatar($size = 30) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Display field as a loading icon. |
||
| 621 | * |
||
| 622 | * @param array $values |
||
| 623 | * @param array $others |
||
| 624 | * |
||
| 625 | * @return $this |
||
| 626 | */ |
||
| 627 | public function loading($values = [], $others = []) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Display column as an font-awesome icon based on it's value. |
||
| 642 | * |
||
| 643 | * @param array $setting |
||
| 644 | * @param string $default |
||
| 645 | * |
||
| 646 | * @return $this |
||
| 647 | */ |
||
| 648 | public function icon(array $setting, $default = '') |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Return a human readable format time. |
||
| 665 | * |
||
| 666 | * @param null $locale |
||
| 667 | * |
||
| 668 | * @return $this |
||
| 669 | */ |
||
| 670 | public function diffForHumans($locale = null) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Returns a string formatted according to the given format string. |
||
| 683 | * |
||
| 684 | * @param string $format |
||
| 685 | * |
||
| 686 | * @return $this |
||
| 687 | */ |
||
| 688 | public function date($format) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
| 697 | * |
||
| 698 | * @param array $map |
||
| 699 | * @param bool $default |
||
| 700 | * |
||
| 701 | * @return $this |
||
| 702 | */ |
||
| 703 | public function bool(array $map = [], $default = false) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Display column using a grid row action. |
||
| 714 | * |
||
| 715 | * @param string $action |
||
| 716 | * |
||
| 717 | * @return Column |
||
| 718 | */ |
||
| 719 | public function action($action) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * If has display callbacks. |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | protected function hasDisplayCallbacks() |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Call all of the "display" callbacks column. |
||
| 749 | * |
||
| 750 | * @param mixed $value |
||
| 751 | * @param int $key |
||
| 752 | * |
||
| 753 | * @return mixed |
||
| 754 | */ |
||
| 755 | protected function callDisplayCallbacks($value, $key) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Set original grid data to column. |
||
| 776 | * |
||
| 777 | * @param Closure $callback |
||
| 778 | * @param int $key |
||
| 779 | * |
||
| 780 | * @return Closure |
||
| 781 | */ |
||
| 782 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Fill all data to every column. |
||
| 791 | * |
||
| 792 | * @param array $data |
||
| 793 | * |
||
| 794 | * @return mixed |
||
| 795 | */ |
||
| 796 | public function fill(array $data) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * If current column is a defined column. |
||
| 820 | * |
||
| 821 | * @return bool |
||
| 822 | */ |
||
| 823 | protected function isDefinedColumn() |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Use a defined column. |
||
| 830 | * |
||
| 831 | * @throws \Exception |
||
| 832 | */ |
||
| 833 | protected function useDefinedColumn() |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Convert characters to HTML entities recursively. |
||
| 863 | * |
||
| 864 | * @param array|string $item |
||
| 865 | * |
||
| 866 | * @return mixed |
||
| 867 | */ |
||
| 868 | protected function htmlEntityEncode($item) |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Find a displayer to display column. |
||
| 883 | * |
||
| 884 | * @param string $abstract |
||
| 885 | * @param array $arguments |
||
| 886 | * |
||
| 887 | * @return $this |
||
| 888 | */ |
||
| 889 | protected function resolveDisplayer($abstract, $arguments) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Call Illuminate/Support displayer. |
||
| 900 | * |
||
| 901 | * @param string $abstract |
||
| 902 | * @param array $arguments |
||
| 903 | * |
||
| 904 | * @return $this |
||
| 905 | */ |
||
| 906 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Call Builtin displayer. |
||
| 923 | * |
||
| 924 | * @param string $abstract |
||
| 925 | * @param array $arguments |
||
| 926 | * |
||
| 927 | * @return $this |
||
| 928 | */ |
||
| 929 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 954 | * |
||
| 955 | * Allow fluent calls on the Column object. |
||
| 956 | * |
||
| 957 | * @param string $method |
||
| 958 | * @param array $arguments |
||
| 959 | * |
||
| 960 | * @return $this |
||
| 961 | */ |
||
| 962 | public function __call($method, $arguments) |
||
| 975 | } |
||
| 976 |
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.