Minix::_distro()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Minix System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI Minix OS class
9
 * @author    Mieczyslaw Nalewaj <[email protected]>
10
 * @copyright 2012 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.Minix.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Minix sysinfo class
17
 * get all the required information from Minix system
18
 *
19
 * @category  PHP
20
 * @package   PSI Minix OS class
21
 * @author    Mieczyslaw Nalewaj <[email protected]>
22
 * @copyright 2012 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class Minix extends OS
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
28
{
29
    /**
30
     * content of the syslog
31
     *
32
     * @var array
33
     */
34
    private $_dmesg = array();
35
36
    /**
37
     * call parent constructor
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * read /var/log/messages, but only if we haven't already
46
     *
47
     * @return array
48
     */
49
    protected function readdmesg()
50
    {
51
        if (count($this->_dmesg) === 0) {
52
            if (CommonFunctions::rfts('/var/log/messages', $buf)) {
53
                    $parts = preg_split("/kernel: APIC/", $buf, -1, PREG_SPLIT_NO_EMPTY);
54
//                    $parts = preg_split("/ syslogd: restart\n/", $buf, -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
                    $this->_dmesg = preg_split("/\n/", $parts[count($parts) - 1], -1, PREG_SPLIT_NO_EMPTY);
56
            }
57
        }
58
59
        return $this->_dmesg;
60
    }
61
62
    /**
63
     * get the cpu information
64
     *
65
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    protected function _cpuinfo()
68
    {
69
        if (CommonFunctions::rfts('/proc/cpuinfo', $bufr, 0, 4096, false)) {
70
            $processors = preg_split('/\s?\n\s?\n/', trim($bufr));
71
            foreach ($processors as $processor) {
72
                $_n = ""; $_f = ""; $_m = ""; $_s = "";
73
                $dev = new CpuDevice();
74
                $details = preg_split("/\n/", $processor, -1, PREG_SPLIT_NO_EMPTY);
75
                foreach ($details as $detail) {
76
                    $arrBuff = preg_split('/\s+:\s+/', trim($detail));
77
                    if (count($arrBuff) == 2) {
78
                        switch (strtolower($arrBuff[0])) {
79
                        case 'model name':
80
                            $_n = $arrBuff[1];
81
                            break;
82
                        case 'cpu mhz':
83
                            $dev->setCpuSpeed($arrBuff[1]);
84
                            break;
85
                        case 'cpu family':
86
                            $_f = $arrBuff[1];
87
                            break;
88
                        case 'model':
89
                            $_m = $arrBuff[1];
90
                            break;
91
                        case 'stepping':
92
                            $_s = $arrBuff[1];
93
                            break;
94
                        case 'flags':
95 View Code Duplication
                            if (preg_match("/ vmx/", $arrBuff[1])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                                $dev->setVirt("vmx");
97
                            } elseif (preg_match("/ svm/", $arrBuff[1])) {
98
                                $dev->setVirt("svm");
99
                            }
100
                        break;
101
                        }
102
                    }
103
                }
104
                if ($_n == "") $_n="CPU";
105
                if ($_f != "") $_n.=" Family ".$_f;
106
                if ($_m != "") $_n.=" Model ".$_m;
107
                if ($_s != "") $_n.=" Stepping ".$_s;
108
                $dev->SetModel($_n);
109
                $this->sys->setCpus($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<CpuDevice>, but the function expects a object<Cpu>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
            }
111
        } else
112
        foreach ($this->readdmesg() as $line) {
113
            if (preg_match('/kernel: (CPU .*) freq (.*) MHz/', $line, $ar_buf)) {
114
                $dev = new CpuDevice();
115
                $dev->setModel($ar_buf[1]);
116
                $dev->setCpuSpeed($ar_buf[2]);
117
                $this->sys->setCpus($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<CpuDevice>, but the function expects a object<Cpu>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
            }
119
        }
120
    }
121
122
    /**
123
     * PCI devices
124
     * get the pci device information out of dmesg
125
     *
126
     * @return void
127
     */
128
    protected function _pci()
129
    {
130
        if (CommonFunctions::rfts('/proc/pci', $strBuf, 0, 4096, false)) {
131
            $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
132 View Code Duplication
            foreach ($arrLines as $strLine) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
               $arrParams = preg_split('/\s+/', trim($strLine), 4);
134
               if (count($arrParams) == 4)
135
                  $strName = $arrParams[3];
136
               else
137
                  $strName = "unknown";
138
               $strName = preg_replace('/\(.*\)/', '', $strName);
139
               $dev = new HWDevice();
140
               $dev->setName($strName);
141
               $arrResults[] = $dev;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arrResults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arrResults = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
            }
143
            foreach ($arrResults as $dev) {
0 ignored issues
show
Bug introduced by
The variable $arrResults does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
144
                $this->sys->setPciDevices($dev);
145
            }
146
        }
147
        if (!(isset($arrResults) && is_array($arrResults)) && ($results = Parser::lspci())) {
148
            /* if access error: chmod 4755 /usr/bin/lspci */
149
            foreach ($results as $dev) {
150
                $this->sys->setPciDevices($dev);
151
            }
152
        }
153
    }
154
155
    /**
156
     * Minix Version
157
     *
158
     * @return void
159
     */
160
    private function _kernel()
161
    {
162
        foreach ($this->readdmesg() as $line) {
163
            if (preg_match('/kernel: MINIX (.*) \((.*)\)/', $line, $ar_buf)) {
164
                $branch = $ar_buf[2];
165
            }
166
        }
167
        if (CommonFunctions::executeProgram('uname', '-rvm', $ret)) {
168
            if (isset($branch))
169
               $this->sys->setKernel($ret.' ('.$branch.')');
170
            else
171
               $this->sys->setKernel($ret);
172
        }
173
    }
174
175
    /**
176
     * Distribution
177
     *
178
     * @return void
179
     */
180
    protected function _distro()
181
    {
182
        if (CommonFunctions::executeProgram('uname', '-sr', $ret))
183
            $this->sys->setDistribution($ret);
184
        else
185
            $this->sys->setDistribution('Minix');
186
187
        $this->sys->setDistributionIcon('Minix.png');
188
    }
189
190
    /**
191
     * UpTime
192
     * time the system is running
193
     *
194
     * @return void
195
     */
196
    private function _uptime()
197
    {
198
        if (CommonFunctions::executeProgram('uptime', '', $buf)) {
199
            if (preg_match("/up (\d+) days,\s*(\d+):(\d+),/", $buf, $ar_buf)) {
200
                $min = $ar_buf[3];
201
                $hours = $ar_buf[2];
202
                $days = $ar_buf[1];
203
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
0 ignored issues
show
Documentation introduced by
$days * 86400 + $hours * 3600 + $min * 60 is of type double, but the function expects a object<Interger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
            } elseif (preg_match("/up (\d+):(\d+),/", $buf, $ar_buf)) {
205
                $min = $ar_buf[2];
206
                $hours = $ar_buf[1];
207
                $this->sys->setUptime($hours * 3600 + $min * 60);
0 ignored issues
show
Documentation introduced by
$hours * 3600 + $min * 60 is of type double, but the function expects a object<Interger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
            }
209
        }
210
    }
211
212
    /**
213
     * Processor Load
214
     * optionally create a loadbar
215
     *
216
     * @return void
217
     */
218 View Code Duplication
    private function _loadavg()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
    {
220
        if (CommonFunctions::executeProgram('uptime', '', $buf)) {
221
            if (preg_match("/load averages: (.*), (.*), (.*)$/", $buf, $ar_buf)) {
222
                $this->sys->setLoad($ar_buf[1].' '.$ar_buf[2].' '.$ar_buf[3]);
223
            }
224
        }
225
    }
226
227
    /**
228
     * Virtual Host Name
229
     *
230
     * @return void
231
     */
232 View Code Duplication
    private function _hostname()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        if (PSI_USE_VHOST === true) {
235
            $this->sys->setHostname(getenv('SERVER_NAME'));
236
        } else {
237
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
238
                $ip = gethostbyname($result);
239
                if ($ip != $result) {
240
                    $this->sys->setHostname(gethostbyaddr($ip));
241
                }
242
            }
243
        }
244
    }
245
246
247
    /**
248
     *  Physical memory information and Swap Space information
249
     *
250
     *  @return void
251
     */
252
    private function _memory()
253
    {
254
        if (CommonFunctions::rfts('/proc/meminfo', $bufr, 1, 4096, false)) {
255
            $ar_buf = preg_split('/\s+/', trim($bufr));
256
            if (count($ar_buf) >= 5) {
257
                    $this->sys->setMemTotal($ar_buf[0]*$ar_buf[1]);
258
                    $this->sys->setMemFree($ar_buf[0]*$ar_buf[2]);
259
                    $this->sys->setMemCache($ar_buf[0]*$ar_buf[4]);
260
                    $this->sys->setMemUsed($ar_buf[0]*($ar_buf[1]-$ar_buf[2]));
261
            }
262
        }
263
    }
264
265
    /**
266
     * filesystem information
267
     *
268
     * @return void
269
     */
270
    private function _filesystems()
271
    {
272
        $arrResult = Parser::df("-P 2>/dev/null");
273
        foreach ($arrResult as $dev) {
274
            $this->sys->setDiskDevices($dev);
275
        }
276
    }
277
278
    /**
279
     * network information
280
     *
281
     * @return void
282
     */
283
    private function _network()
284
    {
285
        if (CommonFunctions::executeProgram('ifconfig', '-a', $bufr, PSI_DEBUG)) {
286
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
287
            foreach ($lines as $line) {
288
                if (preg_match("/^([^\s:]+):\saddress\s(\S+)\snetmask/", $line, $ar_buf)) {
289
                    $dev = new NetDevice();
290
                    $dev->setName($ar_buf[1]);
291
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
292
                            $dev->setInfo($ar_buf[2]);
293
                    }
294
                    $this->sys->setNetDevices($dev);
295
                }
296
            }
297
        }
298
    }
299
300
    /**
301
     * Processes
302
     *
303
     * @return void
304
     */
305 View Code Duplication
    protected function _processes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
    {
307
        if (CommonFunctions::executeProgram('ps', 'alx', $bufr, PSI_DEBUG)) {
308
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
309
            $processes['*'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$processes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $processes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
310
            foreach ($lines as $line) {
311
                if (preg_match("/^\s(\w)\s/", $line, $ar_buf)) {
312
                    $processes['*']++;
313
                    $state = $ar_buf[1];
314
                    if ($state == 'W') $state = 'D'; //linux format
315
                    elseif ($state == 'D') $state = 'd'; //invalid
316
                    if (isset($processes[$state])) {
317
                        $processes[$state]++;
318
                    } else {
319
                        $processes[$state] = 1;
320
                    }
321
                }
322
            }
323
            if ($processes['*'] > 0) {
324
                $this->sys->setProcesses($processes);
325
            }
326
        }
327
    }
328
329
    /**
330
     * get the information
331
     *
332
     * @return Void
333
     */
334
    public function build()
335
    {
336
        $this->error->addError("WARN", "The Minix version of phpSysInfo is a work in progress, some things currently don't work");
337
        $this->_distro();
338
        $this->_hostname();
339
        $this->_kernel();
340
        $this->_uptime();
341
        $this->_users();
342
        $this->_loadavg();
343
        $this->_pci();
344
        $this->_cpuinfo();
345
        $this->_memory();
346
        $this->_filesystems();
347
        $this->_network();
348
        $this->_processes();
349
    }
350
}
351