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

JBZoo / Utils / 29770052588

20 Jul 2026 06:55PM UTC coverage: 92.619% (-0.1%) from 92.722%
29770052588

push

github

web-flow
Merge pull request #57 from JBZoo/release/8.0

8.0.0 — PHP 8.3+ floor & lock-step major

15 of 16 new or added lines in 7 files covered. (93.75%)

106 existing lines in 13 files now uncovered.

1669 of 1802 relevant lines covered (92.62%)

41.2 hits per line

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

69.74
/src/Sys.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Utils.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Utils
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Utils;
18

19
/**
20
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
21
 * @psalm-suppress UnusedClass
22
 */
23
final class Sys
24
{
25
    /**
26
     * Check is current OS Windows.
27
     */
28
    public static function isWin(): bool
29
    {
30
        return \strncasecmp(\PHP_OS_FAMILY, 'WIN', 3) === 0 || \DIRECTORY_SEPARATOR === '\\';
30✔
31
    }
32

33
    /**
34
     * Check is current user ROOT.
35
     */
36
    public static function isRoot(): bool
37
    {
38
        if (self::isFunc('posix_geteuid')) {
12✔
39
            return \posix_geteuid() === 0;
12✔
40
        }
41

UNCOV
42
        return false;
×
43
    }
44

45
    /**
46
     * Returns current linux user who runs script.
47
     */
48
    public static function getUserName(): ?string
49
    {
50
        /** @phpstan-ignore-next-line */
51
        $userInfo = (array)\posix_getpwuid(\posix_geteuid());
6✔
52

53
        /** @phpstan-ignore-next-line */
54
        return $userInfo['name'] ?? null;
6✔
55
    }
56

57
    /**
58
     * Returns a home directory of current user.
59
     * @SuppressWarnings(PHPMD.Superglobals)
60
     */
61
    public static function getHome(): ?string
62
    {
63
        /** @phpstan-ignore-next-line */
64
        $userInfo = (array)\posix_getpwuid(\posix_geteuid());
6✔
65
        if (isset($userInfo['dir'])) {
6✔
66
            return $userInfo['dir'];
6✔
67
        }
68

69
        if (\array_key_exists('HOMEDRIVE', $_SERVER)) {
×
UNCOV
70
            return $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
×
71
        }
72

UNCOV
73
        return $_SERVER['HOME'] ?? null;
×
74
    }
75

76
    /**
77
     * Alias fo ini_set function.
78
     */
79
    public static function iniSet(string $phpIniKey, string $newValue): bool
80
    {
81
        if (self::isFunc('ini_set')) {
6✔
82
            return Filter::bool(\ini_set($phpIniKey, $newValue));
6✔
83
        }
84

UNCOV
85
        return false;
×
86
    }
87

88
    /**
89
     * Alias fo ini_get function.
90
     */
91
    public static function iniGet(string $varName): string
92
    {
93
        return (string)\ini_get($varName);
72✔
94
    }
95

96
    /**
97
     * Checks if function exists and callable.
98
     */
99
    public static function isFunc(\Closure|string $funcName): bool
100
    {
101
        $disabledOnPhpIni = false;
72✔
102

103
        if (!$funcName instanceof \Closure) {
72✔
104
            $disabledOnPhpIni = \str_contains(
72✔
105
                \strtolower(self::iniGet('disable_functions')),
72✔
106
                \strtolower(\trim($funcName)),
72✔
107
            );
72✔
108
        }
109

110
        return !$disabledOnPhpIni && (\is_callable($funcName) || \function_exists($funcName));
72✔
111
    }
112

113
    /**
114
     * Set PHP execution time limit (doesn't work in safe mode).
115
     */
116
    public static function setTime(int $newLimit = 0): void
117
    {
118
        self::iniSet('set_time_limit', (string)$newLimit);
6✔
119
        self::iniSet('max_execution_time', (string)$newLimit);
6✔
120

121
        if (self::isFunc('set_time_limit')) {
6✔
122
            \set_time_limit($newLimit);
6✔
123
        }
124
    }
125

126
    /**
127
     * Set new memory limit.
128
     */
129
    public static function setMemory(string $newLimit = '256M'): void
130
    {
131
        self::iniSet('memory_limit', $newLimit);
6✔
132
    }
133

134
    /**
135
     * Compares PHP versions.
136
     */
137
    public static function isPHP(string $version, string $current = \PHP_VERSION): bool
138
    {
139
        $version = \trim($version, '.');
6✔
140

141
        return (bool)\preg_match('#^' . \preg_quote($version, '') . '#i', $current);
6✔
142
    }
143

144
    /**
145
     * Get usage memory, human-readable.
146
     */
147
    public static function getMemory(bool $isPeak = true): string
148
    {
149
        if ($isPeak) {
6✔
150
            $memory = \memory_get_peak_usage(false);
6✔
151
        } else {
152
            $memory = \memory_get_usage(false);
6✔
153
        }
154

155
        return FS::format($memory);
6✔
156
    }
157

158
    /**
159
     * Returns current document root.
160
     * @SuppressWarnings(PHPMD.Superglobals)
161
     */
162
    public static function getDocRoot(): ?string
163
    {
164
        $result = $_SERVER['DOCUMENT_ROOT'] ?? '.';
12✔
165
        $result = FS::clean($result);
12✔
166
        $result = FS::real($result);
12✔
167

168
        if (isStrEmpty($result)) {
12✔
UNCOV
169
            $result = FS::real('.');
×
170
        }
171

172
        return $result;
12✔
173
    }
174

175
    /**
176
     * Returns true when Xdebug is supported or
177
     * the runtime used is PHPDBG (PHP >= 7.0).
178
     */
179
    public static function canCollectCodeCoverage(): bool
180
    {
181
        return self::hasXdebug() || self::hasPHPDBGCodeCoverage();
6✔
182
    }
183

184
    /**
185
     * Returns the path to the binary of the current runtime.
186
     * Appends ' --php' to the path when the runtime is HHVM.
187
     * @SuppressWarnings(PHPMD.Superglobals)
188
     */
189
    public static function getBinary(): string
190
    {
191
        $customPath = Env::string('PHP_BINARY_CUSTOM');
6✔
192
        if (!isStrEmpty($customPath)) {
6✔
UNCOV
193
            return $customPath;
×
194
        }
195

196
        // HHVM
197
        if (self::isHHVM()) {
6✔
UNCOV
198
            if (($binary = \getenv('PHP_BINARY')) === false) {
×
UNCOV
199
                $binary = \PHP_BINARY;
×
200
            }
201

UNCOV
202
            return \escapeshellarg($binary) . ' --php';
×
203
        }
204

205
        if (\defined('PHP_BINARY')) {
6✔
206
            return \escapeshellarg(\PHP_BINARY);
6✔
207
        }
208

209
        $binaryLocations = [
×
210
            \PHP_BINDIR . '/php',
×
211
            \PHP_BINDIR . '/php-cli.exe',
×
UNCOV
212
            \PHP_BINDIR . '/php.exe',
×
213
        ];
×
214

215
        foreach ($binaryLocations as $binary) {
×
UNCOV
216
            if (\is_readable($binary)) {
×
UNCOV
217
                return $binary;
×
218
            }
219
        }
220

UNCOV
221
        return 'php';
×
222
    }
223

224
    /**
225
     * Return type and version of current PHP.
226
     */
227
    public static function getNameWithVersion(): string
228
    {
229
        $name    = self::getName();
6✔
230
        $version = self::getVersion();
6✔
231

232
        return \trim("{$name} {$version}");
6✔
233
    }
234

235
    /**
236
     * Returns type of PHP.
237
     */
238
    public static function getName(): string
239
    {
240
        if (self::isHHVM()) {
12✔
UNCOV
241
            return 'HHVM';
×
242
        }
243

244
        if (self::isPHPDBG()) {
12✔
UNCOV
245
            return 'PHPDBG';
×
246
        }
247

248
        return 'PHP';
12✔
249
    }
250

251
    /**
252
     * Return URL of PHP official web-site. It depends on PHP vendor.
253
     */
254
    public static function getVendorUrl(): string
255
    {
256
        if (self::isHHVM()) {
6✔
UNCOV
257
            return 'http://hhvm.com/';
×
258
        }
259

260
        return 'http://php.net/';
6✔
261
    }
262

263
    /**
264
     * Returns current PHP version.
265
     */
266
    public static function getVersion(): ?string
267
    {
268
        return \defined('PHP_VERSION') ? \PHP_VERSION : null;
12✔
269
    }
270

271
    /**
272
     * Returns true when the runtime used is PHP and Xdebug is loaded.
273
     */
274
    public static function hasXdebug(): bool
275
    {
276
        return (self::isRealPHP() || self::isHHVM()) && \extension_loaded('xdebug');
12✔
277
    }
278

279
    /**
280
     * Returns true when the runtime used is HHVM.
281
     */
282
    public static function isHHVM(): bool
283
    {
284
        return \defined('HHVM_VERSION');
48✔
285
    }
286

287
    /**
288
     * Returns true when the runtime used is PHP without the PHPDBG SAPI.
289
     */
290
    public static function isRealPHP(): bool
291
    {
292
        return !self::isHHVM() && !self::isPHPDBG();
18✔
293
    }
294

295
    /**
296
     * Returns true when the runtime used is PHP with the PHPDBG SAPI.
297
     */
298
    public static function isPHPDBG(): bool
299
    {
300
        return \PHP_SAPI === 'phpdbg' && !self::isHHVM();
30✔
301
    }
302

303
    /**
304
     * Returns true when the runtime used is PHP with the PHPDBG SAPI
305
     * and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
306
     */
307
    public static function hasPHPDBGCodeCoverage(): bool
308
    {
UNCOV
309
        return self::isPHPDBG() && \function_exists('phpdbg_start_oplog');
×
310
    }
311
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc