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 |
||
| 14 | class Builder |
||
| 15 | { |
||
| 16 | use Macroable; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Collection |
||
| 20 | */ |
||
| 21 | public $collection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Repository |
||
| 25 | */ |
||
| 26 | public $config; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Factory |
||
| 30 | */ |
||
| 31 | public $view; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var HtmlBuilder |
||
| 35 | */ |
||
| 36 | public $html; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string|array |
||
| 40 | */ |
||
| 41 | protected $ajax = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $tableAttributes = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $template = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $attributes = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param Repository $config |
||
| 60 | * @param Factory $view |
||
| 61 | * @param HtmlBuilder $html |
||
| 62 | */ |
||
| 63 | public function __construct(Repository $config, Factory $view, HtmlBuilder $html) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Generate DataTable javascript. |
||
| 74 | * |
||
| 75 | * @param null $script |
||
| 76 | * @param array $attributes |
||
| 77 | * @return \Illuminate\Support\HtmlString |
||
| 78 | */ |
||
| 79 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get generated raw scripts. |
||
| 89 | * |
||
| 90 | * @return \Illuminate\Support\HtmlString |
||
| 91 | */ |
||
| 92 | public function generateScripts() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get generated json configuration. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function generateJson() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Generate DataTables js parameters. |
||
| 125 | * |
||
| 126 | * @param array $attributes |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function parameterize($attributes = []) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Check if given key & value is a valid callback js function. |
||
| 157 | * |
||
| 158 | * @param string $value |
||
| 159 | * @param string $key |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | protected function isCallbackFunction($value, $key) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get javascript template to use. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | protected function template() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieves HTML table attribute value. |
||
| 181 | * |
||
| 182 | * @param string $attribute |
||
| 183 | * @return mixed |
||
| 184 | * @throws \Exception |
||
| 185 | */ |
||
| 186 | public function getTableAttribute($attribute) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get table computed table attributes. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getTableAttributes() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets multiple HTML table attributes at once. |
||
| 207 | * |
||
| 208 | * @param array $attributes |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setTableAttributes(array $attributes) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Sets HTML table "id" attribute. |
||
| 222 | * |
||
| 223 | * @param string $id |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setTableId($id) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Sets HTML table attribute(s). |
||
| 233 | * |
||
| 234 | * @param string|array $attribute |
||
| 235 | * @param mixed $value |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setTableAttribute($attribute, $value = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Add class names to the "class" attribute of HTML table. |
||
| 251 | * |
||
| 252 | * @param string|array $class |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function addTableClass($class) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Remove class names from the "class" attribute of HTML table. |
||
| 268 | * |
||
| 269 | * @param string|array $class |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function removeTableClass($class) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Add a column in collection usingsl attributes. |
||
| 288 | * |
||
| 289 | * @param array $attributes |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function addColumn(array $attributes) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Add a Column object at the beginning of collection. |
||
| 301 | * |
||
| 302 | * @param \Yajra\DataTables\Html\Column $column |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function addBefore(Column $column) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Add a column at the beginning of collection using attributes. |
||
| 314 | * |
||
| 315 | * @param array $attributes |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function addColumnBefore(array $attributes) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Add a Column object in collection. |
||
| 327 | * |
||
| 328 | * @param \Yajra\DataTables\Html\Column $column |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function add(Column $column) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set datatables columns from array definition. |
||
| 340 | * |
||
| 341 | * @param array $columns |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function columns(array $columns) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set title attribute of an array if not set. |
||
| 377 | * |
||
| 378 | * @param string $title |
||
| 379 | * @param array $attributes |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function setTitle($title, array $attributes) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Convert string into a readable title. |
||
| 393 | * |
||
| 394 | * @param string $title |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function getQualifiedTitle($title) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Add a checkbox column. |
||
| 404 | * |
||
| 405 | * @param array $attributes |
||
| 406 | * @param bool|int $position true to prepend, false to append or a zero-based index for positioning |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | public function addCheckbox(array $attributes = [], $position = false) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Add a action column. |
||
| 437 | * |
||
| 438 | * @param array $attributes |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | public function addAction(array $attributes = []) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Add a index column. |
||
| 462 | * |
||
| 463 | * @param array $attributes |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function addIndex(array $attributes = []) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Setup ajax parameter for datatables pipeline plugin. |
||
| 488 | * |
||
| 489 | * @param string $url |
||
| 490 | * @param string $pages |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | public function pipeline($url, $pages) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Setup "ajax" parameter with POST method. |
||
| 502 | * |
||
| 503 | * @param string|array $attributes |
||
| 504 | * @return $this |
||
| 505 | */ |
||
| 506 | public function postAjax($attributes = '') |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Setup ajax parameter. |
||
| 521 | * |
||
| 522 | * @param string|array $attributes |
||
| 523 | * @return $this |
||
| 524 | */ |
||
| 525 | public function ajax($attributes = '') |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Generate DataTable's table html. |
||
| 534 | * |
||
| 535 | * @param array $attributes |
||
| 536 | * @param bool $drawFooter |
||
| 537 | * @param bool $drawSearch |
||
| 538 | * @return \Illuminate\Support\HtmlString |
||
| 539 | */ |
||
| 540 | public function table(array $attributes = [], $drawFooter = false, $drawSearch = false) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Compile table headers and to support responsive extension. |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | private function compileTableHeaders() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Compile table search headers. |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | private function compileTableSearchHeaders() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Compile table footer contents. |
||
| 596 | * |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | private function compileTableFooter() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Configure DataTable's parameters. |
||
| 618 | * |
||
| 619 | * @param array $attributes |
||
| 620 | * @return $this |
||
| 621 | */ |
||
| 622 | public function parameters(array $attributes = []) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Set custom javascript template. |
||
| 631 | * |
||
| 632 | * @param string $template |
||
| 633 | * @return $this |
||
| 634 | */ |
||
| 635 | public function setTemplate($template) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get collection of columns. |
||
| 644 | * |
||
| 645 | * @return \Illuminate\Support\Collection |
||
| 646 | */ |
||
| 647 | public function getColumns() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Remove column by name. |
||
| 654 | * |
||
| 655 | * @param array $names |
||
| 656 | * @return $this |
||
| 657 | */ |
||
| 658 | public function removeColumn(...$names) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Minify ajax url generated when using get request |
||
| 671 | * by deleting unnecessary url params. |
||
| 672 | * |
||
| 673 | * @param string $url |
||
| 674 | * @param string $script |
||
| 675 | * @param array $data |
||
| 676 | * @param array $ajaxParameters |
||
| 677 | * @return $this |
||
| 678 | */ |
||
| 679 | public function minifiedAjax($url = '', $script = null, $data = [], $ajaxParameters = []) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Make a data script to be appended on ajax request of dataTables. |
||
| 716 | * |
||
| 717 | * @param array $data |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | protected function makeDataScript(array $data) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Compile DataTable callback value. |
||
| 732 | * |
||
| 733 | * @param mixed $callback |
||
| 734 | * @return mixed|string |
||
| 735 | */ |
||
| 736 | private function compileCallback($callback) |
||
| 746 | } |
||
| 747 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..