Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Nov 16, 2023
1 parent 12f1256 commit 6b746c4
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ private function convertTo01(array $data): array
}

//missing hour in timezone offset
if (isset($data['content']['operatingsystem']) && isset($data['content']['operatingsystem']['timezone'])) {
if (isset($data['content']['operatingsystem']['timezone'])) {
$timezone = &$data['content']['operatingsystem']['timezone'];

if (preg_match('/^[+-][0-9]{2}$/', $timezone['offset'])) {
Expand Down Expand Up @@ -849,7 +849,7 @@ private function convertTo01(array $data): array
foreach ($powers as $power) {
if (isset($battery[$power])) {
$value = $this->convertBatteryPower($battery[$power]);
if ($value == false) {
if (!$value) {
unset($battery[$power]);
} else {
$battery[$power] = $value;
Expand All @@ -859,7 +859,7 @@ private function convertTo01(array $data): array

if (isset($battery['voltage'])) {
$voltage = $this->convertBatteryVoltage($battery['voltage']);
if ($voltage == false) {
if (!$voltage) {
unset($battery['voltage']);
} else {
$battery['voltage'] = $voltage;
Expand All @@ -873,7 +873,7 @@ private function convertTo01(array $data): array
foreach ($data['content']['powersupplies'] as &$psupply) {
if (isset($psupply['power_max'])) {
$value = $this->convertBatteryPower($psupply['power_max']);
if ($value == false) {
if (!$value) {
unset($psupply['power_max']);
} else {
$psupply['power_max'] = $value;
Expand Down Expand Up @@ -1122,7 +1122,7 @@ public function convertTypes(array &$data): void
}
}
}
if (isset($data['content'][$keys[0]]) && isset($data['content'][$keys[0]][$keys[1]])) {
if (isset($data['content'][$keys[0]][$keys[1]])) {
$data['content'][$keys[0]][$keys[1]] = $this->getCastedValue(
$data['content'][$keys[0]][$keys[1]],
$type
Expand All @@ -1148,7 +1148,7 @@ public function getCastedValue(string $value, string $type)
return (bool)$value;
case 'integer':
$casted = (int)$value;
if ($value == $casted && is_numeric($value)) {
if (is_numeric($value) && $value == $casted) {
return $casted;
} else {
return null;
Expand Down Expand Up @@ -1206,21 +1206,18 @@ public function convertDate(string $value, string $format = 'Y-m-d'): ?string
'd.m.Y',
'Ymd'
];
try {
while ($current = array_shift($formats)) {
$d = \DateTime::createFromFormat($current, $value);
if ($d !== false) {
break;
}
}

while ($current = array_shift($formats)) {
$d = DateTime::createFromFormat($current, $value);
if ($d !== false) {
return $d->format($format);
break;
}
return $value;
} catch (\Exception $e) {
throw $e;
}

if ($d !== false) {
return $d->format($format);
}
return $value;
}

/**
Expand Down Expand Up @@ -1483,7 +1480,7 @@ public function convertNetworkInventory(array $data): array
case 'modems':
case 'simcards':
//first, retrieve data from device
$elements = $device[$key];
$elements = $device_data;
if (!array_is_list($elements)) {
$elements = [$elements];
}
Expand Down Expand Up @@ -1527,11 +1524,9 @@ public function convertNetworkInventory(array $data): array
break;
case "cartridges":
case "pagecounters":
case "error":
$data['content'][$key] = $device[$key];
break;
case "drives":
$data['content'][$key] = $device[$key];
case "error":
$data['content'][$key] = $device_data;
break;
default:
throw new RuntimeException('Key ' . $key . ' is not handled in network devices conversion');
Expand Down Expand Up @@ -1566,7 +1561,7 @@ public function convertNetworkDiscovery(array $data): array
}
$device_info = &$data['content']['device']['info'];

if (isset($device['snmphostname']) && !empty($device['snmphostname'])) {
if (!empty($device['snmphostname'])) {
//SNMP hostname has precedence
if (isset($device['dnshostname'])) {
unset($device['dnshostname']);
Expand All @@ -1576,7 +1571,7 @@ public function convertNetworkDiscovery(array $data): array
}
}

if (isset($device['netbiosname']) && !empty($device['netbiosname']) && isset($device['dnshostname'])) {
if (!empty($device['netbiosname']) && isset($device['dnshostname'])) {
//NETBIOS name has precedence
unset($device['dnshostname']);
}
Expand All @@ -1596,7 +1591,7 @@ public function convertNetworkDiscovery(array $data): array
case 'dnshostname':
case 'snmphostname':
case 'netbiosname':
$device_info['name'] = $device[$key];
$device_info['name'] = $device_data;
unset($device[$key]);
break;
case 'entity':
Expand All @@ -1605,7 +1600,7 @@ public function convertNetworkDiscovery(array $data): array
//not used
break;
case 'ips':
$device_info['ips'] = $device[$key];
$device_info['ips'] = $device_data;
unset($device[$key]);
break;
case 'mac':
Expand All @@ -1619,22 +1614,22 @@ public function convertNetworkDiscovery(array $data): array
case 'description':
case 'serial':
case 'assettag':
$device_info[$key] = $device[$key];
$device_info[$key] = $device_data;
unset($device[$key]);
break;
case 'netportvendor':
//translate as manufacturer - if not present
if (!isset($device['manufacturer'])) {
$device_info['manufacturer'] = $device[$key];
$device_info['manufacturer'] = $device_data;
}
unset($device[$key]);
break;
case 'workgroup':
$data['content']['hardware']['workgroup'] = $device[$key];
$data['content']['hardware']['workgroup'] = $device_data;
unset($device[$key]);
break;
case 'authsnmp':
$device_info['credentials'] = $device[$key];
$device_info['credentials'] = $device_data;
unset($device[$key]);
break;
default:
Expand Down

0 comments on commit 6b746c4

Please sign in to comment.