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 |
||
19 | class Builder |
||
20 | { |
||
21 | /** |
||
22 | * @var Collection |
||
23 | */ |
||
24 | public $collection; |
||
25 | |||
26 | /** |
||
27 | * @var Repository |
||
28 | */ |
||
29 | public $config; |
||
30 | |||
31 | /** |
||
32 | * @var Factory |
||
33 | */ |
||
34 | public $view; |
||
35 | |||
36 | /** |
||
37 | * @var HtmlBuilder |
||
38 | */ |
||
39 | public $html; |
||
40 | |||
41 | /** |
||
42 | * @var UrlGenerator |
||
43 | */ |
||
44 | public $url; |
||
45 | |||
46 | /** |
||
47 | * @var FormBuilder |
||
48 | */ |
||
49 | public $form; |
||
50 | |||
51 | /** |
||
52 | * @var string|array |
||
53 | */ |
||
54 | protected $ajax = ''; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $tableAttributes = ['class' => 'table', 'id' => 'dataTableBuilder']; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $template = ''; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $attributes = []; |
||
70 | |||
71 | /** |
||
72 | * Lists of valid DataTables Callbacks. |
||
73 | * |
||
74 | * @link https://datatables.net/reference/option/. |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $validCallbacks = [ |
||
78 | 'createdRow', |
||
79 | 'drawCallback', |
||
80 | 'footerCallback', |
||
81 | 'formatNumber', |
||
82 | 'headerCallback', |
||
83 | 'infoCallback', |
||
84 | 'initComplete', |
||
85 | 'preDrawCallback', |
||
86 | 'rowCallback', |
||
87 | 'stateLoadCallback', |
||
88 | 'stateLoaded', |
||
89 | 'stateLoadParams', |
||
90 | 'stateSaveCallback', |
||
91 | 'stateSaveParams', |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * @param Repository $config |
||
96 | * @param Factory $view |
||
97 | * @param HtmlBuilder $html |
||
98 | * @param UrlGenerator $url |
||
99 | * @param FormBuilder $form |
||
100 | */ |
||
101 | public function __construct( |
||
102 | Repository $config, |
||
103 | Factory $view, |
||
104 | HtmlBuilder $html, |
||
105 | UrlGenerator $url, |
||
106 | FormBuilder $form |
||
107 | ) { |
||
108 | $this->config = $config; |
||
109 | $this->view = $view; |
||
110 | $this->html = $html; |
||
111 | $this->url = $url; |
||
112 | $this->collection = new Collection; |
||
113 | $this->form = $form; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Generate DataTable javascript. |
||
118 | * |
||
119 | * @param null $script |
||
120 | * @param array $attributes |
||
121 | * @return string |
||
122 | */ |
||
123 | public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
||
124 | { |
||
125 | $script = $script ?: $this->generateScripts(); |
||
126 | |||
127 | return '<script' . $this->html->attributes($attributes) . '>' . $script . '</script>' . PHP_EOL; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Get generated raw scripts. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function generateScripts() |
||
151 | |||
152 | /** |
||
153 | * Generate DataTables js parameters. |
||
154 | * |
||
155 | * @param array $attributes |
||
156 | * @return string |
||
157 | */ |
||
158 | public function parameterize($attributes = []) |
||
174 | |||
175 | /** |
||
176 | * Encode ajax data function param. |
||
177 | * |
||
178 | * @param array $parameters |
||
179 | * @return mixed |
||
180 | */ |
||
181 | protected function encodeAjaxDataFunction($parameters) |
||
191 | |||
192 | /** |
||
193 | * Encode columns render function. |
||
194 | * |
||
195 | * @param array $parameters |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function encodeColumnFunctions(array $parameters) |
||
214 | |||
215 | /** |
||
216 | * Encode DataTables callbacks function. |
||
217 | * |
||
218 | * @param array $parameters |
||
219 | * @return array |
||
220 | */ |
||
221 | protected function encodeCallbackFunctions(array $parameters) |
||
233 | |||
234 | /** |
||
235 | * Compile DataTable callback value. |
||
236 | * |
||
237 | * @param mixed $callback |
||
238 | * @return mixed|string |
||
239 | */ |
||
240 | private function compileCallback($callback) |
||
250 | |||
251 | /** |
||
252 | * Decode ajax data method. |
||
253 | * |
||
254 | * @param string $function |
||
255 | * @param string $json |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function decodeAjaxDataFunction($function, $json) |
||
262 | |||
263 | /** |
||
264 | * Decode columns render functions. |
||
265 | * |
||
266 | * @param array $columnFunctions |
||
267 | * @param string $json |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
278 | |||
279 | /** |
||
280 | * Decode DataTables callbacks function. |
||
281 | * |
||
282 | * @param array $callbackFunctions |
||
283 | * @param string $json |
||
284 | * @return string |
||
285 | */ |
||
286 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
294 | |||
295 | /** |
||
296 | * Get javascript template to use. |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function template() |
||
306 | |||
307 | /** |
||
308 | * Add a column in collection using attributes. |
||
309 | * |
||
310 | * @param array $attributes |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function addColumn(array $attributes) |
||
319 | |||
320 | /** |
||
321 | * Add a Column object in collection. |
||
322 | * |
||
323 | * @param \Yajra\Datatables\Html\Column $column |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function add(Column $column) |
||
332 | |||
333 | /** |
||
334 | * Set datatables columns from array definition. |
||
335 | * |
||
336 | * @param array $columns |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function columns(array $columns) |
||
361 | |||
362 | /** |
||
363 | * Set title attribute of an array if not set. |
||
364 | * |
||
365 | * @param string $title |
||
366 | * @param array $attributes |
||
367 | * @return array |
||
368 | */ |
||
369 | public function setTitle($title, array $attributes) |
||
377 | |||
378 | /** |
||
379 | * Convert string into a readable title. |
||
380 | * |
||
381 | * @param string $title |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getQualifiedTitle($title) |
||
388 | |||
389 | /** |
||
390 | * Add a checkbox column. |
||
391 | * |
||
392 | * @param array $attributes |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function addCheckbox(array $attributes = []) |
||
412 | |||
413 | /** |
||
414 | * Add a action column. |
||
415 | * |
||
416 | * @param array $attributes |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function addAction(array $attributes = []) |
||
437 | |||
438 | /** |
||
439 | * Setup ajax parameter |
||
440 | * |
||
441 | * @param string|array $attributes |
||
442 | * @return $this |
||
443 | */ |
||
444 | public function ajax($attributes) |
||
450 | |||
451 | /** |
||
452 | * Generate DataTable's table html. |
||
453 | * |
||
454 | * @param array $attributes |
||
455 | * @param bool $drawFooter |
||
456 | * @return string |
||
457 | */ |
||
458 | public function table(array $attributes = [], $drawFooter = false) |
||
475 | |||
476 | /** |
||
477 | * Compile table headers and to support responsive extension. |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | private function compileTableHeaders() |
||
493 | |||
494 | /** |
||
495 | * Compile table footer contents. |
||
496 | * |
||
497 | * @return array |
||
498 | */ |
||
499 | private function compileTableFooter() |
||
508 | |||
509 | /** |
||
510 | * Configure DataTable's parameters. |
||
511 | * |
||
512 | * @param array $attributes |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function parameters(array $attributes = []) |
||
521 | |||
522 | /** |
||
523 | * Set custom javascript template. |
||
524 | * |
||
525 | * @param string $template |
||
526 | * @return $this |
||
527 | */ |
||
528 | public function setTemplate($template) |
||
534 | |||
535 | /** |
||
536 | * Get collection of columns. |
||
537 | * |
||
538 | * @return Collection |
||
539 | */ |
||
540 | public function getColumns() |
||
544 | } |
||
545 |