Completed
Push — layout_cleanup ( efb6e0 )
by Tony
02:59
created

InventoryDataTable::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * app/DataTables/General/InventoryDataTable.php
4
 *
5
 * Datatable for inventory
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\General;
27
28
use App\Models\General\Inventory;
29
use Yajra\Datatables\Services\DataTable;
30
31 View Code Duplication
class InventoryDataTable extends DataTable
0 ignored issues
show
Duplication introduced by
This class 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...
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('device.hostname', function($inventory) {
0 ignored issues
show
Documentation introduced by
function ($inventory) { ...e->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...
43
                return '<a href="'.url("devices/".$inventory->device->device_id).'">'.$inventory->device->hostname.'</a>';
44
            })
45
            ->make(true);
46
    }
47
48
    /**
49
     * Get the query object to be processed by datatables.
50
     *
51
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
52
     */
53
    public function query()
54
    {
55
        $inventory = Inventory::with('device')->select('entPhysical.*');
56
        return $this->applyScopes($inventory);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($inventory); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 56 which is incompatible with the return type declared by the interface Yajra\Datatables\Contrac...ataTableContract::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
57
    }
58
59
    /**
60
     * Optional method if you want to use html builder.
61
     *
62
     * @return \Yajra\Datatables\Html\Builder
63
     */
64
    public function html()
65
    {
66
        return $this->builder()
67
                    ->columns($this->getColumns())
68
                    ->parameters($this->getBuilderParameters());
69
    }
70
71
    /**
72
     * Get columns.
73
     *
74
     * @return array
75
     */
76
    private function getColumns()
77
    {
78
        return [
79
            'device.hostname' => [
80
                'title'       => trans('devices.label.hostname'),
81
            ],
82
            'entPhysicalDescr'      => [
83
                'title' => trans('general.text.description'),
84
            ],
85
            'entPhysicalName'   => [
86
                'title' => trans('general.text.name'),
87
            ],
88
            'entPhysicalModelName'  => [
89
                'title' => trans('general.text.model'),
90
            ],
91
            'entPhysicalSerialNum'  => [
92
                'title' => trans('general.text.serial'),
93
            ],
94
        ];
95
    }
96
97
    /**
98
     * Get filename for export.
99
     *
100
     * @return string
101
     */
102
    protected function filename()
103
    {
104
        return 'inventory';
105
    }
106
107
    /**
108
     * Get Builder Params
109
     *
110
     * @return array
111
     */
112
    protected function getBuilderParameters()
113
    {
114
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('dom' => 'B...', 'reset', 'reload')); seems to be an array, but some of its elements' types (string) 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...
115
            'dom' => 'Blfrtip',
116
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
117
            'buttons' => [
118
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
119
            ],
120
        ];
121
    }
122
123
}
124