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 = []) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Encode ajax data function param. |
||
| 196 | * |
||
| 197 | * @param array $parameters |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | protected function encodeAjaxDataFunction($parameters) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Encode columns render function. |
||
| 213 | * |
||
| 214 | * @param array $parameters |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected function encodeColumnFunctions(array $parameters) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Encode DataTables callbacks function. |
||
| 236 | * |
||
| 237 | * @param array $parameters |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | protected function encodeCallbackFunctions(array $parameters) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Compile DataTable callback value. |
||
| 255 | * |
||
| 256 | * @param mixed $callback |
||
| 257 | * @return mixed|string |
||
| 258 | */ |
||
| 259 | private function compileCallback($callback) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Decode ajax data method. |
||
| 272 | * |
||
| 273 | * @param string $function |
||
| 274 | * @param string $json |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | protected function decodeAjaxDataFunction($function, $json) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Decode columns render functions. |
||
| 284 | * |
||
| 285 | * @param array $columnFunctions |
||
| 286 | * @param string $json |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Decode DataTables callbacks function. |
||
| 300 | * |
||
| 301 | * @param array $callbackFunctions |
||
| 302 | * @param string $json |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get javascript template to use. |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | protected function template() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Sets HTML table attribute(s). |
||
| 328 | * |
||
| 329 | * @param string|array $attribute |
||
| 330 | * @param mixed $value |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setTableAttribute($attribute, $value = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Sets multiple HTML table attributes at once. |
||
| 346 | * |
||
| 347 | * @param array $attributes |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setTableAttributes(array $attributes) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Retrieves HTML table attribute value. |
||
| 361 | * |
||
| 362 | * @param string $attribute |
||
| 363 | * @return mixed |
||
| 364 | * @throws \Exception |
||
| 365 | */ |
||
| 366 | public function getTableAttribute($attribute) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Add a column in collection using attributes. |
||
| 377 | * |
||
| 378 | * @param array $attributes |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function addColumn(array $attributes) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Add a Column object at the beginning of collection |
||
| 390 | * |
||
| 391 | * @param \Yajra\Datatables\Html\Column $column |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function addBefore(Column $column) |
||
| 395 | { |
||
| 396 | $this->collection->prepend($column); |
||
| 397 | |||
| 398 | return $this; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Add a column at the beginning of collection using attributes. |
||
| 403 | * |
||
| 404 | * @param array $attributes |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function addColumnBefore(array $attributes) |
||
| 408 | { |
||
| 409 | $this->collection->prepend(new Column($attributes)); |
||
| 410 | |||
| 411 | return $this; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Add a Column object in collection. |
||
| 416 | * |
||
| 417 | * @param \Yajra\Datatables\Html\Column $column |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function add(Column $column) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Set datatables columns from array definition. |
||
| 429 | * |
||
| 430 | * @param array $columns |
||
| 431 | * @return $this |
||
| 432 | */ |
||
| 433 | public function columns(array $columns) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set title attribute of an array if not set. |
||
| 460 | * |
||
| 461 | * @param string $title |
||
| 462 | * @param array $attributes |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function setTitle($title, array $attributes) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Convert string into a readable title. |
||
| 476 | * |
||
| 477 | * @param string $title |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function getQualifiedTitle($title) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Add a checkbox column. |
||
| 487 | * |
||
| 488 | * @param array $attributes |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | public function addCheckbox(array $attributes = []) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Add a action column. |
||
| 511 | * |
||
| 512 | * @param array $attributes |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function addAction(array $attributes = []) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Add a index column. |
||
| 536 | * |
||
| 537 | * @param array $attributes |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function addIndex(array $attributes = []) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Setup ajax parameter for datatables pipeline plugin |
||
| 562 | * |
||
| 563 | * @param string $url |
||
| 564 | * @param string $pages |
||
| 565 | * @return $this |
||
| 566 | */ |
||
| 567 | public function pipeline($url, $pages) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Setup ajax parameter |
||
| 576 | * |
||
| 577 | * @param string|array $attributes |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | public function ajax($attributes) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Generate DataTable's table html. |
||
| 589 | * |
||
| 590 | * @param array $attributes |
||
| 591 | * @param bool $drawFooter |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function table(array $attributes = [], $drawFooter = false) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Compile table headers and to support responsive extension. |
||
| 614 | * |
||
| 615 | * @return array |
||
| 616 | */ |
||
| 617 | private function compileTableHeaders() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Compile table footer contents. |
||
| 633 | * |
||
| 634 | * @return array |
||
| 635 | */ |
||
| 636 | private function compileTableFooter() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Configure DataTable's parameters. |
||
| 648 | * |
||
| 649 | * @param array $attributes |
||
| 650 | * @return $this |
||
| 651 | */ |
||
| 652 | public function parameters(array $attributes = []) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Set custom javascript template. |
||
| 661 | * |
||
| 662 | * @param string $template |
||
| 663 | * @return $this |
||
| 664 | */ |
||
| 665 | public function setTemplate($template) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get collection of columns. |
||
| 674 | * |
||
| 675 | * @return \Illuminate\Support\Collection |
||
| 676 | */ |
||
| 677 | public function getColumns() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Remove column by name. |
||
| 684 | * |
||
| 685 | * @param array $names |
||
| 686 | * @return $this |
||
| 687 | */ |
||
| 688 | public function removeColumn(...$names) |
||
| 698 | } |
||
| 699 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.