| Conditions | 9 |
| Paths | 7 |
| Total Lines | 54 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php require($_SERVER['DOCUMENT_ROOT'] . "/static/config.inc.php"); ?> |
||
| 72 | function getServerLoad() |
||
| 73 | { |
||
| 74 | $load = null; |
||
| 75 | |||
| 76 | if (stristr(PHP_OS, "win")) |
||
| 77 | { |
||
| 78 | $cmd = "wmic cpu get loadpercentage /all"; |
||
| 79 | @exec($cmd, $output); |
||
| 80 | |||
| 81 | if ($output) |
||
| 82 | { |
||
| 83 | foreach ($output as $line) |
||
| 84 | { |
||
| 85 | if ($line && preg_match("/^[0-9]+\$/", $line)) |
||
| 86 | { |
||
| 87 | $load = $line; |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | else |
||
| 94 | { |
||
| 95 | if (is_readable("/proc/stat")) |
||
| 96 | { |
||
| 97 | // Collect 2 samples - each with 1 second period |
||
| 98 | // See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen |
||
| 99 | $statData1 = _getServerLoadLinuxData(); |
||
| 100 | sleep(1); |
||
| 101 | $statData2 = _getServerLoadLinuxData(); |
||
| 102 | |||
| 103 | if |
||
| 104 | ( |
||
| 105 | (!is_null($statData1)) && |
||
| 106 | (!is_null($statData2)) |
||
| 107 | ) |
||
| 108 | { |
||
| 109 | // Get difference |
||
| 110 | $statData2[0] -= $statData1[0]; |
||
| 111 | $statData2[1] -= $statData1[1]; |
||
| 112 | $statData2[2] -= $statData1[2]; |
||
| 113 | $statData2[3] -= $statData1[3]; |
||
| 114 | |||
| 115 | // Sum up the 4 values for User, Nice, System and Idle and calculate |
||
| 116 | // the percentage of idle time (which is part of the 4 values!) |
||
| 117 | $cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3]; |
||
| 118 | |||
| 119 | // Invert percentage to get CPU time, not idle time |
||
| 120 | $load = 100 - ($statData2[3] * 100 / $cpuTime); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return $load; |
||
| 126 | } |
||
| 161 | </html> |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.