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 |
||
| 14 | class Column |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var Grid |
||
| 18 | */ |
||
| 19 | protected $grid; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Name of column. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Label of column. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $label; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Original value of column. |
||
| 37 | * |
||
| 38 | * @var mixed |
||
| 39 | */ |
||
| 40 | protected $original; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Is column sortable. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $sortable = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Sort arguments. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $sort; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Cast Name. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $cast; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Attributes of column. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $attributes = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Relation name. |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $relation = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Relation column. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $relationColumn; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Original grid data. |
||
| 86 | * |
||
| 87 | * @var Collection |
||
| 88 | */ |
||
| 89 | protected static $originalGridModels; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var []Closure |
||
| 93 | */ |
||
| 94 | protected $displayCallbacks = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Displayers for grid column. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | public static $displayers = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Defined columns. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | public static $defined = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | protected static $htmlAttributes = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Model |
||
| 117 | */ |
||
| 118 | protected static $model; |
||
| 119 | |||
| 120 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $name |
||
| 124 | * @param string $label |
||
| 125 | */ |
||
| 126 | public function __construct($name, $label) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Extend column displayer. |
||
| 135 | * |
||
| 136 | * @param $name |
||
| 137 | * @param $displayer |
||
| 138 | */ |
||
| 139 | public static function extend($name, $displayer) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Define a column globally. |
||
| 146 | * |
||
| 147 | * @param string $name |
||
| 148 | * @param mixed $definition |
||
| 149 | */ |
||
| 150 | public static function define($name, $definition) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set grid instance for column. |
||
| 157 | * |
||
| 158 | * @param Grid $grid |
||
| 159 | */ |
||
| 160 | public function setGrid(Grid $grid) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set model for column. |
||
| 169 | * |
||
| 170 | * @param $model |
||
| 171 | */ |
||
| 172 | public function setModel($model) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Set original data for column. |
||
| 181 | * |
||
| 182 | * @param Collection $collection |
||
| 183 | */ |
||
| 184 | public static function setOriginalGridModels(Collection $collection) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set column attributes. |
||
| 191 | * |
||
| 192 | * @param array $attributes |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function setAttributes($attributes = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get column attributes. |
||
| 205 | * |
||
| 206 | * @param string $name |
||
| 207 | * |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | public static function getAttributes($name) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set style of this column. |
||
| 217 | * |
||
| 218 | * @param string $style |
||
| 219 | * |
||
| 220 | * @return Column |
||
| 221 | */ |
||
| 222 | public function style($style) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get name of this column. |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getName() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Format label. |
||
| 239 | * |
||
| 240 | * @param $label |
||
| 241 | * |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | protected function formatLabel($label) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get label of the column. |
||
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | public function getLabel() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set relation. |
||
| 263 | * |
||
| 264 | * @param string $relation |
||
| 265 | * @param string $relationColumn |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function setRelation($relation, $relationColumn = null) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * If this column is relation column. |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | protected function isRelation() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set sort value. |
||
| 289 | * |
||
| 290 | * @param bool $sort |
||
| 291 | * |
||
| 292 | * @return Column |
||
| 293 | */ |
||
| 294 | public function sort($sort) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Mark this column as sortable. |
||
| 303 | * |
||
| 304 | * @return Column |
||
| 305 | */ |
||
| 306 | public function sortable() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set cast name for sortable. |
||
| 313 | * |
||
| 314 | * @return Column |
||
| 315 | */ |
||
| 316 | public function cast($cast) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Add a display callback. |
||
| 325 | * |
||
| 326 | * @param Closure $callback |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function display(Closure $callback) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Display using display abstract. |
||
| 339 | * |
||
| 340 | * @param string $abstract |
||
| 341 | * @param array $arguments |
||
| 342 | * |
||
| 343 | * @return Column |
||
| 344 | */ |
||
| 345 | public function displayUsing($abstract, $arguments = []) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Display column using array value map. |
||
| 361 | * |
||
| 362 | * @param array $values |
||
| 363 | * @param null $default |
||
| 364 | * |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | View Code Duplication | public function using(array $values, $default = null) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Render this column with the given view. |
||
| 380 | * |
||
| 381 | * @param string $view |
||
| 382 | * |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function view($view) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Add column to total-row. |
||
| 396 | * |
||
| 397 | * @param null $display |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | public function totalRow($display = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * If has display callbacks. |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | protected function hasDisplayCallbacks() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Call all of the "display" callbacks column. |
||
| 420 | * |
||
| 421 | * @param mixed $value |
||
| 422 | * @param int $key |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | protected function callDisplayCallbacks($value, $key) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set original grid data to column. |
||
| 447 | * |
||
| 448 | * @param Closure $callback |
||
| 449 | * @param int $key |
||
| 450 | * |
||
| 451 | * @return Closure |
||
| 452 | */ |
||
| 453 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Fill all data to every column. |
||
| 462 | * |
||
| 463 | * @param array $data |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | public function fill(array $data) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * If current column is a defined column. |
||
| 491 | * |
||
| 492 | * @return bool |
||
| 493 | */ |
||
| 494 | protected function isDefinedColumn() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Use a defined column. |
||
| 501 | * |
||
| 502 | * @throws \Exception |
||
| 503 | */ |
||
| 504 | protected function useDefinedColumn() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Convert characters to HTML entities recursively. |
||
| 534 | * |
||
| 535 | * @param array|string $item |
||
| 536 | * |
||
| 537 | * @return mixed |
||
| 538 | */ |
||
| 539 | protected function htmlEntityEncode($item) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Create the column sorter. |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | public function sorter() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Determine if this column is currently sorted. |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | protected function isSorted() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Find a displayer to display column. |
||
| 603 | * |
||
| 604 | * @param string $abstract |
||
| 605 | * @param array $arguments |
||
| 606 | * |
||
| 607 | * @return Column |
||
| 608 | */ |
||
| 609 | protected function resolveDisplayer($abstract, $arguments) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Call Illuminate/Support displayer. |
||
| 620 | * |
||
| 621 | * @param string $abstract |
||
| 622 | * @param array $arguments |
||
| 623 | * |
||
| 624 | * @return Column |
||
| 625 | */ |
||
| 626 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Call Builtin displayer. |
||
| 643 | * |
||
| 644 | * @param string $abstract |
||
| 645 | * @param array $arguments |
||
| 646 | * |
||
| 647 | * @return Column |
||
| 648 | */ |
||
| 649 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 674 | * |
||
| 675 | * Allow fluent calls on the Column object. |
||
| 676 | * |
||
| 677 | * @param string $method |
||
| 678 | * @param array $arguments |
||
| 679 | * |
||
| 680 | * @return $this |
||
| 681 | */ |
||
| 682 | public function __call($method, $arguments) |
||
| 695 | } |
||
| 696 |
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: