Skip to content

Commit

Permalink
PHPStan fixes (librenms#13038)
Browse files Browse the repository at this point in the history
* PHPStan fixes
mostly type fixes
semi-risky changes in availability map widget, tested a bit

* fix style

* Style fix

* restore spaces stupid editor removed

* fix the rest

* device model back

* remove ignores

* introduce variable
  • Loading branch information
murrant authored Jul 13, 2021
1 parent c563efd commit 58ca599
Show file tree
Hide file tree
Showing 61 changed files with 323 additions and 441 deletions.
5 changes: 3 additions & 2 deletions LibreNMS/Modules/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public function discover(\LibreNMS\OS $os)

public function poll(\LibreNMS\OS $os)
{
$deviceModel = $os->getDevice();
$deviceModel = $os->getDevice(); /** @var \App\Models\Device $deviceModel */
if ($os instanceof OSPolling) {
$os->pollOS();
} else {
// legacy poller files
global $graphs, $device;
$location = null;

if (is_file(base_path('/includes/polling/os/' . $device['os'] . '.inc.php'))) {
// OS Specific
include base_path('/includes/polling/os/' . $device['os'] . '.inc.php');
Expand All @@ -74,7 +76,6 @@ public function poll(\LibreNMS\OS $os)
$deviceModel->serial = ($serial ?? $deviceModel->serial) ?: null;

if (! empty($location)) { // legacy support, remove when no longer needed
/** @phpstan-ignore-next-line */
$deviceModel->setLocation($location);
optional($deviceModel->location)->save();
}
Expand Down
17 changes: 11 additions & 6 deletions LibreNMS/Util/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use App\Models\Service;
use App\Models\Vrf;
use Cache;
use Illuminate\Support\Collection;

class ObjectCache
{
Expand All @@ -47,10 +48,13 @@ class ObjectCache
public static function applications()
{
return Cache::remember('ObjectCache:applications_list:' . auth()->id(), self::$cache_time, function () {
return Application::hasAccess(auth()->user())
->select('app_type', 'app_state', 'app_instance')
$user = auth()->user(); /** @var \App\Models\User $user */
$applications = Application::hasAccess($user)
->select(['app_type', 'app_state', 'app_instance'])
->groupBy('app_type', 'app_state', 'app_instance')
->get()
->get(); /** @var Collection $applications */

return $applications
->sortBy('show_name', SORT_NATURAL | SORT_FLAG_CASE)
->groupBy('app_type');
});
Expand All @@ -59,7 +63,7 @@ public static function applications()
public static function routing()
{
return Cache::remember('ObjectCache:routing_counts:' . auth()->id(), self::$cache_time, function () {
$user = auth()->user();
$user = auth()->user(); /** @var \App\Models\User $user */

return [
'vrf' => Vrf::hasAccess($user)->count(),
Expand All @@ -76,7 +80,8 @@ public static function routing()
public static function sensors()
{
return Cache::remember('ObjectCache:sensor_list:' . auth()->id(), self::$cache_time, function () {
$sensor_classes = Sensor::hasAccess(auth()->user())->select('sensor_class')->groupBy('sensor_class')->orderBy('sensor_class')->get();
$user = auth()->user(); /** @var \App\Models\User $user */
$sensor_classes = Sensor::hasAccess($user)->select('sensor_class')->groupBy('sensor_class')->orderBy('sensor_class')->get();

$sensor_menu = [];
foreach ($sensor_classes as $sensor_model) {
Expand All @@ -100,7 +105,7 @@ public static function sensors()
];
}

if (PrinterSupply::hasAccess(auth()->user())->exists()) {
if (PrinterSupply::hasAccess($user)->exists()) {
$sensor_menu[3] = [
[
'class' => 'toner',
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function create()
$this->authorize('create', User::class);

$tmp_user = new User;
$tmp_user->can_modify_passwd = LegacyAuth::get()->canUpdatePasswords(); // default to true for new users
$tmp_user->can_modify_passwd = (int) LegacyAuth::get()->canUpdatePasswords(); // default to true for new users

return view('user.create', [
'user' => $tmp_user,
Expand Down
49 changes: 28 additions & 21 deletions app/Http/Controllers/Widgets/AvailabilityMapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,47 @@ private function getDevices(Request $request)
$device_query->isNotDisabled();
}
$device_query->orderBy($settings['order_by']);
$devices = $device_query->select('devices.device_id', 'hostname', 'sysName', 'status', 'uptime', 'last_polled', 'disabled', 'disable_notify', 'location_id')->get();
$devices = $device_query->select(['devices.device_id', 'hostname', 'sysName', 'status', 'uptime', 'last_polled', 'disabled', 'disable_notify', 'location_id'])->get();

// process status
$uptime_warn = Config::get('uptime_warning', 84600);
$totals = ['warn' => 0, 'up' => 0, 'down' => 0, 'maintenance' => 0, 'ignored' => 0, 'disabled' => 0];
$data = [];

foreach ($devices as $device) {
$row = ['device' => $device];
if ($device->disabled) {
$totals['disabled']++;
$device->stateName = 'disabled';
$device->labelClass = 'blackbg';
$row['stateName'] = 'disabled';
$row['labelClass'] = 'blackbg';
} elseif ($device->disable_notify) {
$totals['ignored']++;
$device->stateName = 'alert-dis';
$device->labelClass = 'label-default';
$row['stateName'] = 'alert-dis';
$row['labelClass'] = 'label-default';
} elseif ($device->status == 1) {
if (($device->uptime < $uptime_warn) && ($device->uptime != 0)) {
$totals['warn']++;
$device->stateName = 'warn';
$device->labelClass = 'label-warning';
$row['stateName'] = 'warn';
$row['labelClass'] = 'label-warning';
} else {
$totals['up']++;
$device->stateName = 'up';
$device->labelClass = 'label-success';
$row['stateName'] = 'up';
$row['labelClass'] = 'label-success';
}
} else {
$totals['down']++;
$device->stateName = 'down';
$device->labelClass = 'label-danger';
$row['stateName'] = 'down';
$row['labelClass'] = 'label-danger';
}

if ($device->isUnderMaintenance()) {
$device->labelClass = 'label-default';
$row['labelClass'] = 'label-default';
$totals['maintenance']++;
}
$data[] = $row;
}

return [$devices, $totals];
return [$data, ['warn' => 0, 'up' => 0, 'down' => 0, 'maintenance' => 0, 'ignored' => 0, 'disabled' => 0]];
}

private function getServices($request)
Expand All @@ -155,25 +159,28 @@ private function getServices($request)
$services_query->leftJoin('devices', 'services.device_id', 'devices.device_id')->orderBy('hostname')->orderBy('service_type');
}
$services = $services_query->with(['device' => function ($query) {
$query->select('devices.device_id', 'hostname', 'sysName');
}])->select('service_id', 'services.device_id', 'service_type', 'service_desc', 'service_status')->get();
$query->select(['devices.device_id', 'hostname', 'sysName']);
}])->select(['service_id', 'services.device_id', 'service_type', 'service_desc', 'service_status'])->get();

// process status
$totals = ['warn' => 0, 'up' => 0, 'down' => 0];
$data = [];
foreach ($services as $service) {
$row = ['service' => $service];
if ($service->service_status == 0) {
$service->labelClass = 'label-success';
$service->stateName = 'up';
$row['labelClass'] = 'label-success';
$row['stateName'] = 'up';
$totals['up']++;
} elseif ($service->service_status == 1) {
$service->labelClass = 'label-warning';
$service->stateName = 'warn';
$row['labelClass'] = 'label-warning';
$row['stateName'] = 'warn';
$totals['warn']++;
} else {
$service->labelClass = 'label-danger';
$service->stateName = 'down';
$row['labelClass'] = 'label-danger';
$row['stateName'] = 'down';
$totals['down']++;
}
$data[] = $row;
}

return [$services, $totals];
Expand Down
12 changes: 7 additions & 5 deletions app/Http/Controllers/Widgets/TopDevicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ private function withDeviceQuery(Builder $query, $left_table)

/** @var Builder $query */
return $query->with(['device' => function ($query) {
$query->select('device_id', 'hostname', 'sysName', 'status', 'os');
return $query->select('device_id', 'hostname', 'sysName', 'status', 'os');
}])
->select("$left_table.device_id")
->leftJoin('devices', "$left_table.device_id", 'devices.device_id')
->groupBy("$left_table.device_id")
->where('devices.last_polled', '>', Carbon::now()->subMinutes($settings['time_interval']))
->when($settings['device_group'], function ($query) use ($settings) {
/** @var Builder<\App\Models\DeviceRelatedModel> $query */
return $query->inDeviceGroup($settings['device_group']);
$inDeviceGroup = $query->inDeviceGroup($settings['device_group']); /** @var Builder $inDeviceGroup */

return $inDeviceGroup;
});
}

Expand All @@ -151,7 +153,7 @@ private function deviceQuery()
return Device::hasAccess(Auth::user())->select('device_id', 'hostname', 'sysName', 'status', 'os')
->where('devices.last_polled', '>', Carbon::now()->subMinutes($settings['time_interval']))
->when($settings['device_group'], function ($query) use ($settings) {
$query->inDeviceGroup($settings['device_group']);
return $query->inDeviceGroup($settings['device_group']);
})
->limit($settings['device_count']);
}
Expand Down Expand Up @@ -189,9 +191,9 @@ private function getTrafficData($sort)
->groupBy('device_id')
->where('poll_time', '>', Carbon::now()->subMinutes($settings['time_interval'])->timestamp)
->when($settings['device_group'], function ($query) use ($settings) {
$query->inDeviceGroup($settings['device_group']);
return $query->inDeviceGroup($settings['device_group']);
}, function ($query) {
$query->has('device');
return $query->has('device');
})
->orderByRaw('SUM(ifInOctets_rate + ifOutOctets_rate) ' . $sort)
->limit($settings['device_count']);
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Widgets/TopErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getView(Request $request)
$query->select('device_id', 'hostname', 'sysName', 'status', 'os');
}])
->isValid()
->select('port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias')
->select(['port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias'])
->groupBy('port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias')
->where('poll_time', '>', Carbon::now()->subMinutes($data['time_interval'])->timestamp)
->where(function ($query) {
Expand All @@ -64,12 +64,12 @@ public function getView(Request $request)
})
->isUp()
->when($data['device_group'], function ($query) use ($data) {
$query->inDeviceGroup($data['device_group']);
return $query->inDeviceGroup($data['device_group']);
}, function ($query) {
$query->has('device');
return $query->has('device');
})
->when($data['port_group'], function ($query) use ($data) {
$query->inPortGroup($data['port_group']);
return $query->inPortGroup($data['port_group']);
})
->orderByRaw('SUM(LEAST(ifInErrors_rate, 9223372036854775807) + LEAST(ifOutErrors_rate, 9223372036854775807)) DESC')
->limit($data['interface_count']);
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Widgets/TopInterfacesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public function getView(Request $request)
$query->select('device_id', 'hostname', 'sysName', 'status', 'os');
}])
->isValid()
->select('port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias')
->select(['port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias'])
->groupBy('port_id', 'device_id', 'ifName', 'ifDescr', 'ifAlias')
->where('poll_time', '>', Carbon::now()->subMinutes($data['time_interval'])->timestamp)
->isUp()
->when($data['device_group'], function ($query) use ($data) {
$query->inDeviceGroup($data['device_group']);
return $query->inDeviceGroup($data['device_group']);
}, function ($query) {
$query->has('device');
return $query->has('device');
})
->when($data['port_group'], function ($query) use ($data) {
$query->inPortGroup($data['port_group']);
return $query->inPortGroup($data['port_group']);
})
->orderByRaw('SUM(LEAST(ifInOctets_rate, 9223372036854775807) + LEAST(ifOutOctets_rate, 9223372036854775807)) DESC')
->limit($data['interface_count']);
Expand Down
10 changes: 5 additions & 5 deletions database/factories/BgpPeerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function definition()
'bgpPeerRemoteAddr' => $this->faker->ipv4,
'bgpPeerRemoteAs' => $this->faker->numberBetween(1, 65535),
'bgpPeerState' => $this->faker->randomElement(['established', 'idle']),
'astext' => $this->faker->sentence(),
'astext' => $this->faker->sentence,
'bgpPeerAdminStatus' => $this->faker->randomElement(['start', 'stop']),
'bgpPeerInUpdates' => $this->faker->randomDigit,
'bgpPeerOutUpdates' => $this->faker->randomDigit,
'bgpPeerInTotalMessages' => $this->faker->randomDigit,
'bgpPeerOutTotalMessages' => $this->faker->randomDigit,
'bgpPeerInUpdates' => $this->faker->randomDigit(),
'bgpPeerOutUpdates' => $this->faker->randomDigit(),
'bgpPeerInTotalMessages' => $this->faker->randomDigit(),
'bgpPeerOutTotalMessages' => $this->faker->randomDigit(),
'bgpPeerFsmEstablishedTime' => $this->faker->unixTime,
'bgpPeerInUpdateElapsedTime' => $this->faker->unixTime,
];
Expand Down
2 changes: 1 addition & 1 deletion database/factories/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ComponentFactory extends Factory
public function definition()
{
return [
'device_id' => $this->faker->randomDigit,
'device_id' => $this->faker->randomDigit(),
'type' => $this->faker->regexify('[A-Za-z0-9]{4,20}'),
];
}
Expand Down
10 changes: 8 additions & 2 deletions database/factories/Ipv4AddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Database\Factories;

use App\Models\Ipv4Address;
use App\Models\Ipv4Network;
use App\Models\Port;
use Illuminate\Database\Eloquent\Factories\Factory;
use LibreNMS\Util\IPv4;

Expand All @@ -29,10 +31,14 @@ public function definition()
'ipv4_address' => $ip->uncompressed(),
'ipv4_prefixlen' => $prefix,
'port_id' => function () {
return \App\Models\Port::factory()->create()->port_id;
$port = Port::factory()->create(); /** @var Port $port */

return $port->port_id;
},
'ipv4_network_id' => function () use ($ip) {
return \App\Models\Ipv4Network::factory()->create(['ipv4_network' => $ip->getNetworkAddress() . '/' . $ip->cidr])->ipv4_network_id;
$ipv4 = Ipv4Network::factory()->create(['ipv4_network' => $ip->getNetworkAddress() . '/' . $ip->cidr]); /** @var Ipv4Address $ipv4 */

return $ipv4->ipv4_network_id;
},
];
}
Expand Down
6 changes: 3 additions & 3 deletions database/factories/OspfNbrFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class OspfNbrFactory extends Factory
public function definition()
{
return [
'id' => $this->faker->randomDigit,
'id' => $this->faker->randomDigit(),
'ospfNbrIpAddr' => $this->faker->ipv4,
'ospfNbrAddressLessIndex' => $this->faker->randomDigit,
'ospfNbrAddressLessIndex' => $this->faker->randomDigit(),
'ospfNbrRtrId' => $this->faker->ipv4,
'ospfNbrOptions' => 0,
'ospfNbrPriority' => 1,
'ospfNbrEvents' => $this->faker->randomDigit,
'ospfNbrEvents' => $this->faker->randomDigit(),
'ospfNbrLsRetransQLen' => 0,
'ospfNbmaNbrStatus' => 'active',
'ospfNbmaNbrPermanence' => 'dynamic',
Expand Down
6 changes: 3 additions & 3 deletions database/factories/OspfPortFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class OspfPortFactory extends Factory
public function definition()
{
return [
'id' => $this->faker->randomDigit,
'ospf_port_id' => $this->faker->randomDigit,
'id' => $this->faker->randomDigit(),
'ospf_port_id' => $this->faker->randomDigit(),
'ospfIfIpAddress' => $this->faker->ipv4,
'ospfAddressLessIf' => $this->faker->randomDigit,
'ospfAddressLessIf' => $this->faker->randomDigit(),
'ospfIfAreaId' => '0.0.0.0',
];
}
Expand Down
4 changes: 2 additions & 2 deletions database/factories/SensorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function definition()
$sensor_oid = '.1.3.6.1.4.1.4115.1.4.3.3.' . $this->faker->numberBetween(0, 10) . '.' . $this->faker->numberBetween(0, 10) . '.' . $this->faker->numberBetween(0, 10);

return [
'sensor_index' => $this->faker->randomDigit,
'sensor_index' => $this->faker->randomDigit(),
'sensor_class' => $this->faker->randomElement($sensor_class),
'sensor_current' => $this->faker->randomDigit,
'sensor_current' => $this->faker->randomDigit(),
'sensor_oid' => $sensor_oid,
];
}
Expand Down
6 changes: 3 additions & 3 deletions database/factories/VminfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function definition()
{
return [
'vm_type' => $this->faker->text(16),
'vmwVmVMID' => $this->faker->randomDigit,
'vmwVmVMID' => $this->faker->randomDigit(),
'vmwVmDisplayName' => $this->faker->domainWord . '.' . $this->faker->domainName,
'vmwVmGuestOS' => $this->faker->text(128),
'vmwVmMemSize' => $this->faker->randomDigit,
'vmwVmCpus' => $this->faker->randomDigit,
'vmwVmMemSize' => $this->faker->randomDigit(),
'vmwVmCpus' => $this->faker->randomDigit(),
'vmwVmState' => $this->faker->randomElement([PowerState::OFF, PowerState::ON, PowerState::SUSPENDED, PowerState::UNKNOWN]),
];
}
Expand Down
Loading

0 comments on commit 58ca599

Please sign in to comment.