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 |
||
| 39 | class Column |
||
| 40 | { |
||
| 41 | use Column\HasHeader; |
||
| 42 | |||
| 43 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 44 | |||
| 45 | const ACTION_COLUMN_NAME = '__actions__'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Grid |
||
| 49 | */ |
||
| 50 | protected $grid; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Name of column. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $name; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Label of column. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $label; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Original value of column. |
||
| 68 | * |
||
| 69 | * @var mixed |
||
| 70 | */ |
||
| 71 | protected $original; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Attributes of column. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $attributes = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Relation name. |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $relation = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Relation column. |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $relationColumn; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Original grid data. |
||
| 96 | * |
||
| 97 | * @var Collection |
||
| 98 | */ |
||
| 99 | protected static $originalGridModels; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var []Closure |
||
| 103 | */ |
||
| 104 | protected $displayCallbacks = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Displayers for grid column. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | public static $displayers = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Defined columns. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | public static $defined = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | protected static $htmlAttributes = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var Model |
||
| 127 | */ |
||
| 128 | protected static $model; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $name |
||
| 132 | * @param string $label |
||
| 133 | */ |
||
| 134 | public function __construct($name, $label) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Initialize column attributes. |
||
| 145 | */ |
||
| 146 | protected function initAttributes() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Extend column displayer. |
||
| 155 | * |
||
| 156 | * @param $name |
||
| 157 | * @param $displayer |
||
| 158 | */ |
||
| 159 | public static function extend($name, $displayer) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Define a column globally. |
||
| 166 | * |
||
| 167 | * @param string $name |
||
| 168 | * @param mixed $definition |
||
| 169 | */ |
||
| 170 | public static function define($name, $definition) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set grid instance for column. |
||
| 177 | * |
||
| 178 | * @param Grid $grid |
||
| 179 | */ |
||
| 180 | public function setGrid(Grid $grid) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set model for column. |
||
| 189 | * |
||
| 190 | * @param $model |
||
| 191 | */ |
||
| 192 | public function setModel($model) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set original data for column. |
||
| 201 | * |
||
| 202 | * @param Collection $collection |
||
| 203 | */ |
||
| 204 | public static function setOriginalGridModels(Collection $collection) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set column attributes. |
||
| 211 | * |
||
| 212 | * @param array $attributes |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function setAttributes($attributes = []) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get column attributes. |
||
| 228 | * |
||
| 229 | * @param string $name |
||
| 230 | * |
||
| 231 | * @return mixed |
||
| 232 | */ |
||
| 233 | public static function getAttributes($name) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Format attributes to html. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function formatHtmlAttributes() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set style of this column. |
||
| 255 | * |
||
| 256 | * @param string $style |
||
| 257 | * |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function style($style) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set the width of column. |
||
| 267 | * |
||
| 268 | * @param int $width |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function width(int $width) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set the color of column. |
||
| 279 | * |
||
| 280 | * @param string $color |
||
| 281 | * |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function color($color) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get original column value. |
||
| 291 | * |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | public function getOriginal() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get name of this column. |
||
| 301 | * |
||
| 302 | * @return mixed |
||
| 303 | */ |
||
| 304 | public function getName() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $name |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getClassName() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Format label. |
||
| 323 | * |
||
| 324 | * @param $label |
||
| 325 | * |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | protected function formatLabel($label) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get label of the column. |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | public function getLabel() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set relation. |
||
| 351 | * |
||
| 352 | * @param string $relation |
||
| 353 | * @param string $relationColumn |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setRelation($relation, $relationColumn = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * If this column is relation column. |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | protected function isRelation() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Mark this column as sortable. |
||
| 377 | * |
||
| 378 | * @param null|string $cast |
||
| 379 | * |
||
| 380 | * @return Column|string |
||
| 381 | */ |
||
| 382 | public function sortable($cast = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set cast name for sortable. |
||
| 389 | * |
||
| 390 | * @return $this |
||
| 391 | * |
||
| 392 | * @deprecated Use `$column->sortable($cast)` instead. |
||
| 393 | */ |
||
| 394 | public function cast($cast) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Set help message for column. |
||
| 403 | * |
||
| 404 | * @param string $help |
||
| 405 | * |
||
| 406 | * @return $this|string |
||
| 407 | */ |
||
| 408 | public function help($help = '') |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Set column filter. |
||
| 415 | * |
||
| 416 | * @param null $builder |
||
| 417 | * |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function filter($builder = null) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Add a display callback. |
||
| 427 | * |
||
| 428 | * @param Closure $callback |
||
| 429 | * |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | public function display(Closure $callback) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Display using display abstract. |
||
| 441 | * |
||
| 442 | * @param string $abstract |
||
| 443 | * @param array $arguments |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | public function displayUsing($abstract, $arguments = []) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Display column using array value map. |
||
| 463 | * |
||
| 464 | * @param array $values |
||
| 465 | * @param null $default |
||
| 466 | * |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | View Code Duplication | public function using(array $values, $default = null) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Replace output value with giving map. |
||
| 482 | * |
||
| 483 | * @param array $replacements |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | public function replace(array $replacements) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Render this column with the given view. |
||
| 500 | * |
||
| 501 | * @param string $view |
||
| 502 | * |
||
| 503 | * @return $this |
||
| 504 | */ |
||
| 505 | public function view($view) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Hide this column by default. |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function hide() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Add column to total-row. |
||
| 528 | * |
||
| 529 | * @param null $display |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function totalRow($display = null) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Convert file size to a human readable format like `100mb`. |
||
| 542 | * |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | public function filesize() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Display the fields in the email format as gavatar. |
||
| 554 | * |
||
| 555 | * @param int $size |
||
| 556 | * |
||
| 557 | * @return $this |
||
| 558 | */ |
||
| 559 | public function gravatar($size = 30) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Display field as a loading icon. |
||
| 574 | * |
||
| 575 | * @param array $values |
||
| 576 | * @param array $others |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function loading($values = [], $others = []) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Display column as an font-awesome icon based on it's value. |
||
| 595 | * |
||
| 596 | * @param array $setting |
||
| 597 | * @param string $default |
||
| 598 | * |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | public function icon(array $setting, $default = '') |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Return a human readable format time. |
||
| 618 | * |
||
| 619 | * @param null $locale |
||
| 620 | * |
||
| 621 | * @return $this |
||
| 622 | */ |
||
| 623 | public function diffForHumans($locale = null) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * If has display callbacks. |
||
| 636 | * |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | protected function hasDisplayCallbacks() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Call all of the "display" callbacks column. |
||
| 646 | * |
||
| 647 | * @param mixed $value |
||
| 648 | * @param int $key |
||
| 649 | * |
||
| 650 | * @return mixed |
||
| 651 | */ |
||
| 652 | protected function callDisplayCallbacks($value, $key) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Set original grid data to column. |
||
| 673 | * |
||
| 674 | * @param Closure $callback |
||
| 675 | * @param int $key |
||
| 676 | * |
||
| 677 | * @return Closure |
||
| 678 | */ |
||
| 679 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Fill all data to every column. |
||
| 688 | * |
||
| 689 | * @param array $data |
||
| 690 | * |
||
| 691 | * @return mixed |
||
| 692 | */ |
||
| 693 | public function fill(array $data) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * If current column is a defined column. |
||
| 717 | * |
||
| 718 | * @return bool |
||
| 719 | */ |
||
| 720 | protected function isDefinedColumn() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Use a defined column. |
||
| 727 | * |
||
| 728 | * @throws \Exception |
||
| 729 | */ |
||
| 730 | protected function useDefinedColumn() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Convert characters to HTML entities recursively. |
||
| 760 | * |
||
| 761 | * @param array|string $item |
||
| 762 | * |
||
| 763 | * @return mixed |
||
| 764 | */ |
||
| 765 | protected function htmlEntityEncode($item) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Find a displayer to display column. |
||
| 780 | * |
||
| 781 | * @param string $abstract |
||
| 782 | * @param array $arguments |
||
| 783 | * |
||
| 784 | * @return $this |
||
| 785 | */ |
||
| 786 | protected function resolveDisplayer($abstract, $arguments) |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Call Illuminate/Support displayer. |
||
| 797 | * |
||
| 798 | * @param string $abstract |
||
| 799 | * @param array $arguments |
||
| 800 | * |
||
| 801 | * @return $this |
||
| 802 | */ |
||
| 803 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Call Builtin displayer. |
||
| 820 | * |
||
| 821 | * @param string $abstract |
||
| 822 | * @param array $arguments |
||
| 823 | * |
||
| 824 | * @return $this |
||
| 825 | */ |
||
| 826 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 851 | * |
||
| 852 | * Allow fluent calls on the Column object. |
||
| 853 | * |
||
| 854 | * @param string $method |
||
| 855 | * @param array $arguments |
||
| 856 | * |
||
| 857 | * @return $this |
||
| 858 | */ |
||
| 859 | public function __call($method, $arguments) |
||
| 872 | } |
||
| 873 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: