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

JBZoo / Utils / 7753442178

28 Jan 2024 08:37AM UTC coverage: 92.758% (-0.4%) from 93.158%
7753442178

push

github

web-flow
Update PHP 8.3 (#47)

14 of 14 new or added lines in 4 files covered. (100.0%)

8 existing lines in 3 files now uncovered.

1665 of 1795 relevant lines covered (92.76%)

41.71 hits per line

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

68.42
/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
 */
22
final class Sys
23
{
24
    /**
25
     * Check is current OS Windows.
26
     */
27
    public static function isWin(): bool
28
    {
29
        return \strncasecmp(\PHP_OS_FAMILY, 'WIN', 3) === 0 || \DIRECTORY_SEPARATOR === '\\';
30✔
30
    }
31

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

41
        return false;
×
42
    }
43

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

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

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

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

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

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

84
        return false;
×
85
    }
86

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

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

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

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

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

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

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

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

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

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

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

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

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

171
        return $result;
12✔
172
    }
173

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

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

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

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

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

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

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

220
        return 'php';
×
221
    }
222

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

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

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

243
        if (self::isPHPDBG()) {
6✔
244
            return 'PHPDBG';
×
245
        }
246

247
        return 'PHP';
6✔
248
    }
249

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

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

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

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

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

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

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

302
    /**
303
     * Returns true when the runtime used is PHP with the PHPDBG SAPI
304
     * and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
305
     */
306
    public static function hasPHPDBGCodeCoverage(): bool
307
    {
308
        return self::isPHPDBG() && \function_exists('phpdbg_start_oplog');
×
309
    }
310
}
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

© 2025 Coveralls, Inc