| Conditions | 10 |
| Paths | 16 |
| Total Lines | 38 |
| Code Lines | 27 |
| 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 |
||
| 33 | public function collect(): array |
||
| 34 | { |
||
| 35 | $os = strtolower(System::getInstance()->getPlatform()); |
||
| 36 | |||
| 37 | $date = false; |
||
| 38 | |||
| 39 | $runner = Runner::getInstance(); |
||
| 40 | $file = File::getInstance(); |
||
| 41 | |||
| 42 | if ($os === 'linux') { |
||
| 43 | if ($runner->fileExists("/proc/uptime")) { |
||
| 44 | $uptime = $file->getContents("/proc/uptime"); |
||
| 45 | $uptime = explode(" ", $uptime); |
||
| 46 | $seconds = floor((int)$uptime[0]); |
||
| 47 | $bootTimestamp = time() - $seconds; |
||
| 48 | $date = date(DateTime::ATOM, (int)$bootTimestamp); |
||
| 49 | } |
||
| 50 | } elseif ($os === 'darwin') { // macOS |
||
| 51 | $output = $runner->run("sysctl -n kern.boottime")->getOutput(); |
||
| 52 | if (preg_match('/sec = (\d+)/', $output, $matches)) { |
||
| 53 | $bootTimestamp = (int)$matches[1]; |
||
| 54 | $date = date(DateTime::ATOM, $bootTimestamp); |
||
| 55 | } |
||
| 56 | } elseif ($os === 'windows') { |
||
| 57 | $output = $runner->run("net stats srv")->getOutput(); |
||
| 58 | if ($output && preg_match('/Statistik seit (.*)/i', $output, $matches)) { |
||
| 59 | $bootTimeStr = trim($matches[1]); |
||
| 60 | $bootTimestamp = strtotime($bootTimeStr); |
||
| 61 | if ($bootTimestamp !== false) { |
||
| 62 | $date = date(DateTime::ATOM, $bootTimestamp); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($date) { |
||
| 68 | return ['date' => $date]; |
||
| 69 | } else { |
||
| 70 | return []; |
||
| 71 | } |
||
| 74 |