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 | * Collection of Editors. |
||
60 | * |
||
61 | * @var null|Editor |
||
62 | */ |
||
63 | protected $editors = []; |
||
64 | |||
65 | /** |
||
66 | * @param Repository $config |
||
67 | * @param Factory $view |
||
68 | * @param HtmlBuilder $html |
||
69 | */ |
||
70 | public function __construct(Repository $config, Factory $view, HtmlBuilder $html) |
||
78 | |||
79 | /** |
||
80 | * Generate DataTable javascript. |
||
81 | * |
||
82 | * @param null $script |
||
83 | * @param array $attributes |
||
84 | * @return \Illuminate\Support\HtmlString |
||
85 | */ |
||
86 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
93 | |||
94 | /** |
||
95 | * Get generated raw scripts. |
||
96 | * |
||
97 | * @return \Illuminate\Support\HtmlString |
||
98 | */ |
||
99 | public function generateScripts() |
||
107 | |||
108 | /** |
||
109 | * Get generated json configuration. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function generateJson() |
||
129 | |||
130 | /** |
||
131 | * Generate DataTables js parameters. |
||
132 | * |
||
133 | * @param array $attributes |
||
134 | * @return string |
||
135 | */ |
||
136 | public function parameterize($attributes = []) |
||
161 | |||
162 | /** |
||
163 | * Check if given key & value is a valid callback js function. |
||
164 | * |
||
165 | * @param string $value |
||
166 | * @param string $key |
||
167 | * @return bool |
||
168 | */ |
||
169 | protected function isCallbackFunction($value, $key) |
||
179 | |||
180 | /** |
||
181 | * Get javascript template to use. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function template() |
||
191 | |||
192 | /** |
||
193 | * Retrieves HTML table attribute value. |
||
194 | * |
||
195 | * @param string $attribute |
||
196 | * @return mixed |
||
197 | * @throws \Exception |
||
198 | */ |
||
199 | public function getTableAttribute($attribute) |
||
207 | |||
208 | /** |
||
209 | * Get table computed table attributes. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getTableAttributes() |
||
217 | |||
218 | /** |
||
219 | * Sets multiple HTML table attributes at once. |
||
220 | * |
||
221 | * @param array $attributes |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function setTableAttributes(array $attributes) |
||
232 | |||
233 | /** |
||
234 | * Sets HTML table "id" attribute. |
||
235 | * |
||
236 | * @param string $id |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function setTableId($id) |
||
243 | |||
244 | /** |
||
245 | * Sets HTML table attribute(s). |
||
246 | * |
||
247 | * @param string|array $attribute |
||
248 | * @param mixed $value |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function setTableAttribute($attribute, $value = null) |
||
261 | |||
262 | /** |
||
263 | * Add class names to the "class" attribute of HTML table. |
||
264 | * |
||
265 | * @param string|array $class |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function addTableClass($class) |
||
278 | |||
279 | /** |
||
280 | * Remove class names from the "class" attribute of HTML table. |
||
281 | * |
||
282 | * @param string|array $class |
||
283 | * @return $this |
||
284 | */ |
||
285 | public function removeTableClass($class) |
||
298 | |||
299 | /** |
||
300 | * Add a column in collection usingsl attributes. |
||
301 | * |
||
302 | * @param array $attributes |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function addColumn(array $attributes) |
||
311 | |||
312 | /** |
||
313 | * Add a Column object at the beginning of collection. |
||
314 | * |
||
315 | * @param \Yajra\DataTables\Html\Column $column |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function addBefore(Column $column) |
||
324 | |||
325 | /** |
||
326 | * Add a column at the beginning of collection using attributes. |
||
327 | * |
||
328 | * @param array $attributes |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function addColumnBefore(array $attributes) |
||
337 | |||
338 | /** |
||
339 | * Add a Column object in collection. |
||
340 | * |
||
341 | * @param \Yajra\DataTables\Html\Column $column |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function add(Column $column) |
||
350 | |||
351 | /** |
||
352 | * Set datatables columns from array definition. |
||
353 | * |
||
354 | * @param array $columns |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function columns(array $columns) |
||
387 | |||
388 | /** |
||
389 | * Set title attribute of an array if not set. |
||
390 | * |
||
391 | * @param string $title |
||
392 | * @param array $attributes |
||
393 | * @return array |
||
394 | */ |
||
395 | public function setTitle($title, array $attributes) |
||
403 | |||
404 | /** |
||
405 | * Convert string into a readable title. |
||
406 | * |
||
407 | * @param string $title |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getQualifiedTitle($title) |
||
414 | |||
415 | /** |
||
416 | * Add a checkbox column. |
||
417 | * |
||
418 | * @param array $attributes |
||
419 | * @param bool|int $position true to prepend, false to append or a zero-based index for positioning |
||
420 | * @return $this |
||
421 | */ |
||
422 | public function addCheckbox(array $attributes = [], $position = false) |
||
447 | |||
448 | /** |
||
449 | * Add a action column. |
||
450 | * |
||
451 | * @param array $attributes |
||
452 | * @return $this |
||
453 | */ |
||
454 | public function addAction(array $attributes = []) |
||
472 | |||
473 | /** |
||
474 | * Add a index column. |
||
475 | * |
||
476 | * @param array $attributes |
||
477 | * @return $this |
||
478 | */ |
||
479 | public function addIndex(array $attributes = []) |
||
498 | |||
499 | /** |
||
500 | * Setup ajax parameter for datatables pipeline plugin. |
||
501 | * |
||
502 | * @param string $url |
||
503 | * @param string $pages |
||
504 | * @return $this |
||
505 | */ |
||
506 | public function pipeline($url, $pages) |
||
512 | |||
513 | /** |
||
514 | * Setup "ajax" parameter with POST method. |
||
515 | * |
||
516 | * @param string|array $attributes |
||
517 | * @return $this |
||
518 | */ |
||
519 | public function postAjax($attributes = '') |
||
531 | |||
532 | /** |
||
533 | * Setup ajax parameter. |
||
534 | * |
||
535 | * @param string|array $attributes |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function ajax($attributes = '') |
||
544 | |||
545 | /** |
||
546 | * Generate DataTable's table html. |
||
547 | * |
||
548 | * @param array $attributes |
||
549 | * @param bool $drawFooter |
||
550 | * @param bool $drawSearch |
||
551 | * @return \Illuminate\Support\HtmlString |
||
552 | */ |
||
553 | public function table(array $attributes = [], $drawFooter = false, $drawSearch = false) |
||
572 | |||
573 | /** |
||
574 | * Compile table headers and to support responsive extension. |
||
575 | * |
||
576 | * @return array |
||
577 | */ |
||
578 | private function compileTableHeaders() |
||
591 | |||
592 | /** |
||
593 | * Compile table search headers. |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | private function compileTableSearchHeaders() |
||
606 | |||
607 | /** |
||
608 | * Compile table footer contents. |
||
609 | * |
||
610 | * @return array |
||
611 | */ |
||
612 | private function compileTableFooter() |
||
628 | |||
629 | /** |
||
630 | * Configure DataTable's parameters. |
||
631 | * |
||
632 | * @param array $attributes |
||
633 | * @return $this |
||
634 | */ |
||
635 | public function parameters(array $attributes = []) |
||
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 | * Attach multiple editors to builder. |
||
732 | * |
||
733 | * @param mixed ...$editors |
||
734 | * @return $this |
||
735 | */ |
||
736 | public function editors(...$editors) |
||
744 | |||
745 | /** |
||
746 | * Integrate with DataTables Editor. |
||
747 | * |
||
748 | * @param array|Editor $fields |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function editor($fields) |
||
761 | |||
762 | /** |
||
763 | * Set custom javascript template. |
||
764 | * |
||
765 | * @param string $template |
||
766 | * @return $this |
||
767 | */ |
||
768 | public function setTemplate($template) |
||
774 | |||
775 | /** |
||
776 | * @param array|Editor $fields |
||
777 | * @throws \Exception |
||
778 | */ |
||
779 | protected function newEditor($fields) |
||
798 | |||
799 | /** |
||
800 | * Get ajax url. |
||
801 | * |
||
802 | * @return array|mixed|string |
||
803 | */ |
||
804 | public function getAjaxUrl() |
||
812 | |||
813 | /** |
||
814 | * Compile DataTable callback value. |
||
815 | * |
||
816 | * @param mixed $callback |
||
817 | * @return mixed|string |
||
818 | */ |
||
819 | private function compileCallback($callback) |
||
829 | } |
||
830 |
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..