Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Builder |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var Collection |
||
| 24 | */ |
||
| 25 | public $collection; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Repository |
||
| 29 | */ |
||
| 30 | public $config; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Factory |
||
| 34 | */ |
||
| 35 | public $view; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var HtmlBuilder |
||
| 39 | */ |
||
| 40 | public $html; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var UrlGenerator |
||
| 44 | */ |
||
| 45 | public $url; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var FormBuilder |
||
| 49 | */ |
||
| 50 | public $form; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string|array |
||
| 54 | */ |
||
| 55 | protected $ajax = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $tableAttributes = ['class' => 'table', 'id' => 'dataTableBuilder']; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $template = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $attributes = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Lists of valid DataTables Callbacks. |
||
| 74 | * |
||
| 75 | * @link https://datatables.net/reference/option/. |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $validCallbacks = [ |
||
| 79 | 'createdRow', |
||
| 80 | 'drawCallback', |
||
| 81 | 'footerCallback', |
||
| 82 | 'formatNumber', |
||
| 83 | 'headerCallback', |
||
| 84 | 'infoCallback', |
||
| 85 | 'initComplete', |
||
| 86 | 'preDrawCallback', |
||
| 87 | 'rowCallback', |
||
| 88 | 'stateLoadCallback', |
||
| 89 | 'stateLoaded', |
||
| 90 | 'stateLoadParams', |
||
| 91 | 'stateSaveCallback', |
||
| 92 | 'stateSaveParams', |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param Repository $config |
||
| 97 | * @param Factory $view |
||
| 98 | * @param HtmlBuilder $html |
||
| 99 | * @param UrlGenerator $url |
||
| 100 | * @param FormBuilder $form |
||
| 101 | */ |
||
| 102 | public function __construct( |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Generate DataTable javascript. |
||
| 119 | * |
||
| 120 | * @param null $script |
||
| 121 | * @param array $attributes |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get generated raw scripts. |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function generateScripts() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Generate DataTables js parameters. |
||
| 155 | * |
||
| 156 | * @param array $attributes |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function parameterize($attributes = []) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Encode ajax data function param. |
||
| 178 | * |
||
| 179 | * @param array $parameters |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | protected function encodeAjaxDataFunction($parameters) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Encode columns render function. |
||
| 195 | * |
||
| 196 | * @param array $parameters |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function encodeColumnFunctions(array $parameters) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Encode DataTables callbacks function. |
||
| 218 | * |
||
| 219 | * @param array $parameters |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function encodeCallbackFunctions(array $parameters) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Compile DataTable callback value. |
||
| 237 | * |
||
| 238 | * @param mixed $callback |
||
| 239 | * @return mixed|string |
||
| 240 | */ |
||
| 241 | private function compileCallback($callback) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Decode ajax data method. |
||
| 254 | * |
||
| 255 | * @param string $function |
||
| 256 | * @param string $json |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | protected function decodeAjaxDataFunction($function, $json) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Decode columns render functions. |
||
| 266 | * |
||
| 267 | * @param array $columnFunctions |
||
| 268 | * @param string $json |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Decode DataTables callbacks function. |
||
| 282 | * |
||
| 283 | * @param array $callbackFunctions |
||
| 284 | * @param string $json |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get javascript template to use. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function template() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add a column in collection using attributes. |
||
| 310 | * |
||
| 311 | * @param array $attributes |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function addColumn(array $attributes) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Add a Column object in collection. |
||
| 323 | * |
||
| 324 | * @param \Yajra\Datatables\Html\Column $column |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function add(Column $column) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set datatables columns from array definition. |
||
| 336 | * |
||
| 337 | * @param array $columns |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function columns(array $columns) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set title attribute of an array if not set. |
||
| 365 | * |
||
| 366 | * @param string $title |
||
| 367 | * @param array $attributes |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function setTitle($title, array $attributes) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Convert string into a readable title. |
||
| 381 | * |
||
| 382 | * @param string $title |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function getQualifiedTitle($title) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Add a checkbox column. |
||
| 392 | * |
||
| 393 | * @param array $attributes |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | public function addCheckbox(array $attributes = []) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Add a action column. |
||
| 416 | * |
||
| 417 | * @param array $attributes |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function addAction(array $attributes = []) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Add a index column. |
||
| 441 | * |
||
| 442 | * @param array $attributes |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | public function addIndex(array $attributes = []) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Setup ajax parameter |
||
| 467 | * |
||
| 468 | * @param string|array $attributes |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function ajax($attributes) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Generate DataTable's table html. |
||
| 480 | * |
||
| 481 | * @param array $attributes |
||
| 482 | * @param bool $drawFooter |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | public function table(array $attributes = [], $drawFooter = false) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Compile table headers and to support responsive extension. |
||
| 505 | * |
||
| 506 | * @return array |
||
| 507 | */ |
||
| 508 | private function compileTableHeaders() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Compile table footer contents. |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | private function compileTableFooter() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Configure DataTable's parameters. |
||
| 538 | * |
||
| 539 | * @param array $attributes |
||
| 540 | * @return $this |
||
| 541 | */ |
||
| 542 | public function parameters(array $attributes = []) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Set custom javascript template. |
||
| 551 | * |
||
| 552 | * @param string $template |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | public function setTemplate($template) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get collection of columns. |
||
| 564 | * |
||
| 565 | * @return Collection |
||
| 566 | */ |
||
| 567 | public function getColumns() |
||
| 571 | } |
||
| 572 |