Issues (2963)

app/Http/Controllers/Table/VminfoController.php (2 issues)

1
<?php
2
/**
3
 * SyslogController.php
4
 *
5
 * -Description-
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 <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Table;
27
28
use App\Models\Device;
29
use App\Models\Vminfo;
30
use LibreNMS\Util\Url;
31
32
class VminfoController extends TableController
33
{
34
    public function searchFields($request)
35
    {
36
        return ['vmwVmDisplayName', 'vmwVmGuestOS', 'devices.hostname', 'devices.sysname'];
37
    }
38
39
    public function sortFields($request)
40
    {
41
        return ['vmwVmDisplayName', 'vmwVmGuestOS', 'vmwVmMemSize', 'vmwVmCpus', 'vmwVmState', 'hostname'];
42
    }
43
44
    /**
45
     * Defines the base query for this resource
46
     *
47
     * @param  \Illuminate\Http\Request  $request
48
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
49
     */
50
    public function baseQuery($request)
51
    {
52
        return Vminfo::hasAccess($request->user())
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Vminfo...ion(...) { /* ... */ }) also could return the type App\Models\Vminfo which is incompatible with the documented return type Illuminate\Database\Eloq...\Database\Query\Builder.
Loading history...
53
            ->select('vminfo.*')
54
            ->with('device')
55
            ->with('parentDevice')
56
            ->when($request->get('searchPhrase') || in_array('hostname', array_keys($request->get('sort', []))), function ($query) {
57
                $query->leftJoin('devices', 'devices.device_id', 'vminfo.device_id');
58
            });
59
    }
60
61
    /**
62
     * @param  Vminfo  $vm
63
     */
64
    public function formatItem($vm)
65
    {
66
        return [
67
            'vmwVmState' => '<span class="label ' . $vm->stateLabel[1] . '">' . $vm->stateLabel[0] . '</span>',
68
            'vmwVmDisplayName' => is_null($vm->parentDevice) ? $vm->vmwVmDisplayName : self::getHostname($vm->parentDevice),
69
            'vmwVmGuestOS' => $vm->operatingSystem,
70
            'vmwVmMemSize' => $vm->memoryFormatted,
71
            'vmwVmCpus' => $vm->vmwVmCpus,
72
            'hostname' => self::getHostname($vm->device),
0 ignored issues
show
It seems like $vm->device can also be of type null; however, parameter $device of App\Http\Controllers\Tab...ntroller::getHostname() does only seem to accept App\Models\Device, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            'hostname' => self::getHostname(/** @scrutinizer ignore-type */ $vm->device),
Loading history...
73
            'deviceid' => $vm->device_id,
74
            'sysname' => $vm->device->sysName,
75
76
        ];
77
    }
78
79
    private static function getHostname(Device $device): string
80
    {
81
        return '<a class="list-device" href="' . Url::deviceUrl($device) . '">' . $device->hostname . '</a><br>' . $device->sysName;
82
    }
83
}
84