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 | * Prepare ajax attributes. |
||
143 | */ |
||
144 | private function prepareAjaxAttributes() |
||
159 | |||
160 | /** |
||
161 | * Get generated raw scripts. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function generateScripts() |
||
183 | |||
184 | /** |
||
185 | * Generate DataTables js parameters. |
||
186 | * |
||
187 | * @param array $attributes |
||
188 | * @return string |
||
189 | */ |
||
190 | public function parameterize($attributes = []) |
||
224 | |||
225 | /** |
||
226 | * Encode ajax data function param. |
||
227 | * |
||
228 | * @param array $parameters |
||
229 | * @return mixed |
||
230 | */ |
||
231 | protected function encodeAjaxDataFunction($parameters) |
||
241 | |||
242 | /** |
||
243 | * Encode columns render function. |
||
244 | * |
||
245 | * @param array $parameters |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function encodeColumnFunctions(array $parameters) |
||
264 | |||
265 | /** |
||
266 | * Encode DataTables callbacks function. |
||
267 | * |
||
268 | * @param array $parameters |
||
269 | * @return array |
||
270 | */ |
||
271 | protected function encodeCallbackFunctions(array $parameters) |
||
283 | |||
284 | /** |
||
285 | * Compile DataTable callback value. |
||
286 | * |
||
287 | * @param mixed $callback |
||
288 | * @return mixed|string |
||
289 | */ |
||
290 | private function compileCallback($callback) |
||
300 | |||
301 | /** |
||
302 | * Decode ajax data method. |
||
303 | * |
||
304 | * @param string $function |
||
305 | * @param string $json |
||
306 | * @return string |
||
307 | */ |
||
308 | protected function decodeAjaxDataFunction($function, $json) |
||
312 | |||
313 | /** |
||
314 | * Decode columns render functions. |
||
315 | * |
||
316 | * @param array $columnFunctions |
||
317 | * @param string $json |
||
318 | * @return string |
||
319 | */ |
||
320 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
328 | |||
329 | /** |
||
330 | * Decode DataTables callbacks function. |
||
331 | * |
||
332 | * @param array $callbackFunctions |
||
333 | * @param string $json |
||
334 | * @return string |
||
335 | */ |
||
336 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
344 | |||
345 | /** |
||
346 | * Get javascript template to use. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function template() |
||
356 | |||
357 | /** |
||
358 | * Sets HTML table attribute(s). |
||
359 | * |
||
360 | * @param string|array $attribute |
||
361 | * @param mixed $value |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function setTableAttribute($attribute, $value = null) |
||
374 | |||
375 | /** |
||
376 | * Sets multiple HTML table attributes at once. |
||
377 | * |
||
378 | * @param array $attributes |
||
379 | * @return $this |
||
380 | */ |
||
381 | public function setTableAttributes(array $attributes) |
||
389 | |||
390 | /** |
||
391 | * Retrieves HTML table attribute value. |
||
392 | * |
||
393 | * @param string $attribute |
||
394 | * @return mixed |
||
395 | * @throws \Exception |
||
396 | */ |
||
397 | public function getTableAttribute($attribute) |
||
405 | |||
406 | /** |
||
407 | * Add a column in collection using attributes. |
||
408 | * |
||
409 | * @param array $attributes |
||
410 | * @return $this |
||
411 | */ |
||
412 | public function addColumn(array $attributes) |
||
418 | |||
419 | /** |
||
420 | * Add a Column object in collection. |
||
421 | * |
||
422 | * @param \Yajra\Datatables\Html\Column $column |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function add(Column $column) |
||
431 | |||
432 | /** |
||
433 | * Set datatables columns from array definition. |
||
434 | * |
||
435 | * @param array $columns |
||
436 | * @return $this |
||
437 | */ |
||
438 | public function columns(array $columns) |
||
460 | |||
461 | /** |
||
462 | * Set title attribute of an array if not set. |
||
463 | * |
||
464 | * @param string $title |
||
465 | * @param array $attributes |
||
466 | * @return array |
||
467 | */ |
||
468 | public function setTitle($title, array $attributes) |
||
476 | |||
477 | /** |
||
478 | * Convert string into a readable title. |
||
479 | * |
||
480 | * @param string $title |
||
481 | * @return string |
||
482 | */ |
||
483 | public function getQualifiedTitle($title) |
||
487 | |||
488 | /** |
||
489 | * Add a checkbox column. |
||
490 | * |
||
491 | * @param array $attributes |
||
492 | * @return $this |
||
493 | */ |
||
494 | public function addCheckbox(array $attributes = []) |
||
511 | |||
512 | /** |
||
513 | * Add a action column. |
||
514 | * |
||
515 | * @param array $attributes |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function addAction(array $attributes = []) |
||
536 | |||
537 | /** |
||
538 | * Add a index column. |
||
539 | * |
||
540 | * @param array $attributes |
||
541 | * @return $this |
||
542 | */ |
||
543 | public function addIndex(array $attributes = []) |
||
562 | |||
563 | /** |
||
564 | * Setup ajax parameter for datatables pipeline plugin |
||
565 | * |
||
566 | * @param string $url |
||
567 | * @param string $pages |
||
568 | * @return $this |
||
569 | */ |
||
570 | public function pipeline($url, $pages) |
||
576 | |||
577 | /** |
||
578 | * Setup ajax parameter |
||
579 | * |
||
580 | * @param string|array $attributes |
||
581 | * @return $this |
||
582 | */ |
||
583 | public function ajax($attributes) |
||
589 | |||
590 | /** |
||
591 | * Generate DataTable's table html. |
||
592 | * |
||
593 | * @param array $attributes |
||
594 | * @param bool $drawFooter |
||
595 | * @return string |
||
596 | */ |
||
597 | public function table(array $attributes = [], $drawFooter = false) |
||
614 | |||
615 | /** |
||
616 | * Compile table headers and to support responsive extension. |
||
617 | * |
||
618 | * @return array |
||
619 | */ |
||
620 | private function compileTableHeaders() |
||
633 | |||
634 | /** |
||
635 | * Compile table footer contents. |
||
636 | * |
||
637 | * @return array |
||
638 | */ |
||
639 | private function compileTableFooter() |
||
648 | |||
649 | /** |
||
650 | * Configure DataTable's parameters. |
||
651 | * |
||
652 | * @param array $attributes |
||
653 | * @return $this |
||
654 | */ |
||
655 | public function parameters(array $attributes = []) |
||
661 | |||
662 | /** |
||
663 | * Set custom javascript template. |
||
664 | * |
||
665 | * @param string $template |
||
666 | * @return $this |
||
667 | */ |
||
668 | public function setTemplate($template) |
||
674 | |||
675 | /** |
||
676 | * Get collection of columns. |
||
677 | * |
||
678 | * @return \Illuminate\Support\Collection |
||
679 | */ |
||
680 | public function getColumns() |
||
684 | } |
||
685 |