• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 3799187254

pending completion
3799187254

push

github

webeweb
Update CHANGELOG

48790 of 48928 relevant lines covered (99.72%)

48.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/core/Helper/OSHelper.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Core\Helper;
13

14
use WBW\Library\Core\Model\Cpu;
15
use WBW\Library\Core\Model\Memory;
16

17
/**
18
 * Operating system helper.
19
 *
20
 * @author webeweb <https://github.com/webeweb>
21
 * @package WBW\Library\Core\Helper
22
 * @deprecated since 8.61.0 use {@see WBW\Library\System\System} instead
23
 */
24
class OSHelper {
25

26
    /**
27
     * Get the CPU.
28
     *
29
     * @return Cpu|null Returns the CPU.
30
     */
31
    public static function getCpu(): ?Cpu {
32

33
        if (true === static::isWindows()) {
7✔
34
            return null;
×
35
        }
36

37
        exec("top -b -n 1 | grep '%Cpu(s):'", $output);
7✔
38

39
        preg_match_all("/[\d.]+/", $output[0], $values);
7✔
40

41
        $cpu = new Cpu();
7✔
42
        $cpu->setUs(floatval(trim($values[0][0])));
7✔
43
        $cpu->setSy(floatval(trim($values[0][1])));
7✔
44
        $cpu->setNi(floatval(trim($values[0][2])));
7✔
45
        $cpu->setId(floatval(trim($values[0][3])));
7✔
46
        $cpu->setWa(floatval(trim($values[0][4])));
7✔
47
        $cpu->setHi(floatval(trim($values[0][5])));
7✔
48
        $cpu->setSi(floatval(trim($values[0][6])));
7✔
49
        $cpu->setSt(floatval(trim($values[0][7])));
7✔
50

51
        return $cpu;
7✔
52
    }
53

54
    /**
55
     * Get the memory.
56
     *
57
     * @param bool $swap Swap ?
58
     * @return Memory|null Returns the memory.
59
     */
60
    public static function getMemory(bool $swap = false): ?Memory {
61

62
        if (true === static::isWindows()) {
14✔
63
            return null;
×
64
        }
65

66
        $row = false === $swap ? 1 : 2;
14✔
67

68
        exec("free", $output);
14✔
69

70
        $values = explode(" ", preg_replace("/\ {2,}/", " ", $output[$row]));
14✔
71

72
        $memory = new Memory();
14✔
73
        $memory->setTotal(intval(trim($values[1])));
14✔
74
        $memory->setUsed(intval(trim($values[2])));
14✔
75
        $memory->setFree(intval(trim($values[3])));
14✔
76
        if (7 === count($values)) {
14✔
77
            $memory->setShared(intval(trim($values[4])));
7✔
78
            $memory->setBuffCache(intval(trim($values[5])));
7✔
79
            $memory->setAvailable(intval(trim($values[6])));
7✔
80
        }
81

82
        return $memory;
14✔
83
    }
84

85
    /**
86
     * Determines if the operating system is Linux.
87
     *
88
     * @return bool Returns true in case of success, false otherwise.
89
     */
90
    public static function isLinux(): bool {
91
        return !static::isWindows();
7✔
92
    }
93

94
    /**
95
     * Determines if the operating system is Windows.
96
     *
97
     * @return bool Returns true in case of success, false otherwise.
98
     */
99
    public static function isWindows(): bool {
100
        return "\\" === DIRECTORY_SEPARATOR;
35✔
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc