Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DataTable 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 DataTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class DataTable implements DataTableContract, DataTableButtonsContract |
||
15 | { |
||
16 | /** |
||
17 | * @var \Yajra\Datatables\Datatables |
||
18 | */ |
||
19 | protected $datatables; |
||
20 | |||
21 | /** |
||
22 | * @var \Illuminate\Contracts\View\Factory |
||
23 | */ |
||
24 | protected $viewFactory; |
||
25 | |||
26 | /** |
||
27 | * Datatables print preview view. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $printPreview; |
||
32 | |||
33 | /** |
||
34 | * List of columns to be exported. |
||
35 | * |
||
36 | * @var string|array |
||
37 | */ |
||
38 | protected $exportColumns = '*'; |
||
39 | |||
40 | /** |
||
41 | * List of columns to be printed. |
||
42 | * |
||
43 | * @var string|array |
||
44 | */ |
||
45 | protected $printColumns = '*'; |
||
46 | |||
47 | /** |
||
48 | * Query scopes. |
||
49 | * |
||
50 | * @var \Yajra\Datatables\Contracts\DataTableScopeContract[] |
||
51 | */ |
||
52 | protected $scopes = []; |
||
53 | |||
54 | /** |
||
55 | * @param \Yajra\Datatables\Datatables $datatables |
||
56 | * @param \Illuminate\Contracts\View\Factory $viewFactory |
||
57 | */ |
||
58 | public function __construct(Datatables $datatables, Factory $viewFactory) |
||
63 | |||
64 | /** |
||
65 | * Render view. |
||
66 | * |
||
67 | * @param $view |
||
68 | * @param array $data |
||
69 | * @param array $mergeData |
||
70 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
71 | */ |
||
72 | public function render($view, $data = [], $mergeData = []) |
||
95 | |||
96 | /** |
||
97 | * Get Datatables Request instance. |
||
98 | * |
||
99 | * @return \Yajra\Datatables\Request |
||
100 | */ |
||
101 | public function request() |
||
105 | |||
106 | /** |
||
107 | * Export results to Excel file. |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function excel() |
||
115 | |||
116 | /** |
||
117 | * Build excel file and prepare for export. |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | protected function buildExcelFile() |
||
129 | |||
130 | /** |
||
131 | * Get filename for export. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function filename() |
||
139 | |||
140 | /** |
||
141 | * Get mapped columns versus final decorated output. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | View Code Duplication | protected function getDataForExport() |
|
157 | |||
158 | /** |
||
159 | * Get decorated data as defined in datatables ajax response. |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | protected function getAjaxResponseData() |
||
172 | |||
173 | /** |
||
174 | * Get export columns definition. |
||
175 | * |
||
176 | * @return array|string |
||
177 | */ |
||
178 | private function exportColumns() |
||
182 | |||
183 | /** |
||
184 | * Get columns definition from html builder. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getColumnsFromBuilder() |
||
192 | |||
193 | /** |
||
194 | * Optional method if you want to use html builder. |
||
195 | * |
||
196 | * @return \Yajra\Datatables\Html\Builder |
||
197 | */ |
||
198 | public function html() |
||
202 | |||
203 | /** |
||
204 | * Get Datatables Html Builder instance. |
||
205 | * |
||
206 | * @return \Yajra\Datatables\Html\Builder |
||
207 | */ |
||
208 | public function builder() |
||
212 | |||
213 | /** |
||
214 | * @param array $row |
||
215 | * @param array|\Illuminate\Support\Collection $columns |
||
216 | * @return array |
||
217 | */ |
||
218 | protected function buildExportColumn(array $row, $columns) |
||
222 | |||
223 | /** |
||
224 | * Build printable and exportable column. |
||
225 | * |
||
226 | * @param array $row |
||
227 | * @param array|\Illuminate\Support\Collection $columns |
||
228 | * @param string $type |
||
229 | * @return array |
||
230 | */ |
||
231 | protected function buildColumn(array $row, $columns, $type) |
||
239 | |||
240 | /** |
||
241 | * Build column from collection. |
||
242 | * |
||
243 | * @param array $row |
||
244 | * @param \Illuminate\Support\Collection $columns |
||
245 | * @param string $type |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function buildColumnByCollection(array $row, Collection $columns, $type) |
||
261 | |||
262 | /** |
||
263 | * Export results to CSV file. |
||
264 | * |
||
265 | * @return mixed |
||
266 | */ |
||
267 | public function csv() |
||
271 | |||
272 | /** |
||
273 | * Export results to PDF file. |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public function pdf() |
||
281 | |||
282 | /** |
||
283 | * Display printable view of datatables. |
||
284 | * |
||
285 | * @return \Illuminate\Contracts\View\View |
||
286 | */ |
||
287 | public function printPreview() |
||
294 | |||
295 | /** |
||
296 | * Get mapped columns versus final decorated output. |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | View Code Duplication | protected function getDataForPrint() |
|
312 | |||
313 | /** |
||
314 | * Get printable columns. |
||
315 | * |
||
316 | * @return array|string |
||
317 | */ |
||
318 | protected function printColumns() |
||
322 | |||
323 | /** |
||
324 | * Build printable column. |
||
325 | * |
||
326 | * @param array $row |
||
327 | * @param array|\Illuminate\Support\Collection $columns |
||
328 | * @return array |
||
329 | */ |
||
330 | protected function buildPrintColumn(array $row, $columns) |
||
334 | |||
335 | /** |
||
336 | * Add basic array query scopes. |
||
337 | * |
||
338 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function addScope(DataTableScopeContract $scope) |
||
347 | |||
348 | /** |
||
349 | * @param array $row |
||
350 | * @param $type |
||
351 | * @param $column |
||
352 | * @param $results |
||
353 | * @return mixed |
||
354 | */ |
||
355 | protected function getRowColumnData(array $row, $type, $column, $results) |
||
372 | |||
373 | /** |
||
374 | * Apply query scopes. |
||
375 | * |
||
376 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
377 | * @return mixed |
||
378 | */ |
||
379 | protected function applyScopes($query) |
||
387 | |||
388 | /** |
||
389 | * Get default builder parameters. |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | protected function getBuilderParameters() |
||
414 | } |
||
415 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.