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 | * Get generated raw scripts. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function generateScripts() |
||
137 | { |
||
138 | $args = array_merge( |
||
139 | $this->attributes, [ |
||
140 | 'ajax' => $this->ajax, |
||
141 | 'columns' => $this->collection->toArray(), |
||
142 | ] |
||
143 | ); |
||
144 | |||
145 | $parameters = $this->parameterize($args); |
||
146 | |||
147 | return sprintf( |
||
148 | $this->template(), |
||
149 | $this->tableAttributes['id'], $parameters |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Generate DataTables js parameters. |
||
155 | * |
||
156 | * @param array $attributes |
||
157 | * @return string |
||
158 | */ |
||
159 | public function parameterize($attributes = []) |
||
160 | { |
||
161 | $parameters = (new Parameters($attributes))->toArray(); |
||
162 | |||
163 | list($ajaxDataFunction, $parameters) = $this->encodeAjaxDataFunction($parameters); |
||
164 | list($columnFunctions, $parameters) = $this->encodeColumnFunctions($parameters); |
||
165 | list($callbackFunctions, $parameters) = $this->encodeCallbackFunctions($parameters); |
||
166 | |||
167 | $json = json_encode($parameters); |
||
168 | |||
169 | $json = $this->decodeAjaxDataFunction($ajaxDataFunction, $json); |
||
170 | $json = $this->decodeColumnFunctions($columnFunctions, $json); |
||
171 | $json = $this->decodeCallbackFunctions($callbackFunctions, $json); |
||
172 | |||
173 | return $json; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Encode ajax data function param. |
||
178 | * |
||
179 | * @param array $parameters |
||
180 | * @return mixed |
||
181 | */ |
||
182 | protected function encodeAjaxDataFunction($parameters) |
||
183 | { |
||
184 | $ajaxData = ''; |
||
185 | if (isset($parameters['ajax']['data'])) { |
||
186 | $ajaxData = $parameters['ajax']['data']; |
||
187 | $parameters['ajax']['data'] = "#ajax_data#"; |
||
188 | } |
||
189 | |||
190 | return [$ajaxData, $parameters]; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Encode columns render function. |
||
195 | * |
||
196 | * @param array $parameters |
||
197 | * @return array |
||
198 | */ |
||
199 | protected function encodeColumnFunctions(array $parameters) |
||
215 | |||
216 | /** |
||
217 | * Encode DataTables callbacks function. |
||
218 | * |
||
219 | * @param array $parameters |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function encodeCallbackFunctions(array $parameters) |
||
234 | |||
235 | /** |
||
236 | * Compile DataTable callback value. |
||
237 | * |
||
238 | * @param mixed $callback |
||
239 | * @return mixed|string |
||
240 | */ |
||
241 | private function compileCallback($callback) |
||
251 | |||
252 | /** |
||
253 | * Decode ajax data method. |
||
254 | * |
||
255 | * @param string $function |
||
256 | * @param string $json |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function decodeAjaxDataFunction($function, $json) |
||
263 | |||
264 | /** |
||
265 | * Decode columns render functions. |
||
266 | * |
||
267 | * @param array $columnFunctions |
||
268 | * @param string $json |
||
269 | * @return string |
||
270 | */ |
||
271 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
279 | |||
280 | /** |
||
281 | * Decode DataTables callbacks function. |
||
282 | * |
||
283 | * @param array $callbackFunctions |
||
284 | * @param string $json |
||
285 | * @return string |
||
286 | */ |
||
287 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
295 | |||
296 | /** |
||
297 | * Get javascript template to use. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function template() |
||
307 | |||
308 | /** |
||
309 | * Sets HTML table attribute(s). |
||
310 | * |
||
311 | * @param string|array $attribute |
||
312 | * @param mixed $value |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function setTableAttribute($attribute, $value = null) |
||
325 | |||
326 | /** |
||
327 | * Sets multiple HTML table attributes at once. |
||
328 | * |
||
329 | * @param array $attributes |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setTableAttributes(array $attributes) |
||
340 | |||
341 | /** |
||
342 | * Retrieves HTML table attribute value. |
||
343 | * |
||
344 | * @param string $attribute |
||
345 | * @return mixed |
||
346 | * @throws \Exception |
||
347 | */ |
||
348 | public function getTableAttribute($attribute) |
||
356 | |||
357 | /** |
||
358 | * Add a column in collection using attributes. |
||
359 | * |
||
360 | * @param array $attributes |
||
361 | * @return $this |
||
362 | */ |
||
363 | public function addColumn(array $attributes) |
||
369 | |||
370 | /** |
||
371 | * Add a Column object in collection. |
||
372 | * |
||
373 | * @param \Yajra\Datatables\Html\Column $column |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function add(Column $column) |
||
382 | |||
383 | /** |
||
384 | * Set datatables columns from array definition. |
||
385 | * |
||
386 | * @param array $columns |
||
387 | * @return $this |
||
388 | */ |
||
389 | public function columns(array $columns) |
||
411 | |||
412 | /** |
||
413 | * Set title attribute of an array if not set. |
||
414 | * |
||
415 | * @param string $title |
||
416 | * @param array $attributes |
||
417 | * @return array |
||
418 | */ |
||
419 | public function setTitle($title, array $attributes) |
||
427 | |||
428 | /** |
||
429 | * Convert string into a readable title. |
||
430 | * |
||
431 | * @param string $title |
||
432 | * @return string |
||
433 | */ |
||
434 | public function getQualifiedTitle($title) |
||
438 | |||
439 | /** |
||
440 | * Add a checkbox column. |
||
441 | * |
||
442 | * @param array $attributes |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function addCheckbox(array $attributes = []) |
||
462 | |||
463 | /** |
||
464 | * Add a action column. |
||
465 | * |
||
466 | * @param array $attributes |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function addAction(array $attributes = []) |
||
487 | |||
488 | /** |
||
489 | * Add a index column. |
||
490 | * |
||
491 | * @param array $attributes |
||
492 | * @return $this |
||
493 | */ |
||
494 | public function addIndex(array $attributes = []) |
||
513 | |||
514 | /** |
||
515 | * Setup ajax parameter |
||
516 | * |
||
517 | * @param string|array $attributes |
||
518 | * @return $this |
||
519 | */ |
||
520 | public function ajax($attributes) |
||
526 | |||
527 | /** |
||
528 | * Generate DataTable's table html. |
||
529 | * |
||
530 | * @param array $attributes |
||
531 | * @param bool $drawFooter |
||
532 | * @return string |
||
533 | */ |
||
534 | public function table(array $attributes = [], $drawFooter = false) |
||
551 | |||
552 | /** |
||
553 | * Compile table headers and to support responsive extension. |
||
554 | * |
||
555 | * @return array |
||
556 | */ |
||
557 | private function compileTableHeaders() |
||
569 | |||
570 | /** |
||
571 | * Compile table footer contents. |
||
572 | * |
||
573 | * @return array |
||
574 | */ |
||
575 | private function compileTableFooter() |
||
584 | |||
585 | /** |
||
586 | * Configure DataTable's parameters. |
||
587 | * |
||
588 | * @param array $attributes |
||
589 | * @return $this |
||
590 | */ |
||
591 | public function parameters(array $attributes = []) |
||
597 | |||
598 | /** |
||
599 | * Set custom javascript template. |
||
600 | * |
||
601 | * @param string $template |
||
602 | * @return $this |
||
603 | */ |
||
604 | public function setTemplate($template) |
||
610 | |||
611 | /** |
||
612 | * Get collection of columns. |
||
613 | * |
||
614 | * @return Collection |
||
615 | */ |
||
616 | public function getColumns() |
||
620 | } |
||
621 |