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

JBZoo / Cli / 5861399187

pending completion
5861399187

push

github

web-flow
Add extra context in output (#16)

23 of 23 new or added lines in 6 files covered. (100.0%)

909 of 1091 relevant lines covered (83.32%)

139.05 hits per line

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

64.81
/src/CliHelper.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Cli.
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/Cli
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Cli;
18

19
use JBZoo\Cli\OutputMods\AbstractOutputMode;
20
use JBZoo\Utils\Env;
21
use JBZoo\Utils\Str;
22

23
use function JBZoo\Utils\int;
24
use function JBZoo\Utils\isStrEmpty;
25

26
class CliHelper
27
{
28
    private static ?AbstractOutputMode $outputMode = null;
29

30
    public static function arrayMergeRecursiveDistinct(array &$array1, array &$array2): array
31
    {
32
        $merged = $array1;
12✔
33

34
        foreach ($array2 as $key => &$value) {
12✔
35
            if (\is_array($value) && isset($merged[$key]) && \is_array($merged[$key])) {
12✔
36
                $merged[$key] = self::arrayMergeRecursiveDistinct($merged[$key], $value);
12✔
37
            } else {
38
                $merged[$key] = $value;
12✔
39
            }
40
        }
41

42
        return $merged;
12✔
43
    }
44

45
    public static function getRootPath(): string
46
    {
47
        $rootPath = \defined('JBZOO_PATH_ROOT') ? (string)JBZOO_PATH_ROOT : null;
16✔
48
        if (isStrEmpty($rootPath)) {
16✔
49
            return Env::string('JBZOO_PATH_ROOT');
4✔
50
        }
51

52
        return (string)$rootPath;
12✔
53
    }
54

55
    public static function getBinPath(): string
56
    {
57
        $binPath = \defined('JBZOO_PATH_BIN') ? (string)JBZOO_PATH_BIN : null;
16✔
58
        if (isStrEmpty($binPath)) {
16✔
59
            return Env::string('JBZOO_PATH_BIN');
4✔
60
        }
61

62
        return (string)$binPath;
12✔
63
    }
64

65
    /**
66
     * @see https://github.com/phpstan/phpstan-src/blob/f8be122188/src/Process/CpuCoreCounter.php
67
     */
68
    public static function getNumberOfCpuCores(): int
69
    {
70
        static $numberOfCpuCores = null;
8✔
71

72
        if ($numberOfCpuCores !== null) {
16✔
73
            return $numberOfCpuCores;
16✔
74
        }
75

76
        if (!\function_exists('proc_open')) {
16✔
77
            return $numberOfCpuCores = 1;
×
78
        }
79

80
        // from brianium/paratest
81
        // Linux (and potentially Windows with linux sub systems)
82
        if (\is_file('/proc/cpuinfo')) {
16✔
83
            $cpuinfo = \file_get_contents('/proc/cpuinfo');
16✔
84
            if ($cpuinfo !== false) {
16✔
85
                \preg_match_all('/^processor/m', $cpuinfo, $matches);
16✔
86

87
                return $numberOfCpuCores = \count($matches[0]);
16✔
88
            }
89
        }
90

91
        // Windows
92
        if (\DIRECTORY_SEPARATOR === '\\') {
×
93
            $process = \popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
×
94
            if (\is_resource($process)) {
×
95
                /** @phan-suppress-next-line PhanPluginUseReturnValueInternalKnown */
96
                \fgets($process);
×
97
                $cores = int(\fgets($process));
×
98
                \pclose($process);
×
99

100
                return $numberOfCpuCores = $cores;
×
101
            }
102
        }
103

104
        // *nix (Linux, BSD and Mac)
105
        $process = \popen('sysctl -n hw.ncpu', 'rb');
×
106
        if (\is_resource($process)) {
×
107
            $cores = int(\fgets($process));
×
108
            \pclose($process);
×
109

110
            return $numberOfCpuCores = $cores;
×
111
        }
112

113
        return $numberOfCpuCores = 2;
×
114
    }
115

116
    public static function renderListForHelpDescription(array $keyValues): string
117
    {
118
        $result = '';
584✔
119

120
        foreach ($keyValues as $key => $value) {
584✔
121
            $result .= "<comment>{$key}</comment> - {$value}\n";
584✔
122
        }
123

124
        return $result;
584✔
125
    }
126

127
    public static function createOrGetTraceId(): string
128
    {
129
        static $traceId = null;
78✔
130

131
        if ($traceId === null) {
156✔
132
            $traceId = Str::uuid();
156✔
133
        }
134

135
        return $traceId;
156✔
136
    }
137

138
    public static function renderExpectedValues(array $values): string
139
    {
140
        $result = '';
×
141

142
        foreach ($values as $value) {
×
143
            $result .= "\"{$value}\", ";
×
144
        }
145

146
        return \rtrim($result);
×
147
    }
148

149
    public static function setInstance(AbstractOutputMode $outputMode): void
150
    {
151
        self::$outputMode = $outputMode;
584✔
152
    }
153

154
    public static function getInstance(): AbstractOutputMode
155
    {
156
        $result = self::$outputMode;
188✔
157

158
        if ($result === null) {
188✔
159
            throw new Exception('Output mode is not defined');
×
160
        }
161

162
        return $result;
188✔
163
    }
164
}
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