Passed
Push — master ( 3f5b2e...850997 )
by Maurício
09:56 queued 12s
created

WindowsNt::getLoadPercentage()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 0
dl 0
loc 28
ccs 0
cts 2
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Server\SysInfo;
6
7
use com;
8
use Throwable;
9
10
use function array_merge;
11
use function class_exists;
12
use function intdiv;
13
use function intval;
14
15
/**
16
 * Windows NT based SysInfo class
17
 */
18
class WindowsNt extends Base
19
{
20
    /** @var object|null */
21
    private $wmiService = null;
22
23
    /**
24
     * The OS name
25
     *
26
     * @var string
27
     */
28
    public $os = 'WINNT';
29
30
    /**
31
     * Constructor to access to wmi database.
32
     */
33
    public function __construct()
34
    {
35
        if (! class_exists('com')) {
36
            return;
37
        }
38
39
        /**
40
         * @see https://www.php.net/manual/en/class.com.php
41
         * @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemlocator
42
         * @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemservices
43
         *
44
         * @psalm-suppress MixedAssignment, UndefinedMagicMethod
45
         */
46
        $this->wmiService = (new com('WbemScripting.SWbemLocator'))->ConnectServer();
47
    }
48
49
    /**
50
     * Gets load information
51
     *
52
     * @return array with load data
53
     */
54
    public function loadavg()
55
    {
56
        return ['loadavg' => $this->getLoadPercentage()];
57
    }
58
59
    /**
60
     * Checks whether class is supported in this environment
61
     */
62
    public function supported(): bool
63
    {
64
        return $this->wmiService !== null;
65
    }
66
67
    /**
68
     * Gets information about memory usage
69
     *
70
     * @return array with memory usage data
71
     */
72
    public function memory()
73
    {
74
        return array_merge($this->getSystemMemory(), $this->getPageFileUsage());
75
    }
76
77
    /**
78
     * @return array<string, int>
79
     * @psalm-return array{MemTotal: int, MemFree: int, MemUsed: int}
80
     */
81
    private function getSystemMemory(): array
82
    {
83
        if ($this->wmiService === null) {
84
            return ['MemTotal' => 0, 'MemFree' => 0, 'MemUsed' => 0];
85
        }
86
87
        /**
88
         * @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
89
         * @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
90
         *
91
         * @var object[] $instances
92
         * @psalm-suppress MixedMethodCall
93
         * @phpstan-ignore-next-line
94
         */
95
        $instances = $this->wmiService->Get('Win32_OperatingSystem')->Instances_();
96
        $totalMemory = 0;
97
        $freeMemory = 0;
98
        foreach ($instances as $instance) {
99
            // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
100
            $totalMemory += (int) $instance->TotalVisibleMemorySize; /* @phpstan-ignore-line */
101
            $freeMemory += (int) $instance->FreePhysicalMemory; /* @phpstan-ignore-line */
102
            // phpcs:enable
103
        }
104
105
        return ['MemTotal' => $totalMemory, 'MemFree' => $freeMemory, 'MemUsed' => $totalMemory - $freeMemory];
106
    }
107
108
    /**
109
     * @return array<string, int>
110
     * @psalm-return array{SwapTotal: int, SwapUsed: int, SwapPeak: int, SwapFree: int}
111
     */
112
    private function getPageFileUsage(): array
113
    {
114
        if ($this->wmiService === null) {
115
            return ['SwapTotal' => 0, 'SwapUsed' => 0, 'SwapPeak' => 0, 'SwapFree' => 0];
116
        }
117
118
        /**
119
         * @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
120
         * @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pagefileusage
121
         *
122
         * @var object[] $instances
123
         * @psalm-suppress MixedMethodCall
124
         * @phpstan-ignore-next-line
125
         */
126
        $instances = $this->wmiService->Get('Win32_PageFileUsage')->Instances_();
127
        $total = 0;
128
        $used = 0;
129
        $peak = 0;
130
        foreach ($instances as $instance) {
131
            // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
132
            $total += intval($instance->AllocatedBaseSize) * 1024; /* @phpstan-ignore-line */
133
            $used += intval($instance->CurrentUsage) * 1024; /* @phpstan-ignore-line */
134
            $peak += intval($instance->PeakUsage) * 1024; /* @phpstan-ignore-line */
135
            // phpcs:enable
136
        }
137
138
        return ['SwapTotal' => $total, 'SwapUsed' => $used, 'SwapPeak' => $peak, 'SwapFree' => $total - $used];
139
    }
140
141
    private function getLoadPercentage(): int
142
    {
143
        if ($this->wmiService === null) {
144
            return 0;
145
        }
146
147
        /**
148
         * @see https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-instances-
149
         * @see https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor
150
         *
151
         * @var object[] $instances
152
         * @psalm-suppress MixedMethodCall
153
         * @phpstan-ignore-next-line
154
         */
155
        $instances = $this->wmiService->Get('Win32_Processor')->Instances_();
156
        $i = 0;
157
        $sum = 0;
158
        foreach ($instances as $instance) {
159
            // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
160
            $sum += (int) $instance->LoadPercentage; /* @phpstan-ignore-line */
161
            // Can't use count($instances).
162
            $i++;
163
        }
164
165
        try {
166
            return intdiv($sum, $i);
167
        } catch (Throwable $throwable) {
168
            return 0;
169
        }
170
    }
171
}
172