Completed
Pull Request — master (#82)
by Brandon
02:15
created

SensorDataDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 19.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 18
loc 93
ccs 0
cts 22
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dataTable() 0 10 1
A query() 0 5 1
A html() 0 7 1
A getColumns() 0 11 1
A getBuilderParameters() 18 18 1
A filename() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace App\DataTables;
4
5
use Yajra\DataTables\Services\DataTable;
6
use App\SensorData;
7
8
class SensorDataDataTable extends DataTable
9
{
10
    /**
11
     * Build DataTable class.
12
     *
13
     * @return \Yajra\DataTables\Engines\BaseEngine
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
1 ignored issue
show
Bug introduced by
The method editColumn does only exist in Yajra\DataTables\DataTableAbstract, but not in Yajra\DataTables\DataTables.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
18
            ->editColumn('sensor_id', function ($sensordata) {
19
                return '<a href="/sensor/' . $sensordata->sensor_id . '">'. $sensordata->sensor->name . '</a>';
20
            })
21
            ->addColumn('action', 'sensordata.action')
22
            ->blacklist([ 'action'])
23
            ->rawColumns(['sensor_id', 'action']);
24
    }
25
26
    /**
27
     * Get the query object to be processed by dataTables.
28
     *
29
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
30
     */
31
    public function query()
32
    {
33
        $query = SensorData::query();
34
        return $this->applyScopes($query);
35
    }
36
37
    /**
38
     * Optional method if you want to use html builder.
39
     *
40
     * @return \Yajra\DataTables\Html\Builder
41
     */
42
    public function html()
43
    {
44
        return $this->builder()
45
            ->columns($this->getColumns())
46
            ->minifiedAjax()
47
            ->parameters($this->getBuilderParameters());
48
    }
49
50
    /**
51
     * Get columns.
52
     *
53
     * @return array
54
     */
55
    protected function getColumns()
56
    {
57
        return [
58
            'id',
59
            'sensor_id',
60
            'value',
61
            'created_at',
62
            'updated_at',
63
            'action'
64
        ];
65
    }
66
67
    /**
68
     * Get builder parameters.
69
     *
70
     * @return array
71
     */
72 View Code Duplication
    protected function getBuilderParameters()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
73
    {
74
        return [
75
            'dom'     => 'Bfrtip',
76
            'order'   => [ [ 0, 'desc' ] ],
77
            'buttons' => [
78
                'create',
79
                'export',
80
                'print',
81
                'reset',
82
                'reload',
83
            ],
84
            'paging' => true,
85
            'searching' => true,
86
            'info' => true,
87
            'searchDelay' => 500,
88
        ];
89
    }
90
91
    /**
92
     * Get filename for export.
93
     *
94
     * @return string
95
     */
96
    protected function filename()
97
    {
98
        return 'sensordata_'.time();
99
    }
100
}
101