Skip to content

Commit

Permalink
fix: avoid buff/cache output for free command
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 3, 2021
1 parent df40228 commit 50fe999
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ return PhpCsFixer\Config::create()
//'mb_str_functions' => true,
'native_function_invocation' => true,
'no_null_property_initialization' => true,
'native_constant_invocation' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'phpdoc_order' => true,
//'strict_comparison' => true,
'combine_nested_dirname' => true,
])
->setRiskyAllowed(true)
->setFinder($finder)
Expand Down
2 changes: 1 addition & 1 deletion src/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function getContents(string $file, $default = null)
public static function getLines(string $file, $default = null)
{
if (\file_exists($file) && \is_readable($file)) {
return @\file($file, FILE_SKIP_EMPTY_LINES);
return @\file($file, \FILE_SKIP_EMPTY_LINES);
}

return $default;
Expand Down
2 changes: 1 addition & 1 deletion src/Ginfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Ginfo
*/
public function __construct()
{
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
$this->os = new Windows();
} else {
// bsd, linux, darwin, solaris
Expand Down
4 changes: 2 additions & 2 deletions src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ public function getPhp(): Php
$disabledClasses = \ini_get('disable_classes');

return (new Php())
->setVersion(PHP_VERSION)
->setVersion(\PHP_VERSION)
->setExtensions(\get_loaded_extensions())
->setZendExtensions(\get_loaded_extensions(true))
->setIniFile(\php_ini_loaded_file())
->setMemoryLimit(Common::convertHumanSizeToBytes(\ini_get('memory_limit')))
->setIncludePath(\get_include_path())
->setOpenBasedir(\ini_get('open_basedir'))
->setZendThreadSafe(ZEND_THREAD_SAFE)
->setZendThreadSafe(\ZEND_THREAD_SAFE)
->setSapiName(\PHP_SAPI)
->setDisabledFunctions($disabledFunctions ? \explode(',', $disabledFunctions) : [])
->setDisabledClasses($disabledClasses ? \explode(',', $disabledClasses) : [])
Expand Down
18 changes: 9 additions & 9 deletions src/OS/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ public function getDrives(): ?array
return null;
}

if (\preg_match_all('/(\d+)\s+([a-z]{3}|nvme\d+n\d+)(p?\d+)$/m', $partitionsContents, $partitionsMatch, PREG_SET_ORDER) > 0) {
if (\preg_match_all('/(\d+)\s+([a-z]{3}|nvme\d+n\d+)(p?\d+)$/m', $partitionsContents, $partitionsMatch, \PREG_SET_ORDER) > 0) {
foreach ($partitionsMatch as $partition) {
$partitions[$partition[2]][] = (new Drive\Partition())
->setName($partition[2].$partition[3])
->setSize($partition[1] * 1024);
}
}

$paths = \glob('/sys/block/*/device/model', GLOB_NOSORT);
$paths = \glob('/sys/block/*/device/model', \GLOB_NOSORT);
if (false === $paths) {
return null;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ public function getMounts(): ?array
return null;
}

if (false === \preg_match_all('/^(\S+) (\S+) (\S+) (.+) \d \d$/m', $contents, $match, PREG_SET_ORDER)) {
if (false === \preg_match_all('/^(\S+) (\S+) (\S+) (.+) \d \d$/m', $contents, $match, \PREG_SET_ORDER)) {
return null;
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public function getSensors(): ?array
}

// Laptop backlight percentage
foreach (\glob('/sys/{devices/virtual,class}/backlight/*/max_brightness', GLOB_NOSORT | GLOB_BRACE) as $bl) {
foreach (\glob('/sys/{devices/virtual,class}/backlight/*/max_brightness', \GLOB_NOSORT | \GLOB_BRACE) as $bl) {
$max = Common::getContents($bl);
$cur = Common::getContents(\dirname($bl).'/actual_brightness');
if (null === $cur) {
Expand Down Expand Up @@ -376,7 +376,7 @@ public function getLoad(): ?array

public function getNetwork(): ?array
{
$paths = \glob('/sys/class/net/*', GLOB_NOSORT);
$paths = \glob('/sys/class/net/*', \GLOB_NOSORT);
if (false === $paths) {
return null;
}
Expand Down Expand Up @@ -456,7 +456,7 @@ public function getNetwork(): ?array

public function getBattery(): ?array
{
$paths = \glob('/sys/class/power_supply/BAT*', GLOB_NOSORT);
$paths = \glob('/sys/class/power_supply/BAT*', \GLOB_NOSORT);
if (false === $paths) {
return null;
}
Expand Down Expand Up @@ -508,7 +508,7 @@ public function getSoundCards(): ?array

public function getProcesses(): ?array
{
$processes = \glob('/proc/*/status', GLOB_NOSORT);
$processes = \glob('/proc/*/status', \GLOB_NOSORT);
if (null === $processes) {
return null;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ public function getOsName(): string
return 'Debian '.$debianVersion;
}

return PHP_OS;
return \PHP_OS;
}

public function getLoggedUsers(): ?array
Expand Down Expand Up @@ -642,7 +642,7 @@ public function getVirtualization(): ?string

// Try getting kernel modules
$modules = [];
if (\preg_match_all('/^(\S+)/m', Common::getContents('/proc/modules', ''), $matches, PREG_SET_ORDER)) {
if (\preg_match_all('/^(\S+)/m', Common::getContents('/proc/modules', ''), $matches, \PREG_SET_ORDER)) {
foreach ($matches as $match) {
$modules[] = $match[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/OS/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getOsName(): string
return $info['Caption'];
}

return PHP_OS;
return \PHP_OS;
}

public function getKernel(): string
Expand Down Expand Up @@ -162,7 +162,7 @@ public function getUptime(): ?float
}

// custom windows date format ¯\_(ツ)_/¯
[$dateTime, $operand, $modifyMinutes] = \preg_split('/([\+\-])+/', $info['LastBootUpTime'], -1, PREG_SPLIT_DELIM_CAPTURE);
[$dateTime, $operand, $modifyMinutes] = \preg_split('/([\+\-])+/', $info['LastBootUpTime'], -1, \PREG_SPLIT_DELIM_CAPTURE);
$modifyHours = ($modifyMinutes / 60 * 100);

$booted = \DateTime::createFromFormat('YmdHis.uO', $dateTime.$operand.$modifyHours, new \DateTimeZone('GMT'));
Expand Down
8 changes: 7 additions & 1 deletion src/Parsers/Free.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ public static function work(): ?array
$free = $process->getOutput();

$arr = \explode("\n", \trim($free));
$isWideOutput = false === \strpos($arr[0], 'buff/cache'); // alpine doesnt support wide output for example
\array_shift($arr); // remove header

$memStr = \trim(\explode(':', $arr[0], 2)[1]);
$swapStr = \trim(\explode(':', $arr[1], 2)[1]);

[$memTotal, $memUsed, $memFree, $memShared, $memBuffers, $memCached, $memAvailable] = \preg_split('/\s+/', $memStr);
if ($isWideOutput) {
[$memTotal, $memUsed, $memFree, $memShared, $memBuffers, $memCached, $memAvailable] = \preg_split('/\s+/', $memStr);
} else {
$memBuffers = $memCached = null;
[$memTotal, $memUsed, $memFree, $memShared, $memBuffCached, $memAvailable] = \preg_split('/\s+/', $memStr);
}
[$swapTotal, $swapUsed, $swapFree] = \preg_split('/\s+/', $swapStr);

return [
Expand Down
8 changes: 4 additions & 4 deletions src/Parsers/Hwpci.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private function __clone()
*/
private function fetchUsbIdsLinux(): void
{
foreach (\glob('/sys/bus/usb/devices/*', GLOB_NOSORT) as $path) {
foreach (\glob('/sys/bus/usb/devices/*', \GLOB_NOSORT) as $path) {
// Avoid the same device artificially appearing more than once
if (false !== \strpos($path, ':')) {
continue;
Expand All @@ -44,8 +44,8 @@ private function fetchUsbIdsLinux(): void
// First try uevent
if (\is_readable($path.'/uevent') &&
\preg_match('/^product=([^\/]+)\/([^\/]+)\/[^$]+$/m', \mb_strtolower(Common::getContents($path.'/uevent')), $match)) {
$vId = \str_pad($match[1], 4, '0', STR_PAD_LEFT);
$dId = \str_pad($match[2], 4, '0', STR_PAD_LEFT);
$vId = \str_pad($match[1], 4, '0', \STR_PAD_LEFT);
$dId = \str_pad($match[2], 4, '0', \STR_PAD_LEFT);
$this->entries[$vId][$dId] = 1;
} // And next modalias
elseif (\is_readable($path.'/modalias') &&
Expand All @@ -62,7 +62,7 @@ private function fetchUsbIdsLinux(): void
*/
private function fetchPciIdsLinux(): void
{
foreach (\glob('/sys/bus/pci/devices/*', GLOB_NOSORT) as $path) {
foreach (\glob('/sys/bus/pci/devices/*', \GLOB_NOSORT) as $path) {
// See if we can use simple vendor/device files and avoid taking time with regex
if (($fDevice = Common::getContents($path.'/device', '')) && ($fVend = Common::getContents($path.'/vendor', ''))) {
[, $vId] = \explode('x', $fVend, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/Mdadm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function work(): ?array
return null;
}

if (false === \preg_match_all('/(\S+)\s*:\s*(\w+)\s*raid(\d+)\s*([\w+\[\d+\] (\(\w\))?]+)\n\s+(\d+) blocks[^[]+\[(\d\/\d)\] \[([U\_]+)\]/mi', (string) $mdadmContents, $match, PREG_SET_ORDER)) {
if (false === \preg_match_all('/(\S+)\s*:\s*(\w+)\s*raid(\d+)\s*([\w+\[\d+\] (\(\w\))?]+)\n\s+(\d+) blocks[^[]+\[(\d\/\d)\] \[([U\_]+)\]/mi', (string) $mdadmContents, $match, \PREG_SET_ORDER)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/Sensors/Hwmon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private function __clone()

public static function work(): ?array
{
$paths = \glob('/sys/class/hwmon/hwmon*/{,device/}*_input', GLOB_NOSORT | GLOB_BRACE);
$paths = \glob('/sys/class/hwmon/hwmon*/{,device/}*_input', \GLOB_NOSORT | \GLOB_BRACE);
if (false === $paths) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/Sensors/Ipmi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function work(): ?array

$result = $process->getOutput();

if (!\preg_match_all('/^([^|]+)\| ([\d\.]+ (?:Volts|degrees [CF]))\s+\| ok$/m', $result, $matches, PREG_SET_ORDER)) {
if (!\preg_match_all('/^([^|]+)\| ([\d\.]+ (?:Volts|degrees [CF]))\s+\| ok$/m', $result, $matches, \PREG_SET_ORDER)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/Sensors/Nvidia.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function work(): ?array

$cardsList = $process->getOutput();

if (!\preg_match_all('/GPU (\d+): (.+) \(UUID:.+\)/', $cardsList, $matches, PREG_SET_ORDER)) {
if (!\preg_match_all('/GPU (\d+): (.+) \(UUID:.+\)/', $cardsList, $matches, \PREG_SET_ORDER)) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parsers/Sensors/ThermalZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private function __clone()

public static function work(): ?array
{
$paths = \glob('/sys/class/thermal/thermal_zone*', GLOB_NOSORT | GLOB_BRACE);
$paths = \glob('/sys/class/thermal/thermal_zone*', \GLOB_NOSORT | \GLOB_BRACE);
if (false === $paths) {
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/GinfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function testServices(): void
public function testSamba(): void
{
$samba = $this->info->getSamba();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($samba);
self::markTestSkipped('Not implemented for windows');
} else {
Expand All @@ -146,7 +146,7 @@ public function testSamba(): void
public function testUps(): void
{
$ups = $this->info->getUps();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($ups);
self::markTestSkipped('Not implemented for windows');
} else {
Expand All @@ -162,7 +162,7 @@ public function testUps(): void
public function testSelinux(): void
{
$selinux = $this->info->getSelinux();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($selinux);
self::markTestSkipped('Not implemented for windows');
} else {
Expand All @@ -178,7 +178,7 @@ public function testSelinux(): void
public function testBattery(): void
{
$battery = $this->info->getBattery();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($battery); //todo
self::markTestSkipped('Not implemented for windows');
} else {
Expand All @@ -191,7 +191,7 @@ public function testBattery(): void
public function testSensors(): void
{
$sensors = $this->info->getSensors();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($sensors); //todo
self::markTestSkipped('Not implemented for windows');
} else {
Expand All @@ -207,7 +207,7 @@ public function testSensors(): void
public function testPrinters(): void
{
$printers = $this->info->getPrinters();
if ('Windows' === PHP_OS_FAMILY) {
if ('Windows' === \PHP_OS_FAMILY) {
self::assertNull($printers); //todo
self::markTestSkipped('Not implemented for windows');
} else {
Expand Down

0 comments on commit 50fe999

Please sign in to comment.