Completed
Push — datatables-fix ( e9c4b2...619c08 )
by Tony
02:59
created

DeviceDataTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 2
cbo 4
dl 0
loc 141
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 38 3
A query() 0 5 1
A html() 0 6 1
B getColumns() 0 36 1
A filename() 0 4 1
A getBuilderParameters() 0 13 1
1
<?php
2
/**
3
 * app/DataTables/DeviceDataTable.php
4
 *
5
 * Datatable for devices
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\DataTables;
27
28
use App\Models\Device;
29
use Yajra\Datatables\Services\DataTable;
30
31
class DeviceDataTable extends DataTable
32
{
33
    /**
34
     * Display ajax response.
35
     *
36
     * @return \Illuminate\Http\JsonResponse
37
     */
38
    public function ajax()
39
    {
40
        return $this->datatables
41
            ->eloquent($this->query())
42
            ->editColumn('status_reason', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...ue">up</span>'; } } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
                if ($device->status == 0)
44
                {
45
                    return '<span data-toggle="tooltip" title="down" class="badge bg-red">'.$device->status_reason.'</span>';
46
                }
47
                else
48
                {
49
                    return '<span data-toggle="tooltip" title="up" class="badge bg-light-blue">up</span>';
50
                }
51
            })
52
            ->editColumn('vendor', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { .... $device->os . '">'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
                return '<img src="'.$device->logo().'" border="0" alt="'.$device->os.'">';
54
            })
55
            ->editColumn('hostname', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { .... $hostname . '</a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
                $hostname = is_null($device) ? trans('devices.text.deleted') : $device->hostname;
57
                return '<a href="'.url("devices/".$device->device_id).'">'.$hostname.'</a>';
58
            })
59
            ->editColumn('resources', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...$sensors . '</span>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
                $ports   = $device->ports()->count();
61
                $sensors = $device->sensors()->count();
62
                return '<span data-toggle="tooltip" title="'.$ports.' Ports" class="badge bg-light-blue"><i class="fa fa-link"></i>&nbsp; '.$ports.'</span><br />
63
                        <span data-toggle="tooltip" title="'.$sensors.' Sensors" class="badge bg-light-blue"><i class="fa fa-dashboard"></i>&nbsp; '.$sensors.'</span>';
64
            })
65
            ->editColumn('hardware', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ... . $device->features; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                return $device->hardware.'<br />'.$device->features;
67
            })
68
            ->editColumn('os', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...' . $device->version; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
                return ucfirst($device->os).'<br />'.$device->version;
70
            })
71
            ->editColumn('location', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ... . $device->location; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
                return $device->formatUptime($device->uptime).'<br />'.$device->location;
73
            })
74
            ->make(true);
75
    }
76
77
    /**
78
     * Get the query object to be processed by datatables.
79
     *
80
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
81
     */
82
    public function query()
83
    {
84
        $device = Device::select('devices.*');
85
        return $this->applyScopes($device);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($device); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 85 which is incompatible with the return type declared by the interface Yajra\Datatables\Contrac...ataTableContract::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
86
    }
87
88
    /**
89
     * Optional method if you want to use html builder.
90
     *
91
     * @return \Yajra\Datatables\Html\Builder
92
     */
93
    public function html()
94
    {
95
        return $this->builder()
96
                    ->columns($this->getColumns())
97
                    ->parameters($this->getBuilderParameters());
98
    }
99
100
    /**
101
     * Get columns.
102
     *
103
     * @return array
104
     */
105
    private function getColumns()
106
    {
107
        return [
108
            'status'    => [
109
                'title' => trans('general.text.status'),
110
                'data'  => 'status_reason',
111
                'width' => '40px',
112
            ],
113
            'vendor'        => [
114
                'title' => trans('devices.text.vendor'),
115
                'width' => '20px',
116
            ],
117
            'hostname'  => [
118
                'title' => trans('devices.label.hostname'),
119
            ],
120
            'resources'  => [
121
                'title'  => '',
122
                'search' => false,
123
            ],
124
            'hardware'  => [
125
                'title' => trans('devices.text.platform'),
126
            ],
127
            'features'    => [
128
                'visible' => false,
129
            ],
130
            'os'        => [
131
                'title' => trans('devices.text.os'),
132
            ],
133
            'version'     => [
134
                'visible' => false,
135
            ],
136
            'location'  => [
137
                'title' => trans('devices.text.uptime_location'),
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * Get filename for export.
144
     *
145
     * @return string
146
     */
147
    protected function filename()
148
    {
149
        return 'devices';
150
    }
151
152
    /**
153
     * Get Builder Params
154
     *
155
     * @return array
156
     */
157
    protected function getBuilderParameters()
158
    {
159
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('dom' => '<... 'autoWidth' => false); seems to be an array, but some of its elements' types (string|false) are incompatible with the return type of the parent method Yajra\Datatables\Service...e::getBuilderParameters of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
160
            'dom' => "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>".
161
                     "<'row'<'col-sm-12'tr>>".
162
                     "<'row'<'col-sm-5'i><'col-sm-7'p>>",
163
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
164
            'buttons' => [
165
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
166
            ],
167
            'autoWidth' => false,
168
        ];
169
    }
170
171
}
172