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 | * Attributes of column. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $attributes = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Relation name. |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $relation = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Relation column. |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $relationColumn; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Original grid data. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected static $originalGridData = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var []Closure |
||
| 86 | */ |
||
| 87 | protected $displayCallbacks = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Displayers for grid column. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public static $displayers = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Defined columns. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | public static $defined = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected static $htmlAttributes = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var |
||
| 110 | */ |
||
| 111 | protected static $model; |
||
| 112 | |||
| 113 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $name |
||
| 117 | * @param string $label |
||
| 118 | */ |
||
| 119 | public function __construct($name, $label) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Extend column displayer. |
||
| 128 | * |
||
| 129 | * @param $name |
||
| 130 | * @param $displayer |
||
| 131 | */ |
||
| 132 | public static function extend($name, $displayer) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Define a column globally. |
||
| 139 | * |
||
| 140 | * @param string $name |
||
| 141 | * @param mixed $definition |
||
| 142 | */ |
||
| 143 | public static function define($name, $definition) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set grid instance for column. |
||
| 150 | * |
||
| 151 | * @param Grid $grid |
||
| 152 | */ |
||
| 153 | public function setGrid(Grid $grid) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set model for column. |
||
| 162 | * |
||
| 163 | * @param $model |
||
| 164 | */ |
||
| 165 | public function setModel($model) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set original data for column. |
||
| 174 | * |
||
| 175 | * @param array $input |
||
| 176 | */ |
||
| 177 | public static function setOriginalGridData(array $input) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set column attributes. |
||
| 184 | * |
||
| 185 | * @param array $attributes |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function setAttributes($attributes = []) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get column attributes. |
||
| 198 | * |
||
| 199 | * @param string $name |
||
| 200 | * |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | public static function getAttributes($name) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set style of this column. |
||
| 210 | * |
||
| 211 | * @param string $style |
||
| 212 | * |
||
| 213 | * @return Column |
||
| 214 | */ |
||
| 215 | public function style($style) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get name of this column. |
||
| 222 | * |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | public function getName() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Format label. |
||
| 232 | * |
||
| 233 | * @param $label |
||
| 234 | * |
||
| 235 | * @return mixed |
||
| 236 | */ |
||
| 237 | protected function formatLabel($label) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get label of the column. |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | public function getLabel() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set relation. |
||
| 256 | * |
||
| 257 | * @param string $relation |
||
| 258 | * @param string $relationColumn |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function setRelation($relation, $relationColumn = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * If this column is relation column. |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | protected function isRelation() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Mark this column as sortable. |
||
| 282 | * |
||
| 283 | * @return Column |
||
| 284 | */ |
||
| 285 | public function sortable() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add a display callback. |
||
| 294 | * |
||
| 295 | * @param Closure $callback |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function display(Closure $callback) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * If has display callbacks. |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | protected function hasDisplayCallbacks() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Call all of the "display" callbacks column. |
||
| 318 | * |
||
| 319 | * @param mixed $value |
||
| 320 | * @param int $key |
||
| 321 | * |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | protected function callDisplayCallbacks($value, $key) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set original grid data to column. |
||
| 336 | * |
||
| 337 | * @param Closure $callback |
||
| 338 | * @param int $key |
||
| 339 | * |
||
| 340 | * @return Closure |
||
| 341 | */ |
||
| 342 | protected function bindOriginalRow(Closure $callback, $key) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Fill all data to every column. |
||
| 351 | * |
||
| 352 | * @param array $data |
||
| 353 | * |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | public function fill(array $data) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * If current column is a defined column. |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | protected function isDefinedColumn() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Use a defined column. |
||
| 390 | * |
||
| 391 | * @throws \Exception |
||
| 392 | */ |
||
| 393 | protected function useDefinedColumn() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Convert characters to HTML entities recursively. |
||
| 422 | * |
||
| 423 | * @param array|string $item |
||
| 424 | * |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | protected function htmlEntityEncode($item) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Create the column sorter. |
||
| 442 | * |
||
| 443 | * @return string|void |
||
| 444 | */ |
||
| 445 | public function sorter() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Determine if this column is currently sorted. |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | protected function isSorted() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Find a displayer to display column. |
||
| 485 | * |
||
| 486 | * @param string $abstract |
||
| 487 | * @param array $arguments |
||
| 488 | * |
||
| 489 | * @return Column |
||
| 490 | */ |
||
| 491 | protected function resolveDisplayer($abstract, $arguments) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Call Illuminate/Support displayer. |
||
| 502 | * |
||
| 503 | * @param string $abstract |
||
| 504 | * @param array $arguments |
||
| 505 | * |
||
| 506 | * @return Column |
||
| 507 | */ |
||
| 508 | protected function callSupportDisplayer($abstract, $arguments) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Call Builtin displayer. |
||
| 525 | * |
||
| 526 | * @param string $abstract |
||
| 527 | * @param array $arguments |
||
| 528 | * |
||
| 529 | * @return Column |
||
| 530 | */ |
||
| 531 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
| 569 | * |
||
| 570 | * Allow fluent calls on the Column object. |
||
| 571 | * |
||
| 572 | * @param string $method |
||
| 573 | * @param array $arguments |
||
| 574 | * |
||
| 575 | * @return $this |
||
| 576 | */ |
||
| 577 | public function __call($method, $arguments) |
||
| 591 | } |
||
| 592 |
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: