Environment::loadAverage()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 10
nop 0
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
/**
6
 * Class Environment. Special function to get information about working environment
7
 * @package Ffcms\Core\Helper
8
 */
9
class Environment
10
{
11
    /**
12
     * Get current operation system full name
13
     * @return string
14
     */
15
    public static function osName()
16
    {
17
        return php_uname('s');
18
    }
19
20
    /**
21
     * Get working API type (apache2handler, cgi, fcgi, etc)
22
     * @return string
23
     */
24
    public static function phpSAPI()
25
    {
26
        return php_sapi_name();
27
    }
28
29
    /**
30
     * Get current php version and small build info
31
     * @return string
32
     */
33
    public static function phpVersion()
34
    {
35
        return phpversion();
36
    }
37
38
    /**
39
     * Get load average in percents.
40
     * @return string
41
     */
42
    public static function loadAverage()
43
    {
44
        $load = 0;
45
        if (stristr(PHP_OS, 'win')) {
46
            // its not a better solution, but no other way to do this
47
            $cmd = "wmic cpu get loadpercentage /all";
48
            @exec($cmd, $output);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

48
            /** @scrutinizer ignore-unhandled */ @exec($cmd, $output);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
49
50
            // if output is exist
51
            if ($output) {
52
                // try to find line with numeric data
53
                foreach ($output as $line) {
54
                    if ($line && preg_match("/^[0-9]+\$/", $line)) {
55
                        $load = $line;
56
                        break;
57
                    }
58
                }
59
            }
60
        } else {
61
            $sys_load = sys_getloadavg(); // get linux load average (1 = 100% of 1 CPU)
62
            $load = $sys_load[0] * 100; // to percentage
63
        }
64
65
        if ((int)$load <= 0) {
66
            return 'error';
67
        }
68
69
        return (int)$load . '%';
70
    }
71
}
72