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 |
||
| 38 | class Column |
||
| 39 | { |
||
| 40 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 41 | |||
| 42 | const ACTION_COLUMN_NAME = '__actions__'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Grid |
||
| 46 | */ |
||
| 47 | protected $grid; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Name of column. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $name; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Label of column. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $label; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Original value of column. |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | */ |
||
| 68 | protected $original; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Is column sortable. |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $sortable = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Sort arguments. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $sort; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Help message. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $help = ''; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Cast Name. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $cast; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Attributes of column. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $attributes = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Relation name. |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $relation = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Relation column. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected $relationColumn; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Original grid data. |
||
| 121 | * |
||
| 122 | * @var Collection |
||
| 123 | */ |
||
| 124 | protected static $originalGridModels; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var []Closure |
||
| 128 | */ |
||
| 129 | protected $displayCallbacks = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Displayers for grid column. |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | public static $displayers = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Defined columns. |
||
| 140 | * |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | public static $defined = []; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected static $htmlAttributes = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var Model |
||
| 152 | */ |
||
| 153 | protected static $model; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $name |
||
| 157 | * @param string $label |
||
| 158 | */ |
||
| 159 | public function __construct($name, $label) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Extend column displayer. |
||
| 168 | * |
||
| 169 | * @param $name |
||
| 170 | * @param $displayer |
||
| 171 | */ |
||
| 172 | public static function extend($name, $displayer) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Define a column globally. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * @param mixed $definition |
||
| 182 | */ |
||
| 183 | public static function define($name, $definition) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set grid instance for column. |
||
| 190 | * |
||
| 191 | * @param Grid $grid |
||
| 192 | */ |
||
| 193 | public function setGrid(Grid $grid) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set model for column. |
||
| 202 | * |
||
| 203 | * @param $model |
||
| 204 | */ |
||
| 205 | public function setModel($model) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set original data for column. |
||
| 214 | * |
||
| 215 | * @param Collection $collection |
||
| 216 | */ |
||
| 217 | public static function setOriginalGridModels(Collection $collection) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set column attributes. |
||
| 224 | * |
||
| 225 | * @param array $attributes |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | public function setAttributes($attributes = []) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get column attributes. |
||
| 238 | * |
||
| 239 | * @param string $name |
||
| 240 | * |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | public static function getAttributes($name) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set style of this column. |
||
| 250 | * |
||
| 251 | * @param string $style |
||
| 252 | * |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function style($style) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the width of column. |
||
| 262 | * |
||
| 263 | * @param int $width |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function width(int $width) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set the color of column. |
||
| 274 | * |
||
| 275 | * @param string $color |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function color($color) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get original column value. |
||
| 286 | * |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function getOriginal() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get name of this column. |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | public function getName() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Format label. |
||
| 306 | * |
||
| 307 | * @param $label |
||
| 308 | * |
||
| 309 | * @return mixed |
||
| 310 | */ |
||
| 311 | protected function formatLabel($label) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get label of the column. |
||
| 324 | * |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function getLabel() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set relation. |
||
| 334 | * |
||
| 335 | * @param string $relation |
||
| 336 | * @param string $relationColumn |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setRelation($relation, $relationColumn = null) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * If this column is relation column. |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | protected function isRelation() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set sort value. |
||
| 360 | * |
||
| 361 | * @param bool $sort |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function sort($sort) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Mark this column as sortable. |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function sortable() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set cast name for sortable. |
||
| 384 | * |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function cast($cast) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Add a display callback. |
||
| 396 | * |
||
| 397 | * @param Closure $callback |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | public function display(Closure $callback) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Display using display abstract. |
||
| 410 | * |
||
| 411 | * @param string $abstract |
||
| 412 | * @param array $arguments |
||
| 413 | * |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function displayUsing($abstract, $arguments = []) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Display column using array value map. |
||
| 432 | * |
||
| 433 | * @param array $values |
||
| 434 | * @param null $default |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | View Code Duplication | public function using(array $values, $default = null) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Replace output value with giving map. |
||
| 451 | * |
||
| 452 | * @param array $replacements |
||
| 453 | * |
||
| 454 | * @return $this |
||
| 455 | */ |
||
| 456 | public function replace(array $replacements) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Render this column with the given view. |
||
| 469 | * |
||
| 470 | * @param string $view |
||
| 471 | * |
||
| 472 | * @return $this |
||
| 473 | */ |
||
| 474 | public function view($view) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Hide this column by default. |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | public function hide() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add column to total-row. |
||
| 497 | * |
||
| 498 | * @param null $display |
||
| 499 | * |
||
| 500 | * @return $this |
||
| 501 | */ |
||
| 502 | public function totalRow($display = null) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Convert file size to a human readable format like `100mb`. |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | */ |
||
| 514 | public function filesize() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Display the fields in the email format as gavatar. |
||
| 523 | * |
||
| 524 | * @param int $size |
||
| 525 | * |
||
| 526 | * @return $this |
||
| 527 | */ |
||
| 528 | public function gravatar($size = 30) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Display field as a loading icon. |
||
| 543 | * |
||
| 544 | * @param array $values |
||
| 545 | * @param array $others |
||
| 546 | * |
||
| 547 | * @return $this |
||
| 548 | */ |
||
| 549 | public function loading($values = [], $others = []) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Display column as an font-awesome icon based on it's value. |
||
| 565 | * |
||
| 566 | * @param array $setting |
||
| 567 | * @param string $default |
||
| 568 | * |
||
| 569 | * @return $this |
||
| 570 | */ |
||
| 571 | public function icon(array $setting, $default = '') |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Return a human readable format time. |
||
| 589 | * |
||
| 590 | * @param null $locale |
||
| 591 | * |
||
| 592 | * @return $this |
||
| 593 | */ |
||
| 594 | public function diffForHumans($locale = null) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * If has display callbacks. |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | protected function hasDisplayCallbacks() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Call all of the "display" callbacks column. |
||
| 617 | * |
||
| 618 | * @param mixed $value |
||
| 619 | * @param int $key |
||
| 620 | * |
||
| 621 | * @return mixed |
||
| 622 | */ |
||
| 623 | protected function callDisplayCallbacks($value, $key) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Set original grid data to column. |
||
| 644 | * |
||
| 645 | * @param Closure $callback |
||
| 646 | * @param int $key |
||
| 647 | * |
||
| 648 | * @return Closure |
||
| 649 | */ |
||
| 650 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Fill all data to every column. |
||
| 659 | * |
||
| 660 | * @param array $data |
||
| 661 | * |
||
| 662 | * @return mixed |
||
| 663 | */ |
||
| 664 | public function fill(array $data) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * If current column is a defined column. |
||
| 688 | * |
||
| 689 | * @return bool |
||
| 690 | */ |
||
| 691 | protected function isDefinedColumn() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Use a defined column. |
||
| 698 | * |
||
| 699 | * @throws \Exception |
||
| 700 | */ |
||
| 701 | protected function useDefinedColumn() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Convert characters to HTML entities recursively. |
||
| 731 | * |
||
| 732 | * @param array|string $item |
||
| 733 | * |
||
| 734 | * @return mixed |
||
| 735 | */ |
||
| 736 | protected function htmlEntityEncode($item) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Create the column sorter. |
||
| 751 | * |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | public function sorter() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Determine if this column is currently sorted. |
||
| 784 | * |
||
| 785 | * @return bool |
||
| 786 | */ |
||
| 787 | protected function isSorted() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Set help message for column. |
||
| 800 | * |
||
| 801 | * @param string $help |
||
| 802 | * |
||
| 803 | * @return $this|string |
||
| 804 | */ |
||
| 805 | public function help($help = '') |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Find a displayer to display column. |
||
| 840 | * |
||
| 841 | * @param string $abstract |
||
| 842 | * @param array $arguments |
||
| 843 | * |
||
| 844 | * @return $this |
||
| 845 | */ |
||
| 846 | protected function resolveDisplayer($abstract, $arguments) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Call Illuminate/Support displayer. |
||
| 857 | * |
||
| 858 | * @param string $abstract |
||
| 859 | * @param array $arguments |
||
| 860 | * |
||
| 861 | * @return $this |
||
| 862 | */ |
||
| 863 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Call Builtin displayer. |
||
| 880 | * |
||
| 881 | * @param string $abstract |
||
| 882 | * @param array $arguments |
||
| 883 | * |
||
| 884 | * @return $this |
||
| 885 | */ |
||
| 886 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 911 | * |
||
| 912 | * Allow fluent calls on the Column object. |
||
| 913 | * |
||
| 914 | * @param string $method |
||
| 915 | * @param array $arguments |
||
| 916 | * |
||
| 917 | * @return $this |
||
| 918 | */ |
||
| 919 | public function __call($method, $arguments) |
||
| 932 | } |
||
| 933 |
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: