Merge pull request #17568 from MauricioFauth/sysinfo-windows-refactor

Refactor the `Server\SysInfo\WindowsNt` class
This commit is contained in:
Maurício Meneghini Fauth 2022-05-30 13:38:25 -03:00 committed by GitHub
commit 850997a53e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 167 deletions

View File

@ -4,21 +4,21 @@ declare(strict_types=1);
namespace PhpMyAdmin\Server\SysInfo;
use COM;
use com;
use Throwable;
use function array_merge;
use function class_exists;
use function count;
use function in_array;
use function is_string;
use function trim;
use function intdiv;
use function intval;
/**
* Windows NT based SysInfo class
*/
class WindowsNt extends Base
{
/** @var COM|null */
private $wmi;
/** @var object|null */
private $wmiService = null;
/**
* The OS name
@ -32,15 +32,18 @@ class WindowsNt extends Base
*/
public function __construct()
{
if (! class_exists('COM')) {
$this->wmi = null;
if (! class_exists('com')) {
return;
}
// initialize the wmi object
$objLocator = new COM('WbemScripting.SWbemLocator');
$this->wmi = $objLocator->ConnectServer();
/**
* @see https://www.php.net/manual/en/class.com.php
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemlocator
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemservices
*
* @psalm-suppress MixedAssignment, UndefinedMagicMethod
*/
$this->wmiService = (new com('WbemScripting.SWbemLocator'))->ConnectServer();
}
/**
@ -50,15 +53,7 @@ class WindowsNt extends Base
*/
public function loadavg()
{
$sum = 0;
$buffer = $this->getWMI('Win32_Processor', ['LoadPercentage']);
foreach ($buffer as $load) {
$value = $load['LoadPercentage'];
$sum += $value;
}
return ['loadavg' => $sum / count($buffer)];
return ['loadavg' => $this->getLoadPercentage()];
}
/**
@ -66,46 +61,7 @@ class WindowsNt extends Base
*/
public function supported(): bool
{
return $this->wmi !== null;
}
/**
* Reads data from WMI
*
* @param string $strClass Class to read
* @param array $strValue Values to read
*
* @return array with results
*/
private function getWMI($strClass, array $strValue = [])
{
$arrData = [];
$objWEBM = $this->wmi->Get($strClass);
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$arrProp = $objWEBM->Properties_;
$arrWEBMCol = $objWEBM->Instances_();
foreach ($arrWEBMCol as $objItem) {
$arrInstance = [];
foreach ($arrProp as $propItem) {
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$name = $propItem->Name;
if (! empty($strValue) && ! in_array($name, $strValue)) {
continue;
}
$value = $objItem->$name;
if (is_string($value)) {
$arrInstance[$name] = trim($value);
} else {
$arrInstance[$name] = $value;
}
}
$arrData[] = $arrInstance;
}
return $arrData;
return $this->wmiService !== null;
}
/**
@ -115,33 +71,101 @@ class WindowsNt extends Base
*/
public function memory()
{
$buffer = $this->getWMI(
'Win32_OperatingSystem',
[
'TotalVisibleMemorySize',
'FreePhysicalMemory',
]
);
$mem = [];
$mem['MemTotal'] = $buffer[0]['TotalVisibleMemorySize'];
$mem['MemFree'] = $buffer[0]['FreePhysicalMemory'];
$mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'];
return array_merge($this->getSystemMemory(), $this->getPageFileUsage());
}
$buffer = $this->getWMI('Win32_PageFileUsage');
$mem['SwapTotal'] = 0;
$mem['SwapFree'] = 0;
$mem['SwapUsed'] = 0;
$mem['SwapPeak'] = 0;
foreach ($buffer as $swapdevice) {
$mem['SwapTotal'] += $swapdevice['AllocatedBaseSize'] * 1024;
$mem['SwapUsed'] += $swapdevice['CurrentUsage'] * 1024;
$mem['SwapPeak'] += $swapdevice['PeakUsage'] * 1024;
/**
* @return array<string, int>
* @psalm-return array{MemTotal: int, MemFree: int, MemUsed: int}
*/
private function getSystemMemory(): array
{
if ($this->wmiService === null) {
return ['MemTotal' => 0, 'MemFree' => 0, 'MemUsed' => 0];
}
$mem['SwapFree'] = $mem['SwapTotal'] - $mem['SwapUsed'];
/**
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
* @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
*
* @var object[] $instances
* @psalm-suppress MixedMethodCall
* @phpstan-ignore-next-line
*/
$instances = $this->wmiService->Get('Win32_OperatingSystem')->Instances_();
$totalMemory = 0;
$freeMemory = 0;
foreach ($instances as $instance) {
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$totalMemory += (int) $instance->TotalVisibleMemorySize; /* @phpstan-ignore-line */
$freeMemory += (int) $instance->FreePhysicalMemory; /* @phpstan-ignore-line */
// phpcs:enable
}
return $mem;
return ['MemTotal' => $totalMemory, 'MemFree' => $freeMemory, 'MemUsed' => $totalMemory - $freeMemory];
}
/**
* @return array<string, int>
* @psalm-return array{SwapTotal: int, SwapUsed: int, SwapPeak: int, SwapFree: int}
*/
private function getPageFileUsage(): array
{
if ($this->wmiService === null) {
return ['SwapTotal' => 0, 'SwapUsed' => 0, 'SwapPeak' => 0, 'SwapFree' => 0];
}
/**
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
* @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pagefileusage
*
* @var object[] $instances
* @psalm-suppress MixedMethodCall
* @phpstan-ignore-next-line
*/
$instances = $this->wmiService->Get('Win32_PageFileUsage')->Instances_();
$total = 0;
$used = 0;
$peak = 0;
foreach ($instances as $instance) {
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$total += intval($instance->AllocatedBaseSize) * 1024; /* @phpstan-ignore-line */
$used += intval($instance->CurrentUsage) * 1024; /* @phpstan-ignore-line */
$peak += intval($instance->PeakUsage) * 1024; /* @phpstan-ignore-line */
// phpcs:enable
}
return ['SwapTotal' => $total, 'SwapUsed' => $used, 'SwapPeak' => $peak, 'SwapFree' => $total - $used];
}
private function getLoadPercentage(): int
{
if ($this->wmiService === null) {
return 0;
}
/**
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
* @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor
*
* @var object[] $instances
* @psalm-suppress MixedMethodCall
* @phpstan-ignore-next-line
*/
$instances = $this->wmiService->Get('Win32_Processor')->Instances_();
$i = 0;
$sum = 0;
foreach ($instances as $instance) {
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$sum += (int) $instance->LoadPercentage; /* @phpstan-ignore-line */
// Can't use count($instances).
$i++;
}
try {
return intdiv($sum, $i);
} catch (Throwable $throwable) {
return 0;
}
}
}

View File

@ -7335,21 +7335,6 @@ parameters:
count: 1
path: libraries/classes/Server/SysInfo/WindowsNt.php
-
message: "#^Cannot call method Get\\(\\) on COM\\|null\\.$#"
count: 1
path: libraries/classes/Server/SysInfo/WindowsNt.php
-
message: "#^Method PhpMyAdmin\\\\Server\\\\SysInfo\\\\WindowsNt\\:\\:getWMI\\(\\) has parameter \\$strValue with no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Server/SysInfo/WindowsNt.php
-
message: "#^Method PhpMyAdmin\\\\Server\\\\SysInfo\\\\WindowsNt\\:\\:getWMI\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Server/SysInfo/WindowsNt.php
-
message: "#^Method PhpMyAdmin\\\\Server\\\\SysInfo\\\\WindowsNt\\:\\:loadavg\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1

View File

@ -2755,8 +2755,7 @@
<InvalidArgument occurrences="1">
<code>$GLOBALS['sql_query']</code>
</InvalidArgument>
<MixedArgument occurrences="12">
<code>$GLOBALS['db']</code>
<MixedArgument occurrences="11">
<code>$GLOBALS['message']</code>
<code>$GLOBALS['password'] ?? ''</code>
<code>$GLOBALS['password'] ?? null</code>
@ -2772,9 +2771,8 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$GLOBALS['queries']</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="31">
<MixedAssignment occurrences="29">
<code>$GLOBALS['_add_user_error']</code>
<code>$GLOBALS['db']</code>
<code>$GLOBALS['db_and_table']</code>
<code>$GLOBALS['dbname']</code>
<code>$GLOBALS['dbname_is_wildcard']</code>
@ -2802,7 +2800,6 @@
<code>$GLOBALS['tooltip_truename']</code>
<code>$GLOBALS['total_num_tables']</code>
<code>$GLOBALS['url_dbname']</code>
<code>$_REQUEST['db']</code>
<code>$db_name</code>
</MixedAssignment>
<MixedOperand occurrences="2">
@ -6704,7 +6701,7 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$params</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="6">
<MixedAssignment occurrences="3">
<code>$info</code>
<code>$params['single_table']</code>
<code>$subObject</code>
@ -12803,66 +12800,6 @@
<code>$params['sort_order']</code>
</MixedOperand>
</file>
<file src="libraries/classes/Server/SysInfo/WindowsNt.php">
<MixedArrayAccess occurrences="6">
<code>$buffer[0]['FreePhysicalMemory']</code>
<code>$buffer[0]['TotalVisibleMemorySize']</code>
<code>$load['LoadPercentage']</code>
<code>$swapdevice['AllocatedBaseSize']</code>
<code>$swapdevice['CurrentUsage']</code>
<code>$swapdevice['PeakUsage']</code>
</MixedArrayAccess>
<MixedArrayOffset occurrences="2">
<code>$arrInstance[$name]</code>
<code>$arrInstance[$name]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="20">
<code>$arrInstance[$name]</code>
<code>$arrProp</code>
<code>$arrWEBMCol</code>
<code>$load</code>
<code>$mem['MemFree']</code>
<code>$mem['MemTotal']</code>
<code>$mem['MemUsed']</code>
<code>$mem['SwapPeak']</code>
<code>$mem['SwapTotal']</code>
<code>$mem['SwapUsed']</code>
<code>$name</code>
<code>$objItem</code>
<code>$objLocator</code>
<code>$objWEBM</code>
<code>$propItem</code>
<code>$sum</code>
<code>$swapdevice</code>
<code>$this-&gt;wmi</code>
<code>$value</code>
<code>$value</code>
</MixedAssignment>
<MixedMethodCall occurrences="2">
<code>ConnectServer</code>
<code>Instances_</code>
</MixedMethodCall>
<MixedOperand occurrences="6">
<code>$mem['MemTotal']</code>
<code>$sum</code>
<code>$swapdevice['AllocatedBaseSize']</code>
<code>$swapdevice['CurrentUsage']</code>
<code>$swapdevice['PeakUsage']</code>
<code>$value</code>
</MixedOperand>
<MixedPropertyFetch occurrences="3">
<code>$objItem-&gt;$name</code>
<code>$objWEBM-&gt;Properties_</code>
<code>$propItem-&gt;Name</code>
</MixedPropertyFetch>
<PossiblyNullReference occurrences="1">
<code>Get</code>
</PossiblyNullReference>
<UndefinedDocblockClass occurrences="2">
<code>$this-&gt;wmi</code>
<code>COM|null</code>
</UndefinedDocblockClass>
</file>
<file src="libraries/classes/Session.php">
<MixedArgument occurrences="3">
<code>$config-&gt;getCookie('phpMyAdmin')</code>