Completed
Push — user-management ( d87e21 )
by Tony
03:32
created

IPv4DataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 69
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ajax() 14 14 3
A query() 6 6 1
A getColumns() 17 17 1
A filename() 4 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
 * app/DataTables/General/IPv4DataTable.php
4
 *
5
 * Datatable for ipv4 search
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\DataTables\BaseDataTable;
29
use App\Models\General\IPv4;
30
31 View Code Duplication
class IPv4DataTable extends BaseDataTable
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
    /**
35
     * Display ajax response.
36
     *
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function ajax()
40
    {
41
        return $this->datatables
42
            ->eloquent($this->query())
43
            ->editColumn('hostname', function($data) {
0 ignored issues
show
Documentation introduced by
function ($data) { $.... $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...
44
                $hostname = is_null($data->device) ? trans('devices.text.deleted') : $data->device->hostname;
45
                return '<a href="'.url("devices/".$data->device_id).'">'.$hostname.'</a>';
46
            })
47
            ->editColumn('ifName', function($data) {
0 ignored issues
show
Documentation introduced by
function ($data) { $...' . $ifName . '</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...
48
                $ifName = is_null($data->ifName) ? trans('devices.text.deleted') : $data->ifName;
49
                return '<a href="'.url("devices/".$data->device_id."/ports/".$data->port_id).'">'.$ifName.'</a>';
50
            })
51
            ->make(true);
52
    }
53
54
    /**
55
     * Get the query object to be processed by datatables.
56
     *
57
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
58
     */
59
    public function query()
60
    {
61
        $data = IPv4::join('ports', 'ports.port_id', '=', 'ipv4_addresses.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv4_addresses.*', 'ports.*', 'devices.*');
62
        //FIXME We should use this once laravel-datatables supports it upstream $data = IPv4::with('port.device')->select('ipv4_addresses.*');
63
        return $this->applyScopes($data);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($data); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 63 which is incompatible with the return type declared by the interface Yajra\Datatables\Contrac...ataTableContract::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
64
    }
65
66
    /**
67
     * Get columns.
68
     *
69
     * @return array
70
     */
71
    public function getColumns()
72
    {
73
        return [
74
            'hostname'  => [
75
                'title' => trans('devices.label.hostname'),
76
            ],
77
            'ifName'    => [
78
                'title' => trans('general.text.interface'),
79
            ],
80
            'ipv4_address' => [
81
                'title'    => trans('general.text.address'),
82
            ],
83
            'ifDescr'   => [
84
                'title' => trans('general.text.port_descr'),
85
            ],
86
        ];
87
    }
88
89
    /**
90
     * Get filename for export.
91
     *
92
     * @return string
93
     */
94
    protected function filename()
95
    {
96
        return 'ipv4';
97
    }
98
99
}
100