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 | * Determine if the datatable has a custom id. |
||
133 | * |
||
134 | * @return boolean |
||
135 | */ |
||
136 | private function hasCustomTableId() |
||
140 | |||
141 | /** |
||
142 | * Get generated raw scripts. |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | public function generateScripts() |
||
162 | |||
163 | /** |
||
164 | * Translates literal javascript functions to key value pairs. (Recursively) |
||
165 | * |
||
166 | * @param array &$parameters |
||
167 | * @param array $values |
||
168 | * @param array $replacements |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | public function parseJavascriptFunctions(&$parameters, $values = [], $replacements = []) |
||
191 | |||
192 | /** |
||
193 | * Generate DataTables js parameters. |
||
194 | * |
||
195 | * @param array $attributes |
||
196 | * @return string |
||
197 | */ |
||
198 | public function parameterize($attributes = []) |
||
218 | |||
219 | /** |
||
220 | * Encode ajax data function param. |
||
221 | * |
||
222 | * @param array $parameters |
||
223 | * @return mixed |
||
224 | */ |
||
225 | protected function encodeAjaxDataFunction($parameters) |
||
235 | |||
236 | /** |
||
237 | * Encode columns render function. |
||
238 | * |
||
239 | * @param array $parameters |
||
240 | * @return array |
||
241 | */ |
||
242 | protected function encodeColumnFunctions(array $parameters) |
||
258 | |||
259 | /** |
||
260 | * Encode DataTables callbacks function. |
||
261 | * |
||
262 | * @param array $parameters |
||
263 | * @return array |
||
264 | */ |
||
265 | protected function encodeCallbackFunctions(array $parameters) |
||
277 | |||
278 | /** |
||
279 | * Compile DataTable callback value. |
||
280 | * |
||
281 | * @param mixed $callback |
||
282 | * @return mixed|string |
||
283 | */ |
||
284 | private function compileCallback($callback) |
||
294 | |||
295 | /** |
||
296 | * Decode ajax data method. |
||
297 | * |
||
298 | * @param string $function |
||
299 | * @param string $json |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function decodeAjaxDataFunction($function, $json) |
||
306 | |||
307 | /** |
||
308 | * Decode columns render functions. |
||
309 | * |
||
310 | * @param array $columnFunctions |
||
311 | * @param string $json |
||
312 | * @return string |
||
313 | */ |
||
314 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
322 | |||
323 | /** |
||
324 | * Decode DataTables callbacks function. |
||
325 | * |
||
326 | * @param array $callbackFunctions |
||
327 | * @param string $json |
||
328 | * @return string |
||
329 | */ |
||
330 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
338 | |||
339 | /** |
||
340 | * Get javascript template to use. |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | protected function template() |
||
350 | |||
351 | /** |
||
352 | * Sets HTML table attribute(s). |
||
353 | * |
||
354 | * @param string|array $attribute |
||
355 | * @param mixed $value |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function setTableAttribute($attribute, $value = null) |
||
368 | |||
369 | /** |
||
370 | * Sets multiple HTML table attributes at once. |
||
371 | * |
||
372 | * @param array $attributes |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setTableAttributes(array $attributes) |
||
383 | |||
384 | /** |
||
385 | * Retrieves HTML table attribute value. |
||
386 | * |
||
387 | * @param string $attribute |
||
388 | * @return mixed |
||
389 | * @throws \Exception |
||
390 | */ |
||
391 | public function getTableAttribute($attribute) |
||
399 | |||
400 | /** |
||
401 | * Add a column in collection using attributes. |
||
402 | * |
||
403 | * @param array $attributes |
||
404 | * @return $this |
||
405 | */ |
||
406 | public function addColumn(array $attributes) |
||
412 | |||
413 | /** |
||
414 | * Add a Column object in collection. |
||
415 | * |
||
416 | * @param \Yajra\Datatables\Html\Column $column |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function add(Column $column) |
||
425 | |||
426 | /** |
||
427 | * Set datatables columns from array definition. |
||
428 | * |
||
429 | * @param array $columns |
||
430 | * @return $this |
||
431 | */ |
||
432 | public function columns(array $columns) |
||
454 | |||
455 | /** |
||
456 | * Set title attribute of an array if not set. |
||
457 | * |
||
458 | * @param string $title |
||
459 | * @param array $attributes |
||
460 | * @return array |
||
461 | */ |
||
462 | public function setTitle($title, array $attributes) |
||
470 | |||
471 | /** |
||
472 | * Convert string into a readable title. |
||
473 | * |
||
474 | * @param string $title |
||
475 | * @return string |
||
476 | */ |
||
477 | public function getQualifiedTitle($title) |
||
481 | |||
482 | /** |
||
483 | * Add a checkbox column. |
||
484 | * |
||
485 | * @param array $attributes |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function addCheckbox(array $attributes = []) |
||
505 | |||
506 | /** |
||
507 | * Add a action column. |
||
508 | * |
||
509 | * @param array $attributes |
||
510 | * @return $this |
||
511 | */ |
||
512 | public function addAction(array $attributes = []) |
||
530 | |||
531 | /** |
||
532 | * Add a index column. |
||
533 | * |
||
534 | * @param array $attributes |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function addIndex(array $attributes = []) |
||
556 | |||
557 | /** |
||
558 | * Setup ajax parameter for datatables pipeline plugin |
||
559 | * |
||
560 | * @param string $url |
||
561 | * @param string $pages |
||
562 | * @return $this |
||
563 | */ |
||
564 | public function pipeline($url, $pages) |
||
572 | |||
573 | public function getQueryString() |
||
579 | |||
580 | /** |
||
581 | * Setup ajax parameter |
||
582 | * |
||
583 | * @param string|array $attributes |
||
584 | * @return $this |
||
585 | */ |
||
586 | public function ajax($attributes) |
||
600 | |||
601 | /** |
||
602 | * Generate DataTable's table html. |
||
603 | * |
||
604 | * @param array $attributes |
||
605 | * @param bool $drawFooter |
||
606 | * @return string |
||
607 | */ |
||
608 | public function table(array $attributes = [], $drawFooter = false) |
||
625 | |||
626 | /** |
||
627 | * Compile table headers and to support responsive extension. |
||
628 | * |
||
629 | * @return array |
||
630 | */ |
||
631 | private function compileTableHeaders() |
||
644 | |||
645 | /** |
||
646 | * Compile table footer contents. |
||
647 | * |
||
648 | * @return array |
||
649 | */ |
||
650 | private function compileTableFooter() |
||
659 | |||
660 | /** |
||
661 | * Configure DataTable's parameters. |
||
662 | * |
||
663 | * @param array $attributes |
||
664 | * @return $this |
||
665 | */ |
||
666 | public function parameters(array $attributes = []) |
||
672 | |||
673 | /** |
||
674 | * Set custom javascript template. |
||
675 | * |
||
676 | * @param string $template |
||
677 | * @return $this |
||
678 | */ |
||
679 | public function setTemplate($template) |
||
685 | |||
686 | /** |
||
687 | * Get collection of columns. |
||
688 | * |
||
689 | * @return \Illuminate\Support\Collection |
||
690 | */ |
||
691 | public function getColumns() |
||
695 | } |
||
696 |