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 |
||
| 37 | class Column |
||
| 38 | { |
||
| 39 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Grid |
||
| 43 | */ |
||
| 44 | protected $grid; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Name of column. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $name; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Label of column. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $label; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Original value of column. |
||
| 62 | * |
||
| 63 | * @var mixed |
||
| 64 | */ |
||
| 65 | protected $original; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Is column sortable. |
||
| 69 | * |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $sortable = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Sort arguments. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $sort; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Help message. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $help = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Cast Name. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $cast; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Attributes of column. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $attributes = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Relation name. |
||
| 104 | * |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $relation = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Relation column. |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $relationColumn; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Original grid data. |
||
| 118 | * |
||
| 119 | * @var Collection |
||
| 120 | */ |
||
| 121 | protected static $originalGridModels; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var []Closure |
||
| 125 | */ |
||
| 126 | protected $displayCallbacks = []; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Displayers for grid column. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | public static $displayers = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Defined columns. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | public static $defined = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | protected static $htmlAttributes = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var Model |
||
| 149 | */ |
||
| 150 | protected static $model; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $name |
||
| 154 | * @param string $label |
||
| 155 | */ |
||
| 156 | public function __construct($name, $label) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Extend column displayer. |
||
| 165 | * |
||
| 166 | * @param $name |
||
| 167 | * @param $displayer |
||
| 168 | */ |
||
| 169 | public static function extend($name, $displayer) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Define a column globally. |
||
| 176 | * |
||
| 177 | * @param string $name |
||
| 178 | * @param mixed $definition |
||
| 179 | */ |
||
| 180 | public static function define($name, $definition) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Set grid instance for column. |
||
| 187 | * |
||
| 188 | * @param Grid $grid |
||
| 189 | */ |
||
| 190 | public function setGrid(Grid $grid) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set model for column. |
||
| 199 | * |
||
| 200 | * @param $model |
||
| 201 | */ |
||
| 202 | public function setModel($model) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set original data for column. |
||
| 211 | * |
||
| 212 | * @param Collection $collection |
||
| 213 | */ |
||
| 214 | public static function setOriginalGridModels(Collection $collection) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set column attributes. |
||
| 221 | * |
||
| 222 | * @param array $attributes |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setAttributes($attributes = []) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get column attributes. |
||
| 235 | * |
||
| 236 | * @param string $name |
||
| 237 | * |
||
| 238 | * @return mixed |
||
| 239 | */ |
||
| 240 | public static function getAttributes($name) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set style of this column. |
||
| 247 | * |
||
| 248 | * @param string $style |
||
| 249 | * |
||
| 250 | * @return Column |
||
| 251 | */ |
||
| 252 | public function style($style) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set the width of column. |
||
| 259 | * |
||
| 260 | * @param int $width |
||
| 261 | * |
||
| 262 | * @return Column |
||
| 263 | */ |
||
| 264 | public function width(int $width) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get name of this column. |
||
| 271 | * |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | public function getName() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Format label. |
||
| 281 | * |
||
| 282 | * @param $label |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | protected function formatLabel($label) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get label of the column. |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | public function getLabel() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set relation. |
||
| 305 | * |
||
| 306 | * @param string $relation |
||
| 307 | * @param string $relationColumn |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function setRelation($relation, $relationColumn = null) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * If this column is relation column. |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | protected function isRelation() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set sort value. |
||
| 331 | * |
||
| 332 | * @param bool $sort |
||
| 333 | * |
||
| 334 | * @return Column |
||
| 335 | */ |
||
| 336 | public function sort($sort) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Mark this column as sortable. |
||
| 345 | * |
||
| 346 | * @return Column |
||
| 347 | */ |
||
| 348 | public function sortable() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Set cast name for sortable. |
||
| 355 | * |
||
| 356 | * @return Column |
||
| 357 | */ |
||
| 358 | public function cast($cast) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Add a display callback. |
||
| 367 | * |
||
| 368 | * @param Closure $callback |
||
| 369 | * |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function display(Closure $callback) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Display using display abstract. |
||
| 381 | * |
||
| 382 | * @param string $abstract |
||
| 383 | * @param array $arguments |
||
| 384 | * |
||
| 385 | * @return Column |
||
| 386 | */ |
||
| 387 | public function displayUsing($abstract, $arguments = []) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Display column using array value map. |
||
| 403 | * |
||
| 404 | * @param array $values |
||
| 405 | * @param null $default |
||
| 406 | * |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function using(array $values, $default = null) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Render this column with the given view. |
||
| 422 | * |
||
| 423 | * @param string $view |
||
| 424 | * |
||
| 425 | * @return $this |
||
| 426 | */ |
||
| 427 | public function view($view) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Add column to total-row. |
||
| 438 | * |
||
| 439 | * @param null $display |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function totalRow($display = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * If has display callbacks. |
||
| 452 | * |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | protected function hasDisplayCallbacks() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Call all of the "display" callbacks column. |
||
| 462 | * |
||
| 463 | * @param mixed $value |
||
| 464 | * @param int $key |
||
| 465 | * |
||
| 466 | * @return mixed |
||
| 467 | */ |
||
| 468 | protected function callDisplayCallbacks($value, $key) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set original grid data to column. |
||
| 489 | * |
||
| 490 | * @param Closure $callback |
||
| 491 | * @param int $key |
||
| 492 | * |
||
| 493 | * @return Closure |
||
| 494 | */ |
||
| 495 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Fill all data to every column. |
||
| 504 | * |
||
| 505 | * @param array $data |
||
| 506 | * |
||
| 507 | * @return mixed |
||
| 508 | */ |
||
| 509 | public function fill(array $data) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * If current column is a defined column. |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | protected function isDefinedColumn() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Use a defined column. |
||
| 543 | * |
||
| 544 | * @throws \Exception |
||
| 545 | */ |
||
| 546 | protected function useDefinedColumn() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Convert characters to HTML entities recursively. |
||
| 576 | * |
||
| 577 | * @param array|string $item |
||
| 578 | * |
||
| 579 | * @return mixed |
||
| 580 | */ |
||
| 581 | protected function htmlEntityEncode($item) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Create the column sorter. |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | public function sorter() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Determine if this column is currently sorted. |
||
| 629 | * |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | protected function isSorted() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Set help message for column. |
||
| 645 | * |
||
| 646 | * @param string $help |
||
| 647 | * |
||
| 648 | * @return $this|string |
||
| 649 | */ |
||
| 650 | public function help($help = '') |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Find a displayer to display column. |
||
| 673 | * |
||
| 674 | * @param string $abstract |
||
| 675 | * @param array $arguments |
||
| 676 | * |
||
| 677 | * @return Column |
||
| 678 | */ |
||
| 679 | protected function resolveDisplayer($abstract, $arguments) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Call Illuminate/Support displayer. |
||
| 690 | * |
||
| 691 | * @param string $abstract |
||
| 692 | * @param array $arguments |
||
| 693 | * |
||
| 694 | * @return Column |
||
| 695 | */ |
||
| 696 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Call Builtin displayer. |
||
| 713 | * |
||
| 714 | * @param string $abstract |
||
| 715 | * @param array $arguments |
||
| 716 | * |
||
| 717 | * @return Column |
||
| 718 | */ |
||
| 719 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 744 | * |
||
| 745 | * Allow fluent calls on the Column object. |
||
| 746 | * |
||
| 747 | * @param string $method |
||
| 748 | * @param array $arguments |
||
| 749 | * |
||
| 750 | * @return $this |
||
| 751 | */ |
||
| 752 | public function __call($method, $arguments) |
||
| 765 | } |
||
| 766 |
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: