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) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set title attribute of an array if not set. |
||
| 371 | * |
||
| 372 | * @param string $title |
||
| 373 | * @param array $attributes |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function setTitle($title, array $attributes) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Convert string into a readable title. |
||
| 387 | * |
||
| 388 | * @param string $title |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function getQualifiedTitle($title) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Add a checkbox column. |
||
| 398 | * |
||
| 399 | * @param array $attributes |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | public function addCheckbox(array $attributes = []) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add a action column. |
||
| 422 | * |
||
| 423 | * @param array $attributes |
||
| 424 | * @return $this |
||
| 425 | */ |
||
| 426 | public function addAction(array $attributes = []) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Add a index column. |
||
| 447 | * |
||
| 448 | * @param array $attributes |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function addIndex(array $attributes = []) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Setup ajax parameter for datatables pipeline plugin. |
||
| 473 | * |
||
| 474 | * @param string $url |
||
| 475 | * @param string $pages |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function pipeline($url, $pages) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Setup "ajax" parameter with POST method. |
||
| 487 | * |
||
| 488 | * @param string|array $attributes |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | public function postAjax($attributes = '') |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Setup ajax parameter. |
||
| 506 | * |
||
| 507 | * @param string|array $attributes |
||
| 508 | * @return $this |
||
| 509 | */ |
||
| 510 | public function ajax($attributes = '') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Generate DataTable's table html. |
||
| 519 | * |
||
| 520 | * @param array $attributes |
||
| 521 | * @param bool $drawFooter |
||
| 522 | * @param bool $drawSearch |
||
| 523 | * @return \Illuminate\Support\HtmlString |
||
| 524 | */ |
||
| 525 | public function table(array $attributes = [], $drawFooter = false, $drawSearch = false) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Compile table headers and to support responsive extension. |
||
| 547 | * |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | private function compileTableHeaders() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Compile table search headers. |
||
| 566 | * |
||
| 567 | * @return array |
||
| 568 | */ |
||
| 569 | private function compileTableSearchHeaders() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Compile table footer contents. |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | private function compileTableFooter() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Configure DataTable's parameters. |
||
| 603 | * |
||
| 604 | * @param array $attributes |
||
| 605 | * @return $this |
||
| 606 | */ |
||
| 607 | public function parameters(array $attributes = []) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Set custom javascript template. |
||
| 616 | * |
||
| 617 | * @param string $template |
||
| 618 | * @return $this |
||
| 619 | */ |
||
| 620 | public function setTemplate($template) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get collection of columns. |
||
| 629 | * |
||
| 630 | * @return \Illuminate\Support\Collection |
||
| 631 | */ |
||
| 632 | public function getColumns() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Remove column by name. |
||
| 639 | * |
||
| 640 | * @param array $names |
||
| 641 | * @return $this |
||
| 642 | */ |
||
| 643 | public function removeColumn(...$names) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Minify ajax url generated when using get request |
||
| 656 | * by deleting unnecessary url params. |
||
| 657 | * |
||
| 658 | * @param string $url |
||
| 659 | * @param string $script |
||
| 660 | * @param array $data |
||
| 661 | * @return $this |
||
| 662 | */ |
||
| 663 | public function minifiedAjax($url = '', $script = null, $data = []) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Make a data script to be appended on ajax request of dataTables. |
||
| 698 | * |
||
| 699 | * @param array $data |
||
| 700 | * @return string |
||
| 701 | */ |
||
| 702 | protected function makeDataScript(array $data) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Compile DataTable callback value. |
||
| 714 | * |
||
| 715 | * @param mixed $callback |
||
| 716 | * @return mixed|string |
||
| 717 | */ |
||
| 718 | private function compileCallback($callback) |
||
| 728 | } |
||
| 729 |
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..