Passed
Push — test ( 9687aa...9734ad )
by Tom
02:37
created

Lib::phpBinary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

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