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:
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() |
|
155 | |||
156 | /** |
||
157 | * Get export columns definition. |
||
158 | * |
||
159 | * @return array|string |
||
160 | */ |
||
161 | private function exportColumns() |
||
165 | |||
166 | /** |
||
167 | * Get columns definition from html builder. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | protected function getColumnsFromBuilder() |
||
175 | |||
176 | /** |
||
177 | * Optional method if you want to use html builder. |
||
178 | * |
||
179 | * @return \Yajra\Datatables\Html\Builder |
||
180 | */ |
||
181 | public function html() |
||
185 | |||
186 | /** |
||
187 | * Get Datatables Html Builder instance. |
||
188 | * |
||
189 | * @return \Yajra\Datatables\Html\Builder |
||
190 | */ |
||
191 | public function builder() |
||
195 | |||
196 | /** |
||
197 | * Get decorated data as defined in datatables ajax response. |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | protected function getAjaxResponseData() |
||
210 | |||
211 | /** |
||
212 | * Export results to CSV file. |
||
213 | * |
||
214 | * @return mixed |
||
215 | */ |
||
216 | public function csv() |
||
220 | |||
221 | /** |
||
222 | * Export results to PDF file. |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function pdf() |
||
230 | |||
231 | /** |
||
232 | * Display printable view of datatables. |
||
233 | * |
||
234 | * @return \Illuminate\Contracts\View\View |
||
235 | */ |
||
236 | public function printPreview() |
||
243 | |||
244 | /** |
||
245 | * Get mapped columns versus final decorated output. |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | View Code Duplication | protected function getDataForPrint() |
|
259 | |||
260 | /** |
||
261 | * Get printable columns. |
||
262 | * |
||
263 | * @return array|string |
||
264 | */ |
||
265 | protected function printColumns() |
||
269 | |||
270 | /** |
||
271 | * Add basic array query scopes. |
||
272 | * |
||
273 | * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function addScope(DataTableScopeContract $scope) |
||
282 | |||
283 | /** |
||
284 | * Apply query scopes. |
||
285 | * |
||
286 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
||
287 | * @return mixed |
||
288 | */ |
||
289 | protected function applyScopes($query) |
||
297 | |||
298 | /** |
||
299 | * Get default builder parameters. |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | protected function getBuilderParameters() |
||
324 | } |
||
325 |
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.