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 |
||
13 | class Builder |
||
14 | { |
||
15 | use Macroable; |
||
16 | |||
17 | /** |
||
18 | * @var Collection |
||
19 | */ |
||
20 | public $collection; |
||
21 | |||
22 | /** |
||
23 | * @var Repository |
||
24 | */ |
||
25 | public $config; |
||
26 | |||
27 | /** |
||
28 | * @var Factory |
||
29 | */ |
||
30 | public $view; |
||
31 | |||
32 | /** |
||
33 | * @var HtmlBuilder |
||
34 | */ |
||
35 | public $html; |
||
36 | |||
37 | /** |
||
38 | * @var string|array |
||
39 | */ |
||
40 | protected $ajax = ''; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $tableAttributes = []; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $template = ''; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $attributes = []; |
||
56 | |||
57 | /** |
||
58 | * Lists of valid DataTables Callbacks. |
||
59 | * |
||
60 | * @link https://datatables.net/reference/option/. |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $validCallbacks = [ |
||
64 | 'createdRow', |
||
65 | 'drawCallback', |
||
66 | 'footerCallback', |
||
67 | 'formatNumber', |
||
68 | 'headerCallback', |
||
69 | 'infoCallback', |
||
70 | 'initComplete', |
||
71 | 'preDrawCallback', |
||
72 | 'rowCallback', |
||
73 | 'stateLoadCallback', |
||
74 | 'stateLoaded', |
||
75 | 'stateLoadParams', |
||
76 | 'stateSaveCallback', |
||
77 | 'stateSaveParams', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * @param Repository $config |
||
82 | * @param Factory $view |
||
83 | * @param HtmlBuilder $html |
||
84 | */ |
||
85 | public function __construct(Repository $config, Factory $view, HtmlBuilder $html) |
||
92 | |||
93 | /** |
||
94 | * Generate DataTable javascript. |
||
95 | * |
||
96 | * @param null $script |
||
97 | * @param array $attributes |
||
98 | * @return \Illuminate\Support\HtmlString |
||
99 | */ |
||
100 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
107 | |||
108 | /** |
||
109 | * Get generated raw scripts. |
||
110 | * |
||
111 | * @return \Illuminate\Support\HtmlString |
||
112 | */ |
||
113 | public function generateScripts() |
||
132 | |||
133 | /** |
||
134 | * Generate DataTables js parameters. |
||
135 | * |
||
136 | * @param array $attributes |
||
137 | * @return string |
||
138 | */ |
||
139 | public function parameterize($attributes = []) |
||
172 | |||
173 | /** |
||
174 | * Encode ajax data function param. |
||
175 | * |
||
176 | * @param array $parameters |
||
177 | * @return mixed |
||
178 | */ |
||
179 | protected function encodeAjaxDataFunction($parameters) |
||
189 | |||
190 | /** |
||
191 | * Encode columns render function. |
||
192 | * |
||
193 | * @param array $parameters |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function encodeColumnFunctions(array $parameters) |
||
212 | |||
213 | /** |
||
214 | * Encode DataTables callbacks function. |
||
215 | * |
||
216 | * @param array $parameters |
||
217 | * @return array |
||
218 | */ |
||
219 | protected function encodeCallbackFunctions(array $parameters) |
||
231 | |||
232 | /** |
||
233 | * Compile DataTable callback value. |
||
234 | * |
||
235 | * @param mixed $callback |
||
236 | * @return mixed|string |
||
237 | */ |
||
238 | private function compileCallback($callback) |
||
248 | |||
249 | /** |
||
250 | * Decode ajax data method. |
||
251 | * |
||
252 | * @param string $function |
||
253 | * @param string $json |
||
254 | * @return string |
||
255 | */ |
||
256 | protected function decodeAjaxDataFunction($function, $json) |
||
260 | |||
261 | /** |
||
262 | * Decode columns render functions. |
||
263 | * |
||
264 | * @param array $columnFunctions |
||
265 | * @param string $json |
||
266 | * @return string |
||
267 | */ |
||
268 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
276 | |||
277 | /** |
||
278 | * Decode DataTables callbacks function. |
||
279 | * |
||
280 | * @param array $callbackFunctions |
||
281 | * @param string $json |
||
282 | * @return string |
||
283 | */ |
||
284 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
292 | |||
293 | /** |
||
294 | * Get javascript template to use. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | protected function template() |
||
304 | |||
305 | /** |
||
306 | * Sets HTML table attribute(s). |
||
307 | * |
||
308 | * @param string|array $attribute |
||
309 | * @param mixed $value |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setTableAttribute($attribute, $value = null) |
||
322 | |||
323 | /** |
||
324 | * Retrieves HTML table attribute value. |
||
325 | * |
||
326 | * @param string $attribute |
||
327 | * @return mixed |
||
328 | * @throws \Exception |
||
329 | */ |
||
330 | public function getTableAttribute($attribute) |
||
338 | |||
339 | /** |
||
340 | * Add a column in collection usingsl attributes. |
||
341 | * |
||
342 | * @param array $attributes |
||
343 | * @return $this |
||
344 | */ |
||
345 | public function addColumn(array $attributes) |
||
351 | |||
352 | /** |
||
353 | * Add a Column object at the beginning of collection |
||
354 | * |
||
355 | * @param \Yajra\Datatables\Html\Column $column |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function addBefore(Column $column) |
||
364 | |||
365 | /** |
||
366 | * Add a column at the beginning of collection using attributes. |
||
367 | * |
||
368 | * @param array $attributes |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function addColumnBefore(array $attributes) |
||
377 | |||
378 | /** |
||
379 | * Add a Column object in collection. |
||
380 | * |
||
381 | * @param \Yajra\Datatables\Html\Column $column |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function add(Column $column) |
||
390 | |||
391 | /** |
||
392 | * Set datatables columns from array definition. |
||
393 | * |
||
394 | * @param array $columns |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function columns(array $columns) |
||
421 | |||
422 | /** |
||
423 | * Set title attribute of an array if not set. |
||
424 | * |
||
425 | * @param string $title |
||
426 | * @param array $attributes |
||
427 | * @return array |
||
428 | */ |
||
429 | public function setTitle($title, array $attributes) |
||
437 | |||
438 | /** |
||
439 | * Convert string into a readable title. |
||
440 | * |
||
441 | * @param string $title |
||
442 | * @return string |
||
443 | */ |
||
444 | public function getQualifiedTitle($title) |
||
448 | |||
449 | /** |
||
450 | * Add a checkbox column. |
||
451 | * |
||
452 | * @param array $attributes |
||
453 | * @return $this |
||
454 | */ |
||
455 | public function addCheckbox(array $attributes = []) |
||
472 | |||
473 | /** |
||
474 | * Add a action column. |
||
475 | * |
||
476 | * @param array $attributes |
||
477 | * @return $this |
||
478 | */ |
||
479 | public function addAction(array $attributes = []) |
||
497 | |||
498 | /** |
||
499 | * Add a index column. |
||
500 | * |
||
501 | * @param array $attributes |
||
502 | * @return $this |
||
503 | */ |
||
504 | public function addIndex(array $attributes = []) |
||
523 | |||
524 | /** |
||
525 | * Setup ajax parameter for datatables pipeline plugin |
||
526 | * |
||
527 | * @param string $url |
||
528 | * @param string $pages |
||
529 | * @return $this |
||
530 | */ |
||
531 | public function pipeline($url, $pages) |
||
537 | |||
538 | /** |
||
539 | * Setup ajax parameter |
||
540 | * |
||
541 | * @param string|array $attributes |
||
542 | * @return $this |
||
543 | */ |
||
544 | public function ajax($attributes = '') |
||
550 | |||
551 | /** |
||
552 | * Generate DataTable's table html. |
||
553 | * |
||
554 | * @param array $attributes |
||
555 | * @param bool $drawFooter |
||
556 | * @return \Illuminate\Support\HtmlString |
||
557 | */ |
||
558 | public function table(array $attributes = [], $drawFooter = false) |
||
575 | |||
576 | /** |
||
577 | * Get table computed table attributes. |
||
578 | * |
||
579 | * @return array |
||
580 | */ |
||
581 | public function getTableAttributes() |
||
587 | |||
588 | /** |
||
589 | * Sets multiple HTML table attributes at once. |
||
590 | * |
||
591 | * @param array $attributes |
||
592 | * @return $this |
||
593 | */ |
||
594 | public function setTableAttributes(array $attributes) |
||
602 | |||
603 | /** |
||
604 | * Compile table headers and to support responsive extension. |
||
605 | * |
||
606 | * @return array |
||
607 | */ |
||
608 | private function compileTableHeaders() |
||
621 | |||
622 | /** |
||
623 | * Compile table footer contents. |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | private function compileTableFooter() |
||
636 | |||
637 | /** |
||
638 | * Configure DataTable's parameters. |
||
639 | * |
||
640 | * @param array $attributes |
||
641 | * @return $this |
||
642 | */ |
||
643 | public function parameters(array $attributes = []) |
||
649 | |||
650 | /** |
||
651 | * Set custom javascript template. |
||
652 | * |
||
653 | * @param string $template |
||
654 | * @return $this |
||
655 | */ |
||
656 | public function setTemplate($template) |
||
662 | |||
663 | /** |
||
664 | * Get collection of columns. |
||
665 | * |
||
666 | * @return \Illuminate\Support\Collection |
||
667 | */ |
||
668 | public function getColumns() |
||
672 | |||
673 | /** |
||
674 | * Remove column by name. |
||
675 | * |
||
676 | * @param array $names |
||
677 | * @return $this |
||
678 | */ |
||
679 | public function removeColumn(...$names) |
||
689 | |||
690 | /** |
||
691 | * Minify ajax url generated when using get request |
||
692 | * by deleting unnecessary url params. |
||
693 | * |
||
694 | * @param string $url |
||
695 | * @param string $script |
||
696 | * @param array $data |
||
697 | * @return $this |
||
698 | */ |
||
699 | public function minifiedAjax($url = '', $script = null, $data = []) |
||
726 | |||
727 | /** |
||
728 | * Make a data script to be appended on ajax request of dataTables. |
||
729 | * |
||
730 | * @param array $data |
||
731 | * @return string |
||
732 | */ |
||
733 | protected function makeDataScript(array $data) |
||
742 | } |
||
743 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: