Passed
Push — master ( b084c8...530618 )
by Tom
02:39
created

Lib::emptyCoalesce()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
class Lib
8
{
9 2
    public static function r(&$v, $d)
10
    {
11 2
        if (isset($v)) {
12 1
            return $v;
13
        }
14
15 1
        return $d;
16
    }
17
18
    /**
19
     * @param mixed $v variable reference
20
     * @param mixed $d [optional]  default value (null)
21
     * @return void
22
     */
23 2
    public static function v(&$v, $d = null)
24
    {
25 2
        if (!isset($v)) {
26 1
            $v = $d;
27
        }
28 2
    }
29
30
    /**
31
     * @return string UUID version 4
32
     */
33 1
    public static function generateUuid()
34
    {
35 1
        return sprintf(
36 1
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
37
38
            // 32 bits for "time_low"
39 1
            mt_rand(0, 0xffff),
40 1
            mt_rand(0, 0xffff),
41
42
            // 16 bits for "time_mid"
43 1
            mt_rand(0, 0xffff),
44
45
            // 16 bits for "time_hi_and_version",
46
            // four most significant bits holds version number 4
47 1
            mt_rand(0, 0x0fff) | 0x4000,
48
49
            // 16 bits, 8 bits for "clk_seq_hi_res",
50
            // 8 bits for "clk_seq_low",
51
            // two most significant bits holds zero and one for variant DCE1.1
52 1
            mt_rand(0, 0x3fff) | 0x8000,
53
54
            // 48 bits for "node"
55 1
            mt_rand(0, 0xffff),
56 1
            mt_rand(0, 0xffff),
57 1
            mt_rand(0, 0xffff)
58
        );
59
    }
60
61
    /**
62
     * @param string $command
63
     * @param array|string[] $arguments
64
     * @return string
65
     */
66 2
    public static function cmd($command, array $arguments)
67
    {
68 2
        $buffer = $command;
69
70 2
        $arguments = call_user_func_array('self::merge', $arguments);
71
72 2
        foreach ($arguments as $argument) {
73 2
            $buffer .= ' ' . self::quoteArg($argument);
74
        }
75
76 2
        return $buffer;
77
    }
78
79
    /**
80
     * quote an argument to preserve its value verbatim when used as
81
     * a utility argument in shell
82
     *
83
     * @param string $argument
84
     * @return string
85
     */
86 8
    public static function quoteArg($argument)
87
    {
88 8
        $parts = explode("'", $argument);
89
90 8
        $buffer = '';
91 8
        foreach ($parts as $index => $part) {
92 8
            $index && $buffer .= "\\'";
93 8
            $safe = preg_match('~^[a-zA-Z0-9,._+@%/-]*$~', $part);
94 8
            $buffer .= $safe ? $part : "'${part}'";
95
        }
96
97
        if ('' === $buffer) {
98
            $buffer = "''";
99
        }
100
101
        return $buffer;
102
    }
103
104
    /**
105
     * Turn multi-line string into an array of lines.
106
     *
107
     * Handles no newline at the end of buffer
108
     *
109
     * @param string $buffer
110
     * @return array|string[]
111
     */
112
    public static function lines($buffer)
113
    {
114 1
        $lines = explode("\n", $buffer);
115 1
        if ($c = count($lines) and '' === $lines[$c - 1]) {
116 1
            array_pop($lines);
117
        }
118
119 1
        return $lines;
120
    }
121
122
    /**
123
     * merge n parameters, if a scalar, turned into array, otherwise must be an array
124
     */
125
    public static function merge()
126
    {
127 4
        if (!$arrays = func_get_args()) {
128 1
            return $arrays;
129
        }
130
131 3
        foreach ($arrays as $key => $value) {
132 3
            if (!is_array($value)) {
133 3
                $arrays[$key] = (array)$value;
134
            }
135
        }
136
137 3
        return call_user_func_array('array_merge', $arrays);
138
    }
139
140
    /**
141
     * Chunk an array of strings based on maximum string length per chunk
142
     *
143
     * @param array|string[] $array
144
     * @param $maxLength
145
     * @param int $overHeadPerEntry
146
     * @return array|array[]
147
     */
148
    public static function arrayChunkByStringLength(array $array, $maxLength, $overHeadPerEntry = 0)
149
    {
150 2
        $chunks = array();
151 2
        $chunkStringLength = 0;
152 2
        $chunk = array();
153
154 2
        foreach ($array as $key => $value) {
155 2
            $entryLen = strlen($value) + $overHeadPerEntry;
156 2
            if ($chunkStringLength + $entryLen > $maxLength) {
157 2
                if (empty($chunk)) {
158 1
                    throw new \InvalidArgumentException(
159 1
                        sprintf(
160 1
                            'maximum length of %d is too little to chunk the array at %s %s (%d chunk(s) so far)',
161 1
                            $maxLength,
162 1
                            is_string($key) ? 'key' : 'index',
163 1
                            is_string($key) ? var_export($key, true) : (int)$key,
164 1
                            count($chunks)
165
                        )
166
                    );
167
                }
168 1
                $chunks[] = $chunk;
169 1
                $chunk = array();
170 1
                $chunkStringLength = 0;
171
            }
172 1
            $chunk[] = $value;
173 1
            $chunkStringLength += $entryLen;
174
        }
175
176 1
        if (!empty($chunk)) {
177 1
            $chunks[] = $chunk;
178
        }
179
180 1
        return $chunks;
181
    }
182
183
    /**
184
     * Get shell environment variables only from $_SERVER in PHP CLI
185
     *
186
     * Filter an array like $_SERVER in PHP CLI shell for environment
187
     * variables only.
188
     *
189
     * @param array $server
190
     * @return array|string[]
191
     */
192
    public static function env(array $server)
193
    {
194 1
        return array_filter(
195
            // Pipelines must not support environment variable names with '=' in them
196 1
            array_flip(preg_grep('~=~', array_keys($server)))
197
            // What PHP might have added
198
            + array(
199 1
                'PHP_SELF' => null,
200
                'SCRIPT_NAME' => null,
201
                'SCRIPT_FILENAME' => null,
202
                'PATH_TRANSLATED' => null,
203
                'DOCUMENT_ROOT' => null,
204
                'REQUEST_TIME_FLOAT' => null,
205
                'REQUEST_TIME' => null,
206
                'argv' => null,
207
                'argc' => null,
208
            )
209 1
            + $server,
210 1
            'is_string'
211
        );
212
    }
213
214
    /**
215
     * fallback for the php 5.3 version which does not have PHP_BINARY.
216
     *
217
     * @return string
218
     */
219
    public static function phpBinary()
220
    {
221 1
        return defined('PHP_BINARY') ? constant('PHP_BINARY') : PHP_BINDIR . '/php';
222
    }
223
224
    /**
225
     * Empty "coalesce" function
226
     *
227
     * @return mixed
228
     */
229
    public static function emptyCoalesce()
230
    {
231 1
        foreach (func_get_args() as $arg) {
232 1
            if (empty($arg)) {
233 1
                continue;
234
            }
235
236 1
            return $arg;
237
        }
238
239 1
        return null;
240
    }
241
}
242