Passed
Push — master ( bba5a4...181b61 )
by Tom
03:44
created

Lib::fsFileLookUp()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 2
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
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 2
    public static function cmd($command, array $arguments)
64
    {
65 2
        $buffer = $command;
66
67 2
        $arguments = call_user_func_array('self::merge', $arguments);
68
69 2
        foreach ($arguments as $argument) {
70 2
            $buffer .= ' ' . self::quoteArg($argument);
71
        }
72
73 2
        return $buffer;
74
    }
75
76
    /**
77
     * quote an argument to preserve its value verbatim when used as
78
     * a utility argument in shell
79
     *
80
     * @param string $argument
81
     * @return string
82
     */
83 8
    public static function quoteArg($argument)
84
    {
85 8
        $parts = explode("'", $argument);
86
87 8
        $buffer = '';
88 8
        foreach ($parts as $index => $part) {
89 8
            $index && $buffer .= "\\'";
90 8
            $safe = preg_match('~^[a-zA-Z0-9,._+@%/-]*$~', $part);
91 8
            $buffer .= $safe ? $part : "'${part}'";
92
        }
93
94
        if ('' === $buffer) {
95
            $buffer = "''";
96
        }
97
98
        return $buffer;
99
    }
100
101
    /**
102
     * Turn multi-line string into an array of lines.
103
     *
104
     * Handles no newline at the end of buffer
105
     *
106
     * @param string $buffer
107
     * @return array|string[]
108
     */
109
    public static function lines($buffer)
110
    {
111 1
        $lines = explode("\n", $buffer);
112 1
        if ($c = count($lines) and '' === $lines[$c - 1]) {
113 1
            array_pop($lines);
114
        }
115
116 1
        return $lines;
117
    }
118
119
    /**
120
     * merge n parameters, if a scalar, turned into array, otherwise must be an array
121
     */
122
    public static function merge()
123
    {
124 4
        if (!$arrays = func_get_args()) {
125 1
            return $arrays;
126
        }
127
128 3
        foreach ($arrays as $key => $value) {
129 3
            if (!is_array($value)) {
130 3
                $arrays[$key] = (array)$value;
131
            }
132
        }
133
134 3
        return call_user_func_array('array_merge', $arrays);
135
    }
136
137
    /**
138
     * Chunk an array of strings based on maximum string length per chunk
139
     *
140
     * @param array|string[] $array
141
     * @param $maxLength
142
     * @param int $overHeadPerEntry
143
     * @return array|array[]
144
     */
145
    public static function arrayChunkByStringLength(array $array, $maxLength, $overHeadPerEntry = 0)
146
    {
147 2
        $chunks = array();
148 2
        $chunkStringLength = 0;
149 2
        $chunk = array();
150
151 2
        foreach ($array as $key => $value) {
152 2
            $entryLen = strlen($value) + $overHeadPerEntry;
153 2
            if ($chunkStringLength + $entryLen > $maxLength) {
154 2
                if (empty($chunk)) {
155 1
                    throw new \InvalidArgumentException(
156 1
                        sprintf(
157 1
                            'maximum length of %d is too little to chunk the array at %s %s (%d chunk(s) so far)',
158 1
                            $maxLength,
159 1
                            is_string($key) ? 'key' : 'index',
160 1
                            is_string($key) ? var_export($key, true) : (int)$key,
161 1
                            count($chunks)
162
                        )
163
                    );
164
                }
165 1
                $chunks[] = $chunk;
166 1
                $chunk = array();
167 1
                $chunkStringLength = 0;
168
            }
169 1
            $chunk[] = $value;
170 1
            $chunkStringLength += $entryLen;
171
        }
172
173 1
        if (!empty($chunk)) {
174 1
            $chunks[] = $chunk;
175
        }
176
177 1
        return $chunks;
178
    }
179
180
    /**
181
     * Get shell environment variables only from $_SERVER in PHP CLI
182
     *
183
     * Filter an array like $_SERVER in PHP CLI shell for environment
184
     * variables only.
185
     *
186
     * @param array $server
187
     * @return array|string[]
188
     */
189
    public static function env(array $server)
190
    {
191 1
        return array_filter(
192
            // Pipelines must not support environment variable names with '=' in them
193 1
            array_flip(preg_grep('~=~', array_keys($server)))
194
            // What PHP might have added
195
            + array(
196 1
                'PHP_SELF' => null,
197
                'SCRIPT_NAME' => null,
198
                'SCRIPT_FILENAME' => null,
199
                'PATH_TRANSLATED' => null,
200
                'DOCUMENT_ROOT' => null,
201
                'REQUEST_TIME_FLOAT' => null,
202
                'REQUEST_TIME' => null,
203
                'argv' => null,
204
                'argc' => null,
205
            )
206 1
            + $server,
207 1
            'is_string'
208
        );
209
    }
210
211
    /**
212
     * fallback for the php 5.3 version which does not have PHP_BINARY.
213
     *
214
     * @return string
215
     */
216
    public static function phpBinary()
217
    {
218 1
        return defined('PHP_BINARY') ? constant('PHP_BINARY') : PHP_BINDIR . '/php';
219
    }
220
}
221