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

SensorDataDataTable::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
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