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 |
||
| 9 | class Column |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Grid |
||
| 13 | */ |
||
| 14 | protected $grid; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Name of column. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Label of column. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $label; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Original value of column. |
||
| 32 | * |
||
| 33 | * @var mixed |
||
| 34 | */ |
||
| 35 | protected $original; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Is column sortable. |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $sortable = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sort arguments. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $sort; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Attributes of column. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $attributes = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Value callback. |
||
| 60 | * |
||
| 61 | * @var \Closure |
||
| 62 | */ |
||
| 63 | protected $valueCallback; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Html callback. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $htmlCallbacks = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Relation name. |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $relation = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Relation column. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $relationColumn; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Original grid data. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected static $originalGridData = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $name |
||
| 95 | * @param string $label |
||
| 96 | */ |
||
| 97 | public function __construct($name, $label) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Grid $grid |
||
| 106 | */ |
||
| 107 | public function setGrid(Grid $grid) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get name of this column. |
||
| 114 | * |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function getName() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Format label. |
||
| 124 | * |
||
| 125 | * @param $label |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | protected function formatLabel($label) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get label of the column. |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function getLabel() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set relation. |
||
| 148 | * |
||
| 149 | * @param $relation |
||
| 150 | * |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function setRelation($relation) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * If this column is relation column. |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | protected function isRelation() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Mark this column as sortable. |
||
| 172 | * |
||
| 173 | * @return Column |
||
| 174 | */ |
||
| 175 | public function sortable() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Wrap value with badge. |
||
| 184 | * |
||
| 185 | * @param string $style |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function badge($style = 'red') |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Wrap value with label. |
||
| 200 | * |
||
| 201 | * @param string $style |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function label($style = 'success') |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Wrap value as a link. |
||
| 216 | * |
||
| 217 | * @param $href |
||
| 218 | * @param string $target |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function link($href = '', $target = '_blank') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Wrap value as a button. |
||
| 237 | * |
||
| 238 | * @param string $style |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function button($style = 'default') |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Wrap value as a progressbar. |
||
| 255 | * |
||
| 256 | * @param string $style |
||
| 257 | * @param string $size |
||
| 258 | * @param int $max |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function progressBar($style = 'primary', $size = 'sm', $max = 100) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Wrap value as a image. |
||
| 291 | * |
||
| 292 | * @param string $server |
||
| 293 | * @param int $width |
||
| 294 | * @param int $height |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function image($server = '', $width = 200, $height = 200) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Make the column editable. |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function editable() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a value callback. |
||
| 326 | * |
||
| 327 | * @param callable $callable |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function value(Closure $callable) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Alias for value method. |
||
| 340 | * |
||
| 341 | * @param callable $callable |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function display(Closure $callable) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * If has a value callback. |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | protected function hasValueCallback() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set html callback. |
||
| 362 | * |
||
| 363 | * @param $callback |
||
| 364 | */ |
||
| 365 | protected function htmlCallback($callback) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * If column has html callback. |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | protected function hasHtmlCallback() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Wrap value with callback. |
||
| 382 | * |
||
| 383 | * @param $value |
||
| 384 | * |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | protected function htmlWrap($value, $row = []) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Fill all data to every column. |
||
| 405 | * |
||
| 406 | * @param array $data |
||
| 407 | * |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | public function fill(array $data) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set original grid data to column. |
||
| 440 | * |
||
| 441 | * @param Closure $callback |
||
| 442 | * @param int $key |
||
| 443 | * |
||
| 444 | * @return Closure |
||
| 445 | */ |
||
| 446 | protected function bindOriginalRow(Closure $callback, $key) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Set original data for column. |
||
| 455 | * |
||
| 456 | * @param array $input |
||
| 457 | */ |
||
| 458 | public static function setOriginalGridData(array $input) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Convert characters to HTML entities recursively. |
||
| 465 | * |
||
| 466 | * @param array|string $item |
||
| 467 | * |
||
| 468 | * @return mixed |
||
| 469 | */ |
||
| 470 | protected function htmlEntityEncode($item) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Create the column sorter. |
||
| 485 | * |
||
| 486 | * @return string|void |
||
| 487 | */ |
||
| 488 | public function sorter() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Determine if this column is currently sorted. |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | protected function isSorted() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param string $method |
||
| 528 | * @param array $arguments |
||
| 529 | * |
||
| 530 | * @return $this |
||
| 531 | */ |
||
| 532 | public function __call($method, $arguments) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param $style |
||
| 546 | * @return array|string |
||
| 547 | */ |
||
| 548 | private function _resolveClassName($style) |
||
| 564 | } |
||
| 565 |