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 |
||
| 13 | class Column |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Grid |
||
| 17 | */ |
||
| 18 | protected $grid; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Name of column. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $name; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Label of column. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $label; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Original value of column. |
||
| 36 | * |
||
| 37 | * @var mixed |
||
| 38 | */ |
||
| 39 | protected $original; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Is column sortable. |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $sortable = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Sort arguments. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $sort; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Attributes of column. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $attributes = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Relation name. |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $relation = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Relation column. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $relationColumn; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Original grid data. |
||
| 78 | * |
||
| 79 | * @var Collection |
||
| 80 | */ |
||
| 81 | protected static $originalGridModels; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var []Closure |
||
| 85 | */ |
||
| 86 | protected $displayCallbacks = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Displayers for grid column. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | public static $displayers = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Defined columns. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | public static $defined = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected static $htmlAttributes = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Model |
||
| 109 | */ |
||
| 110 | protected static $model; |
||
| 111 | |||
| 112 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $name |
||
| 116 | * @param string $label |
||
| 117 | */ |
||
| 118 | public function __construct($name, $label) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Extend column displayer. |
||
| 127 | * |
||
| 128 | * @param $name |
||
| 129 | * @param $displayer |
||
| 130 | */ |
||
| 131 | public static function extend($name, $displayer) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Define a column globally. |
||
| 138 | * |
||
| 139 | * @param string $name |
||
| 140 | * @param mixed $definition |
||
| 141 | */ |
||
| 142 | public static function define($name, $definition) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set grid instance for column. |
||
| 149 | * |
||
| 150 | * @param Grid $grid |
||
| 151 | */ |
||
| 152 | public function setGrid(Grid $grid) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set model for column. |
||
| 161 | * |
||
| 162 | * @param $model |
||
| 163 | */ |
||
| 164 | public function setModel($model) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set original data for column. |
||
| 173 | * |
||
| 174 | * @param Collection $collection |
||
| 175 | */ |
||
| 176 | public static function setOriginalGridModels(Collection $collection) |
||
| 177 | { |
||
| 178 | static::$originalGridModels = $collection; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set column attributes. |
||
| 183 | * |
||
| 184 | * @param array $attributes |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function setAttributes($attributes = []) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get column attributes. |
||
| 197 | * |
||
| 198 | * @param string $name |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public static function getAttributes($name) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set style of this column. |
||
| 209 | * |
||
| 210 | * @param string $style |
||
| 211 | * |
||
| 212 | * @return Column |
||
| 213 | */ |
||
| 214 | public function style($style) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get name of this column. |
||
| 221 | * |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function getName() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Format label. |
||
| 231 | * |
||
| 232 | * @param $label |
||
| 233 | * |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | protected function formatLabel($label) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get label of the column. |
||
| 245 | * |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public function getLabel() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set relation. |
||
| 255 | * |
||
| 256 | * @param string $relation |
||
| 257 | * @param string $relationColumn |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function setRelation($relation, $relationColumn = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * If this column is relation column. |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | protected function isRelation() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set sort value. |
||
| 281 | * |
||
| 282 | * @param bool $sort |
||
| 283 | * |
||
| 284 | * @return Column |
||
| 285 | */ |
||
| 286 | public function sort($sort) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Mark this column as sortable. |
||
| 295 | * |
||
| 296 | * @return Column |
||
| 297 | */ |
||
| 298 | public function sortable() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Add a display callback. |
||
| 305 | * |
||
| 306 | * @param Closure $callback |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function display(Closure $callback) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Display using display abstract. |
||
| 319 | * |
||
| 320 | * @param string $abstract |
||
| 321 | * @param array $arguments |
||
| 322 | * |
||
| 323 | * @return Column |
||
| 324 | */ |
||
| 325 | public function displayUsing($abstract, $arguments = []) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Display column using array value map. |
||
| 341 | * |
||
| 342 | * @param array $values |
||
| 343 | * @param null $default |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function using(array $values, $default = null) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Render this column with the given view. |
||
| 360 | * |
||
| 361 | * @param string $view |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function view($view) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * If has display callbacks. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | protected function hasDisplayCallbacks() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Call all of the "display" callbacks column. |
||
| 386 | * |
||
| 387 | * @param mixed $value |
||
| 388 | * @param int $key |
||
| 389 | * |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | protected function callDisplayCallbacks($value, $key) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set original grid data to column. |
||
| 413 | * |
||
| 414 | * @param Closure $callback |
||
| 415 | * @param int $key |
||
| 416 | * |
||
| 417 | * @return Closure |
||
| 418 | */ |
||
| 419 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Fill all data to every column. |
||
| 428 | * |
||
| 429 | * @param array $data |
||
| 430 | * |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function fill(array $data) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * If current column is a defined column. |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | protected function isDefinedColumn() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Use a defined column. |
||
| 467 | * |
||
| 468 | * @throws \Exception |
||
| 469 | */ |
||
| 470 | protected function useDefinedColumn() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Convert characters to HTML entities recursively. |
||
| 500 | * |
||
| 501 | * @param array|string $item |
||
| 502 | * |
||
| 503 | * @return mixed |
||
| 504 | */ |
||
| 505 | protected function htmlEntityEncode($item) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Create the column sorter. |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function sorter() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Determine if this column is currently sorted. |
||
| 547 | * |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | protected function isSorted() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Find a displayer to display column. |
||
| 563 | * |
||
| 564 | * @param string $abstract |
||
| 565 | * @param array $arguments |
||
| 566 | * |
||
| 567 | * @return Column |
||
| 568 | */ |
||
| 569 | protected function resolveDisplayer($abstract, $arguments) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Call Illuminate/Support displayer. |
||
| 580 | * |
||
| 581 | * @param string $abstract |
||
| 582 | * @param array $arguments |
||
| 583 | * |
||
| 584 | * @return Column |
||
| 585 | */ |
||
| 586 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Call Builtin displayer. |
||
| 603 | * |
||
| 604 | * @param string $abstract |
||
| 605 | * @param array $arguments |
||
| 606 | * |
||
| 607 | * @return Column |
||
| 608 | */ |
||
| 609 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 634 | * |
||
| 635 | * Allow fluent calls on the Column object. |
||
| 636 | * |
||
| 637 | * @param string $method |
||
| 638 | * @param array $arguments |
||
| 639 | * |
||
| 640 | * @return $this |
||
| 641 | */ |
||
| 642 | public function __call($method, $arguments) |
||
| 655 | } |
||
| 656 |
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: