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( |
||
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']) |
||
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 = []) |
||
172 | |||
173 | /** |
||
174 | * Encode columns render function. |
||
175 | * |
||
176 | * @param array $parameters |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function encodeColumnFunctions(array $parameters) |
||
195 | |||
196 | /** |
||
197 | * Encode DataTables callbacks function. |
||
198 | * |
||
199 | * @param array $parameters |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function encodeCallbackFunctions(array $parameters) |
||
214 | |||
215 | /** |
||
216 | * Compile DataTable callback value. |
||
217 | * |
||
218 | * @param mixed $callback |
||
219 | * @return mixed|string |
||
220 | */ |
||
221 | private function compileCallback($callback) |
||
231 | |||
232 | /** |
||
233 | * Decode columns render functions. |
||
234 | * |
||
235 | * @param array $columnFunctions |
||
236 | * @param string $json |
||
237 | * @return string |
||
238 | */ |
||
239 | protected function decodeColumnFunctions(array $columnFunctions, $json) |
||
247 | |||
248 | /** |
||
249 | * Decode DataTables callbacks function. |
||
250 | * |
||
251 | * @param array $callbackFunctions |
||
252 | * @param string $json |
||
253 | * @return string |
||
254 | */ |
||
255 | protected function decodeCallbackFunctions(array $callbackFunctions, $json) |
||
263 | |||
264 | /** |
||
265 | * Get javascript template to use. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | protected function template() |
||
275 | |||
276 | /** |
||
277 | * Add a column in collection using attributes. |
||
278 | * |
||
279 | * @param array $attributes |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function addColumn(array $attributes) |
||
288 | |||
289 | /** |
||
290 | * Add a Column object in collection. |
||
291 | * |
||
292 | * @param \Yajra\Datatables\Html\Column $column |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function add(Column $column) |
||
301 | |||
302 | /** |
||
303 | * Set datatables columns from array definition. |
||
304 | * |
||
305 | * @param array $columns |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function columns(array $columns) |
||
330 | |||
331 | /** |
||
332 | * Set title attribute of an array if not set. |
||
333 | * |
||
334 | * @param string $title |
||
335 | * @param array $attributes |
||
336 | * @return array |
||
337 | */ |
||
338 | public function setTitle($title, array $attributes) |
||
346 | |||
347 | /** |
||
348 | * Convert string into a readable title. |
||
349 | * |
||
350 | * @param string $title |
||
351 | * @return string |
||
352 | */ |
||
353 | public function getQualifiedTitle($title) |
||
357 | |||
358 | /** |
||
359 | * Add a checkbox column. |
||
360 | * |
||
361 | * @param array $attributes |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function addCheckbox(array $attributes = []) |
||
381 | |||
382 | /** |
||
383 | * Add a action column. |
||
384 | * |
||
385 | * @param array $attributes |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function addAction(array $attributes = []) |
||
406 | |||
407 | /** |
||
408 | * Setup ajax parameter |
||
409 | * |
||
410 | * @param string|array $attributes |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function ajax($attributes) |
||
419 | |||
420 | /** |
||
421 | * Generate DataTable's table html. |
||
422 | * |
||
423 | * @param array $attributes |
||
424 | * @param bool $drawFooter |
||
425 | * @return string |
||
426 | */ |
||
427 | public function table(array $attributes = [], $drawFooter = false) |
||
444 | |||
445 | /** |
||
446 | * Compile table headers and to support responsive extension. |
||
447 | * |
||
448 | * @return array |
||
449 | */ |
||
450 | private function compileTableHeaders() |
||
462 | |||
463 | /** |
||
464 | * Compile table footer contents. |
||
465 | * |
||
466 | * @return array |
||
467 | */ |
||
468 | private function compileTableFooter() |
||
477 | |||
478 | /** |
||
479 | * Configure DataTable's parameters. |
||
480 | * |
||
481 | * @param array $attributes |
||
482 | * @return $this |
||
483 | */ |
||
484 | public function parameters(array $attributes = []) |
||
490 | |||
491 | /** |
||
492 | * Set custom javascript template. |
||
493 | * |
||
494 | * @param string $template |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function setTemplate($template) |
||
503 | |||
504 | /** |
||
505 | * Get collection of columns. |
||
506 | * |
||
507 | * @return Collection |
||
508 | */ |
||
509 | public function getColumns() |
||
513 | } |
||
514 |