From bb974b24f60e8f8644e80e88e98ef28f25cffb13 Mon Sep 17 00:00:00 2001 From: Shift Date: Mon, 24 Oct 2022 17:28:37 +0000 Subject: [PATCH] Convert `optional()` to nullsafe operator --- LibreNMS/Modules/Os.php | 6 +++--- LibreNMS/Util/Url.php | 2 +- app/Http/Controllers/Table/PortsController.php | 2 +- app/Models/Device.php | 2 +- app/Models/Port.php | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/LibreNMS/Modules/Os.php b/LibreNMS/Modules/Os.php index 3b49e931dc..d911455ca5 100644 --- a/LibreNMS/Modules/Os.php +++ b/LibreNMS/Modules/Os.php @@ -92,7 +92,7 @@ class Os implements Module if (! empty($location)) { // legacy support, remove when no longer needed $deviceModel->setLocation($location); - optional($deviceModel->location)->save(); + $deviceModel->location?->save(); } } @@ -127,7 +127,7 @@ class Os implements Module $device->icon = basename(Url::findOsImage($device->os, $device->features, null, 'images/os/')); - echo trans('device.attributes.location') . ': ' . optional($device->location)->display() . PHP_EOL; + echo trans('device.attributes.location') . ': ' . $device->location?->display() . PHP_EOL; foreach (['hardware', 'version', 'features', 'serial'] as $attribute) { if (isset($device->$attribute)) { $device->$attribute = trim($device->$attribute); @@ -143,7 +143,7 @@ class Os implements Module $device = $os->getDevice(); $new_location = $device->override_sysLocation ? new Location() : $os->fetchLocation(); // fetch location data from device $device->setLocation($new_location, true); // set location and lookup coordinates if needed - optional($device->location)->save(); + $device->location?->save(); } private function sysContact(\LibreNMS\OS $os): void diff --git a/LibreNMS/Util/Url.php b/LibreNMS/Util/Url.php index 70c45c3b18..3134a31e4e 100644 --- a/LibreNMS/Util/Url.php +++ b/LibreNMS/Util/Url.php @@ -141,7 +141,7 @@ class Url $text = $label; } - $content = '
' . addslashes(htmlentities(optional($port->device)->displayName() . ' - ' . $label)) . '
'; + $content = '
' . addslashes(htmlentities($port->device?->displayName() . ' - ' . $label)) . '
'; if ($description = $port->getDescription()) { $content .= addslashes(htmlentities($description)) . '
'; } diff --git a/app/Http/Controllers/Table/PortsController.php b/app/Http/Controllers/Table/PortsController.php index d9732e2f53..2c33c7b6cb 100644 --- a/app/Http/Controllers/Table/PortsController.php +++ b/app/Http/Controllers/Table/PortsController.php @@ -160,7 +160,7 @@ class PortsController extends TableController 'status' => $status, 'device' => Url::deviceLink($port->device), 'port' => Url::portLink($port), - 'secondsIfLastChange' => ceil(optional($port->device)->uptime - ($port->ifLastChange / 100)), + 'secondsIfLastChange' => ceil($port->device?->uptime - ($port->ifLastChange / 100)), 'ifConnectorPresent' => ($port->ifConnectorPresent == 'true') ? 'yes' : 'no', 'ifSpeed' => $port->ifSpeed, 'ifMtu' => $port->ifMtu, diff --git a/app/Models/Device.php b/app/Models/Device.php index e03d36c21f..71c4e6c4a7 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -410,7 +410,7 @@ class Device extends BaseModel return; } - if (! $this->relationLoaded('location') || optional($this->location)->location !== $new_location->location) { + if (! $this->relationLoaded('location') || $this->location?->location !== $new_location->location) { if (! $new_location->exists) { // don't fetch if new location persisted to the DB, just use it $new_location = Location::firstOrCreate(['location' => $new_location->location], $coord); } diff --git a/app/Models/Port.php b/app/Models/Port.php index e83c0810db..f1e735471f 100644 --- a/app/Models/Port.php +++ b/app/Models/Port.php @@ -49,7 +49,7 @@ class Port extends DeviceRelatedModel DB::table('links')->where('local_port_id', $port->port_id)->orWhere('remote_port_id', $port->port_id)->delete(); DB::table('ports_stack')->where('port_id_low', $port->port_id)->orWhere('port_id_high', $port->port_id)->delete(); - \Rrd::purge(optional($port->device)->hostname, \Rrd::portName($port->port_id)); // purge all port rrd files + \Rrd::purge($port->device?->hostname, \Rrd::portName($port->port_id)); // purge all port rrd files }); } @@ -62,7 +62,7 @@ class Port extends DeviceRelatedModel */ public function getLabel() { - $os = optional($this->device)->os; + $os = $this->device?->os; if (\LibreNMS\Config::getOsSetting($os, 'ifname')) { $label = $this->ifName;