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 |
||
| 9 | class DevicesDataTable extends DataTable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Build DataTable class. |
||
| 13 | * |
||
| 14 | * @return \Yajra\DataTables\Engines\BaseEngine |
||
| 15 | */ |
||
| 16 | public function dataTable($query) |
||
| 17 | { |
||
| 18 | return datatables($query) |
||
| 19 | ->editColumn('name', function ($device) { |
||
| 20 | return '<a href="' . route('device.show', $device->id) . '">' . $device->name . '</a>'; |
||
| 21 | }) |
||
| 22 | ->addColumn('location', function ($device) { |
||
| 23 | return ($device->location->name ?? 'null'); |
||
| 24 | }) |
||
| 25 | ->addColumn('site', function ($device) { |
||
| 26 | return ($device->location->site->name ?? 'null'); |
||
| 27 | }) |
||
| 28 | ->addColumn('rates', function ($device) { |
||
| 29 | return $device->update_rate . '/' . $device->image_rate .'/' . $device->sensor_rate; |
||
| 30 | }) |
||
| 31 | ->blacklist([ 'location', 'site', 'rates' ]) |
||
| 32 | ->rawColumns([ 'name' ]); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Get the query object to be processed by dataTables. |
||
| 37 | * |
||
| 38 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection |
||
| 39 | */ |
||
| 40 | public function query() |
||
| 41 | { |
||
| 42 | $query = Device::query(); |
||
| 43 | return $this->applyScopes($query); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Optional method if you want to use html builder. |
||
| 48 | * |
||
| 49 | * @return \Yajra\DataTables\Html\Builder |
||
| 50 | */ |
||
| 51 | public function html() |
||
| 52 | { |
||
| 53 | return $this->builder() |
||
| 54 | ->columns($this->getColumns()) |
||
| 55 | ->minifiedAjax() |
||
| 56 | ->parameters($this->getBuilderParameters()); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get columns. |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | protected function getColumns() |
||
| 65 | { |
||
| 66 | return [ |
||
| 67 | 'id', |
||
| 68 | 'name', |
||
| 69 | 'location', |
||
| 70 | 'site', |
||
| 71 | 'open_time', |
||
| 72 | 'close_time', |
||
| 73 | 'rates' |
||
| 74 | ]; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get builder parameters. |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | View Code Duplication | protected function getBuilderParameters() |
|
| 83 | { |
||
| 84 | return [ |
||
| 85 | 'dom' => 'Bfrtip', |
||
| 86 | 'order' => [ [ 0, 'asc' ] ], |
||
| 87 | 'buttons' => [ |
||
| 88 | 'create', |
||
| 89 | 'export', |
||
| 90 | 'print', |
||
| 91 | 'reset', |
||
| 92 | 'reload', |
||
| 93 | ], |
||
| 94 | 'paging' => true, |
||
| 95 | 'searching' => true, |
||
| 96 | 'info' => true, |
||
| 97 | 'searchDelay' => 500, |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get filename for export. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | protected function filename() |
||
| 110 | } |
||
| 111 |