Skip to content

Commit

Permalink
refactoring: cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jul 2, 2023
1 parent 7418e55 commit 515f8d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
48 changes: 28 additions & 20 deletions src/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Common
{
/**
* Certain files, specifcally the pci/usb ids files, vary in location from
* Certain files, specifically the pci/usb ids files, vary in location from
* linux distro to linux distro. This function, when passed an array of
* possible file location, picks the first it finds and returns it. When
* none are found, it returns false.
Expand Down Expand Up @@ -63,6 +63,25 @@ public static function getLines(string $file, $default = null)
return $default;
}

/**
* Like above, but parse as ini.
*
* @return array|mixed|null
*/
public static function getIni(string $file, $default = null)
{
if (\file_exists($file) && \is_readable($file)) {
$data = @\parse_ini_file($file);
if (false === $data) {
return $default;
}

return $data;
}

return $default;
}

/**
* Prevent silly conditionals like if (in_array() || in_array() || in_array())
* Poor man's python's any() on a list comprehension kinda.
Expand All @@ -79,7 +98,7 @@ public static function parseKeyValueBlock(string $block, string $delimiter = ':'
{
$tmp = [];
foreach (\explode("\n", $block) as $line) {
if (false !== \mb_strpos($line, $delimiter)) {
if (\str_contains($line, $delimiter)) {
[$key, $value] = \explode($delimiter, $line, 2);
$tmp[\trim($key)] = \trim($value);
}
Expand Down Expand Up @@ -112,24 +131,13 @@ public static function convertHumanSizeToBytes(string $humanSize): ?float
}

$size = \substr($humanSize, 0, -1);
switch (\strtolower($lastLetter)) {
case 'b':
return (float) $size;
break;

case 'k':
return (float) $size * 1024;
break;

case 'm':
return (float) $size * 1024 * 1024;
break;

case 'g':
return (float) $size * 1024 * 1024 * 1024;
break;
}

return null;
return match (\strtolower($lastLetter)) {
'b' => (float) $size,
'k' => (float) $size * 1024,
'm' => (float) $size * 1024 * 1024,
'g' => (float) $size * 1024 * 1024 * 1024,
default => null,
};
}
}
22 changes: 8 additions & 14 deletions src/OS/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,8 @@ public function getNetwork(): ?array
$typeContents = \mb_strtoupper(Common::getContents($path.'/device/modalias', ''));
[$typeMatch] = \explode(':', $typeContents, 2);

$ueventContents = Common::getContents($path.'/uevent');
if ($ueventContents) {
$ueventContents = @\parse_ini_string($ueventContents);
}
$deviceUeventContents = Common::getContents($path.'/device/uevent');
if ($deviceUeventContents) {
$deviceUeventContents = @\parse_ini_string($deviceUeventContents);
}
$ueventContents = Common::getIni($path.'/uevent');
$deviceUeventContents = Common::getIni($path.'/device/uevent');

if ($ueventContents && isset($ueventContents['DEVTYPE'])) {
$type = \ucfirst($ueventContents['DEVTYPE']);
Expand Down Expand Up @@ -634,13 +628,13 @@ public function getOsName(): string
}
}

$lsbRelease = Common::getContents('/etc/lsb-release');
if (null !== $lsbRelease) {
return \parse_ini_string($lsbRelease)['DISTRIB_DESCRIPTION'];
$lsbRelease = Common::getIni('/etc/lsb-release');
if ($lsbRelease) {
return $lsbRelease['DISTRIB_DESCRIPTION'];
}

$suseRelease = Common::getLines('/etc/SuSE-release');
if (null !== $suseRelease) {
if ($suseRelease) {
return $suseRelease[0];
}

Expand Down Expand Up @@ -737,7 +731,7 @@ public function getModel(): ?string
}

// Don't add vendor to the mix if the name starts with it
if ($vendor && 0 !== \mb_strpos($name, $vendor)) {
if ($vendor && !\str_starts_with($name, $vendor)) {
$info[] = $vendor;
}

Expand All @@ -747,7 +741,7 @@ public function getModel(): ?string

// product name is usually bullshit, but *occasionally* it's a useful name of the computer, such as
// dell latitude e6500 or hp z260
if ($product && false === \mb_strpos($name, $product) && !\str_contains($product, 'Filled')) {
if ($product && !\str_contains($name, $product) && !\str_contains($product, 'Filled')) {
return $product.' ('.$infoStr.')';
}

Expand Down

0 comments on commit 515f8d9

Please sign in to comment.