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 | use Column\HasHeader; |
||
| 40 | |||
| 41 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 42 | |||
| 43 | const ACTION_COLUMN_NAME = '__actions__'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Grid |
||
| 47 | */ |
||
| 48 | protected $grid; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Name of column. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $name; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Label of column. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $label; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Original value of column. |
||
| 66 | * |
||
| 67 | * @var mixed |
||
| 68 | */ |
||
| 69 | protected $original; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Attributes of column. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $attributes = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Relation name. |
||
| 80 | * |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | protected $relation = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Relation column. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $relationColumn; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Original grid data. |
||
| 94 | * |
||
| 95 | * @var Collection |
||
| 96 | */ |
||
| 97 | protected static $originalGridModels; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var []Closure |
||
| 101 | */ |
||
| 102 | protected $displayCallbacks = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Displayers for grid column. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | public static $displayers = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Defined columns. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | public static $defined = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected static $htmlAttributes = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var Model |
||
| 125 | */ |
||
| 126 | protected static $model; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $name |
||
| 130 | * @param string $label |
||
| 131 | */ |
||
| 132 | public function __construct($name, $label) |
||
| 133 | { |
||
| 134 | $this->name = $name; |
||
| 135 | |||
| 136 | $this->label = $this->formatLabel($label); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Extend column displayer. |
||
| 141 | * |
||
| 142 | * @param $name |
||
| 143 | * @param $displayer |
||
| 144 | */ |
||
| 145 | public static function extend($name, $displayer) |
||
| 146 | { |
||
| 147 | static::$displayers[$name] = $displayer; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Define a column globally. |
||
| 152 | * |
||
| 153 | * @param string $name |
||
| 154 | * @param mixed $definition |
||
| 155 | */ |
||
| 156 | public static function define($name, $definition) |
||
| 157 | { |
||
| 158 | static::$defined[$name] = $definition; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Set grid instance for column. |
||
| 163 | * |
||
| 164 | * @param Grid $grid |
||
| 165 | */ |
||
| 166 | public function setGrid(Grid $grid) |
||
| 167 | { |
||
| 168 | $this->grid = $grid; |
||
| 169 | |||
| 170 | $this->setModel($grid->model()->eloquent()); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set model for column. |
||
| 175 | * |
||
| 176 | * @param $model |
||
| 177 | */ |
||
| 178 | public function setModel($model) |
||
| 179 | { |
||
| 180 | if (is_null(static::$model) && ($model instanceof Model)) { |
||
| 181 | static::$model = $model->newInstance(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Set original data for column. |
||
| 187 | * |
||
| 188 | * @param Collection $collection |
||
| 189 | */ |
||
| 190 | public static function setOriginalGridModels(Collection $collection) |
||
| 191 | { |
||
| 192 | static::$originalGridModels = $collection; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set column attributes. |
||
| 197 | * |
||
| 198 | * @param array $attributes |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function setAttributes($attributes = []) |
||
| 203 | { |
||
| 204 | static::$htmlAttributes[$this->name] = $attributes; |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get column attributes. |
||
| 211 | * |
||
| 212 | * @param string $name |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | public static function getAttributes($name) |
||
| 217 | { |
||
| 218 | return Arr::get(static::$htmlAttributes, $name, ''); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set style of this column. |
||
| 223 | * |
||
| 224 | * @param string $style |
||
| 225 | * |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function style($style) |
||
| 229 | { |
||
| 230 | return $this->setAttributes(compact('style')); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set the width of column. |
||
| 235 | * |
||
| 236 | * @param int $width |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function width(int $width) |
||
| 241 | { |
||
| 242 | return $this->style("width: {$width}px;"); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set the color of column. |
||
| 247 | * |
||
| 248 | * @param string $color |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function color($color) |
||
| 253 | { |
||
| 254 | return $this->style("color:$color;"); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get original column value. |
||
| 259 | * |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | public function getOriginal() |
||
| 263 | { |
||
| 264 | return $this->original; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get name of this column. |
||
| 269 | * |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | public function getName() |
||
| 273 | { |
||
| 274 | return $this->name; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Format label. |
||
| 279 | * |
||
| 280 | * @param $label |
||
| 281 | * |
||
| 282 | * @return mixed |
||
| 283 | */ |
||
| 284 | protected function formatLabel($label) |
||
| 285 | { |
||
| 286 | if ($label) { |
||
| 287 | return $label; |
||
| 288 | } |
||
| 289 | |||
| 290 | $label = ucfirst($this->name); |
||
| 291 | |||
| 292 | return __(str_replace(['.', '_'], ' ', $label)); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get label of the column. |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | public function getLabel() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Set relation. |
||
| 307 | * |
||
| 308 | * @param string $relation |
||
| 309 | * @param string $relationColumn |
||
| 310 | * |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function setRelation($relation, $relationColumn = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * If this column is relation column. |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | protected function isRelation() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Mark this column as sortable. |
||
| 333 | * |
||
| 334 | * @param null|string $cast |
||
| 335 | * |
||
| 336 | * @return Column|string |
||
| 337 | */ |
||
| 338 | public function sortable($cast = null) |
||
| 339 | { |
||
| 340 | return $this->addSorter($cast); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set cast name for sortable. |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | * |
||
| 348 | * @deprecated Use `$column->sortable($cast)` instead. |
||
| 349 | */ |
||
| 350 | public function cast($cast) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set help message for column. |
||
| 359 | * |
||
| 360 | * @param string $help |
||
| 361 | * |
||
| 362 | * @return $this|string |
||
| 363 | */ |
||
| 364 | public function help($help = '') |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set column filter. |
||
| 371 | * |
||
| 372 | * @param null $builder |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function filter($builder = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Add a display callback. |
||
| 383 | * |
||
| 384 | * @param Closure $callback |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | public function display(Closure $callback) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Display using display abstract. |
||
| 397 | * |
||
| 398 | * @param string $abstract |
||
| 399 | * @param array $arguments |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function displayUsing($abstract, $arguments = []) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Display column using array value map. |
||
| 419 | * |
||
| 420 | * @param array $values |
||
| 421 | * @param null $default |
||
| 422 | * |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function using(array $values, $default = null) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Replace output value with giving map. |
||
| 438 | * |
||
| 439 | * @param array $replacements |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function replace(array $replacements) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Render this column with the given view. |
||
| 456 | * |
||
| 457 | * @param string $view |
||
| 458 | * |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | public function view($view) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Hide this column by default. |
||
| 472 | * |
||
| 473 | * @return $this |
||
| 474 | */ |
||
| 475 | public function hide() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Add column to total-row. |
||
| 484 | * |
||
| 485 | * @param null $display |
||
| 486 | * |
||
| 487 | * @return $this |
||
| 488 | */ |
||
| 489 | public function totalRow($display = null) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Convert file size to a human readable format like `100mb`. |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | public function filesize() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Display the fields in the email format as gavatar. |
||
| 510 | * |
||
| 511 | * @param int $size |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function gravatar($size = 30) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Display field as a loading icon. |
||
| 530 | * |
||
| 531 | * @param array $values |
||
| 532 | * @param array $others |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function loading($values = [], $others = []) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Display column as an font-awesome icon based on it's value. |
||
| 551 | * |
||
| 552 | * @param array $setting |
||
| 553 | * @param string $default |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function icon(array $setting, $default = '') |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return a human readable format time. |
||
| 574 | * |
||
| 575 | * @param null $locale |
||
| 576 | * |
||
| 577 | * @return $this |
||
| 578 | */ |
||
| 579 | public function diffForHumans($locale = null) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * If has display callbacks. |
||
| 592 | * |
||
| 593 | * @return bool |
||
| 594 | */ |
||
| 595 | protected function hasDisplayCallbacks() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Call all of the "display" callbacks column. |
||
| 602 | * |
||
| 603 | * @param mixed $value |
||
| 604 | * @param int $key |
||
| 605 | * |
||
| 606 | * @return mixed |
||
| 607 | */ |
||
| 608 | protected function callDisplayCallbacks($value, $key) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Set original grid data to column. |
||
| 629 | * |
||
| 630 | * @param Closure $callback |
||
| 631 | * @param int $key |
||
| 632 | * |
||
| 633 | * @return Closure |
||
| 634 | */ |
||
| 635 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Fill all data to every column. |
||
| 644 | * |
||
| 645 | * @param array $data |
||
| 646 | * |
||
| 647 | * @return mixed |
||
| 648 | */ |
||
| 649 | public function fill(array $data) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * If current column is a defined column. |
||
| 673 | * |
||
| 674 | * @return bool |
||
| 675 | */ |
||
| 676 | protected function isDefinedColumn() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Use a defined column. |
||
| 683 | * |
||
| 684 | * @throws \Exception |
||
| 685 | */ |
||
| 686 | protected function useDefinedColumn() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Convert characters to HTML entities recursively. |
||
| 716 | * |
||
| 717 | * @param array|string $item |
||
| 718 | * |
||
| 719 | * @return mixed |
||
| 720 | */ |
||
| 721 | protected function htmlEntityEncode($item) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Find a displayer to display column. |
||
| 736 | * |
||
| 737 | * @param string $abstract |
||
| 738 | * @param array $arguments |
||
| 739 | * |
||
| 740 | * @return $this |
||
| 741 | */ |
||
| 742 | protected function resolveDisplayer($abstract, $arguments) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Call Illuminate/Support displayer. |
||
| 753 | * |
||
| 754 | * @param string $abstract |
||
| 755 | * @param array $arguments |
||
| 756 | * |
||
| 757 | * @return $this |
||
| 758 | */ |
||
| 759 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Call Builtin displayer. |
||
| 776 | * |
||
| 777 | * @param string $abstract |
||
| 778 | * @param array $arguments |
||
| 779 | * |
||
| 780 | * @return $this |
||
| 781 | */ |
||
| 782 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 807 | * |
||
| 808 | * Allow fluent calls on the Column object. |
||
| 809 | * |
||
| 810 | * @param string $method |
||
| 811 | * @param array $arguments |
||
| 812 | * |
||
| 813 | * @return $this |
||
| 814 | */ |
||
| 815 | public function __call($method, $arguments) |
||
| 828 | } |
||
| 829 |
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: