Completed
Pull Request — develop (#100)
by Neil
18:13
created

LogsDataTable::getBuilderParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * app/DataTables/Alerting/LogsDataTable.php
4
 *
5
 * Datatable for alert logs
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\Alerting;
27
28
use App\Models\Alerting\Alert;
29
use App\Models\Alerting\Log;
30
use Yajra\Datatables\Services\DataTable;
31
32
class LogsDataTable extends DataTable
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('device.hostname', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...'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
                return '<a href="'.url("devices/".$this['device']['device_id']).'">'.$this['device']['hostname'].'</a>';
45
            })
46
            ->editColumn('rule.name', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { i...text.invalid'); } } 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...
47
                if ($this['rule']['id']) {
48
                    return '<a href="'.url("alerting/rules/".$this['rule']['id']).'">'.$this['rule']['name'].'</a>';
49
                } else {
50
                    return trans('alerting.general.text.invalid');
51
                }
52
            })
53
            ->editColumn('state', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { $...> ' . $text . '</b>'; } 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...
54
                $icon = '';
55
                if ($this['state'] == 0) {
56
                    $icon   = 'check';
57
                    $colour = 'green';
58
                    $text   = trans('alerting.logs.text.ok');
59
                }
60
                elseif ($this['state'] == 1) {
61
                    $icon   = 'times';
62
                    $colour = 'red';
63
                    $text   = trans('alerting.logs.text.fail');
64
                }
65
                elseif ($this['state'] == 2) {
66
                    $icon   = 'volume-off';
67
                    $colour = 'lightgrey';
68
                    $text   = trans('alerting.logs.text.ack');
69
                }
70
                elseif ($this['state'] == 3) {
71
                    $icon   = 'arrow-down';
72
                    $colour = 'orange';
73
                    $text   = trans('alerting.logs.text.worse');
74
                }
75
                elseif ($this['state'] == 4) {
76
                    $icon   = 'arrow-up';
77
                    $colour = 'khaki';
78
                    $text   = trans('alerting.logs.text.better');
79
                }
80
                return '<b><span class="fa fa-'.$icon.'" style="color:'.$colour.'"></span> '.$text.'</b>';
0 ignored issues
show
Bug introduced by
The variable $colour does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $text does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
            })
82
            ->make(true);
83
    }
84
85
    /**
86
     * Get the query object to be processed by datatables.
87
     *
88
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
89
     */
90
    public function query()
91
    {
92
        $logs = Log::query()->with('device','user','rule')->select('alert_log.*');
93
        return $this->applyScopes($logs);
94
    }
95
96
    /**
97
     * Optional method if you want to use html builder.
98
     *
99
     * @return \Yajra\Datatables\Html\Builder
100
     */
101 1
    public function html()
102
    {
103 1
        return $this->builder()
104 1
                    ->columns($this->getColumns())
105 1
                    ->parameters($this->getBuilderParameters());
106
    }
107
108
    /**
109
     * Get columns.
110
     *
111
     * @return array
112
     */
113 1
    private function getColumns()
114
    {
115
        return [
116
            'device.hostname' => [
117 1
                'title'       => trans('devices.label.hostname'),
118 1
            ],
119
            'rule.name' => [
120 1
                'title' => trans('alerting.general.text.rule'),
121 1
            ],
122
            'state'     => [
123 1
                'title' => trans('alerting.general.text.state'),
124 1
            ],
125 1
            'time_logged',
126 1
        ];
127
    }
128
129
    /**
130
     * Get filename for export.
131
     *
132
     * @return string
133
     */
134
    protected function filename()
135
    {
136
        return 'logs';
137
    }
138
139
    /**
140
     * Get Builder Params
141
     *
142
     * @return array
143
     */
144 1
    protected function getBuilderParameters()
145
    {
146
        return [
147 1
            'dom' => 'Blfrtip',
148 1
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
149
            'buttons' => [
150 1
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
151 1
            ],
152 1
        ];
153
    }
154
155
}
156