Skip to content

Commit a2456aa

Browse files
committed
Change SysInfo's supported method to static
Enables checking if a class is supported before creating a new instance. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 850997a commit a2456aa

6 files changed

Lines changed: 13 additions & 16 deletions

File tree

libraries/classes/Server/SysInfo/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function memory()
4141
/**
4242
* Checks whether class is supported in this environment
4343
*/
44-
public function supported(): bool
44+
public static function isSupported(): bool
4545
{
4646
return true;
4747
}

libraries/classes/Server/SysInfo/Linux.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function loadavg()
6666
/**
6767
* Checks whether class is supported in this environment
6868
*/
69-
public function supported(): bool
69+
public static function isSupported(): bool
7070
{
7171
return @is_readable('/proc/meminfo') && @is_readable('/proc/stat');
7272
}

libraries/classes/Server/SysInfo/SunOs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function loadavg()
5757
/**
5858
* Checks whether class is supported in this environment
5959
*/
60-
public function supported(): bool
60+
public static function isSupported(): bool
6161
{
6262
return @is_readable('/proc/meminfo');
6363
}

libraries/classes/Server/SysInfo/SysInfo.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,20 @@ public static function get()
5252

5353
switch ($php_os) {
5454
case 'Linux':
55-
$sysInfo = new Linux();
56-
if ($sysInfo->supported()) {
57-
return $sysInfo;
55+
if (Linux::isSupported()) {
56+
return new Linux();
5857
}
5958

6059
break;
6160
case 'WINNT':
62-
$sysInfo = new WindowsNt();
63-
if ($sysInfo->supported()) {
64-
return $sysInfo;
61+
if (WindowsNt::isSupported()) {
62+
return new WindowsNt();
6563
}
6664

6765
break;
6866
case 'SunOS':
69-
$sysInfo = new SunOs();
70-
if ($sysInfo->supported()) {
71-
return $sysInfo;
67+
if (SunOs::isSupported()) {
68+
return new SunOs();
7269
}
7370

7471
break;

libraries/classes/Server/SysInfo/WindowsNt.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct()
4141
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemlocator
4242
* @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemservices
4343
*
44-
* @psalm-suppress MixedAssignment, UndefinedMagicMethod
44+
* @psalm-suppress MixedAssignment, UndefinedMagicMethod, MixedMethodCall
4545
*/
4646
$this->wmiService = (new com('WbemScripting.SWbemLocator'))->ConnectServer();
4747
}
@@ -59,9 +59,9 @@ public function loadavg()
5959
/**
6060
* Checks whether class is supported in this environment
6161
*/
62-
public function supported(): bool
62+
public static function isSupported(): bool
6363
{
64-
return $this->wmiService !== null;
64+
return class_exists('com');
6565
}
6666

6767
/**

test/classes/Server/SysInfo/SysInfoTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public function testGetSysInfo(): void
6767
*/
6868
public function testGetSysInfoSupported(): void
6969
{
70-
$this->assertTrue(SysInfo::get()->supported());
70+
$this->assertTrue(SysInfo::get()::isSupported());
7171
}
7272
}

0 commit comments

Comments
 (0)