Complex classes like CollectionDataTable 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 CollectionDataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class CollectionDataTable extends DataTableAbstract |
||
11 | { |
||
12 | /** |
||
13 | * Collection object. |
||
14 | * |
||
15 | * @var \Illuminate\Support\Collection |
||
16 | */ |
||
17 | public $collection; |
||
18 | |||
19 | /** |
||
20 | * Collection object. |
||
21 | * |
||
22 | * @var \Illuminate\Support\Collection |
||
23 | */ |
||
24 | public $original; |
||
25 | |||
26 | /** |
||
27 | * The offset of the first record in the full dataset. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | private $offset = 0; |
||
32 | |||
33 | /** |
||
34 | * Can the DataTable engine be created with these parameters. |
||
35 | * |
||
36 | * @param mixed $source |
||
37 | * @return bool |
||
38 | */ |
||
39 | public static function canCreate($source) |
||
43 | |||
44 | /** |
||
45 | * Factory method, create and return an instance for the DataTable engine. |
||
46 | * |
||
47 | * @param array|\Illuminate\Support\Collection $source |
||
48 | * @return CollectionDataTable|DataTableAbstract |
||
49 | */ |
||
50 | public static function create($source) |
||
58 | |||
59 | /** |
||
60 | * CollectionEngine constructor. |
||
61 | * |
||
62 | * @param \Illuminate\Support\Collection $collection |
||
63 | */ |
||
64 | public function __construct(Collection $collection) |
||
72 | |||
73 | /** |
||
74 | * Serialize collection. |
||
75 | * |
||
76 | * @param mixed $collection |
||
77 | * @return mixed|null |
||
78 | */ |
||
79 | protected function serialize($collection) |
||
83 | |||
84 | /** |
||
85 | * Count results. |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | public function count() |
||
93 | |||
94 | /** |
||
95 | * Perform column search. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function columnSearch() |
||
137 | |||
138 | /** |
||
139 | * Perform pagination. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function paging() |
||
150 | |||
151 | /** |
||
152 | * Organizes works. |
||
153 | * |
||
154 | * @param bool $mDataSupport |
||
155 | * @return \Illuminate\Http\JsonResponse |
||
156 | */ |
||
157 | public function make($mDataSupport = true) |
||
180 | |||
181 | /** |
||
182 | * Count total items. |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | public function totalCount() |
||
190 | |||
191 | /** |
||
192 | * Get results. |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function results() |
||
200 | |||
201 | /** |
||
202 | * Revert transformed DT_RowIndex back to it's original values. |
||
203 | * |
||
204 | * @param bool $mDataSupport |
||
205 | */ |
||
206 | private function revertIndexColumn($mDataSupport) |
||
218 | |||
219 | /** |
||
220 | * Perform global search for the given keyword. |
||
221 | * |
||
222 | * @param string $keyword |
||
223 | */ |
||
224 | protected function globalSearch($keyword) |
||
252 | |||
253 | /** |
||
254 | * Perform default query orderBy clause. |
||
255 | */ |
||
256 | protected function defaultOrdering() |
||
277 | |||
278 | /** |
||
279 | * Get array sorter closure. |
||
280 | * |
||
281 | * @param array $criteria |
||
282 | * @return \Closure |
||
283 | */ |
||
284 | protected function getSorter(array $criteria) |
||
321 | |||
322 | /** |
||
323 | * Resolve callback parameter instance. |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | protected function resolveCallbackParameter() |
||
331 | |||
332 | /** |
||
333 | * Define the offset of the first item of the collection with respect to |
||
334 | * the FULL dataset the collection was sliced from. It effectively allows the |
||
335 | * collection to be "pre-sliced". |
||
336 | * |
||
337 | * @param int $offset |
||
338 | * @return $this |
||
339 | */ |
||
340 | public function setOffset(int $offset) |
||
346 | } |
||
347 |