GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e2875b...35ced9 )
by Anton
03:41
created

helpers.php ➔ parse_home_dir()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.3183

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 6.3183
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Support;
9
10
/**
11
 * Flatten array
12
 *
13
 * @param array $array
14
 * @return array
15
 */
16
function array_flatten(array $array)
17
{
18 16
    $flatten = [];
19
    array_walk_recursive($array, function ($value) use (&$flatten) {
20 16
        $flatten[] = $value;
21 16
    });
22 16
    return $flatten;
23
}
24
25
/**
26
 * Recursively merge two config arrays with a specific behavior:
27
 *
28
 * 1. scalar values are overridden
29
 * 2. array values are extended uniquely if all keys are numeric
30
 * 3. all other array values are merged
31
 *
32
 * @param array $original
33
 * @param array $override
34
 * @return array
35
 * @see http://stackoverflow.com/a/36366886/6812729
36
 */
37
function array_merge_alternate(array $original, array $override)
38
{
39 16
    foreach ($override as $key => $value) {
40 7
        if (isset($original[$key])) {
41 7
            if (!is_array($original[$key])) {
42 7
                if (is_numeric($key)) {
43
                    // Append scalar value
44 6
                    $original[] = $value;
45
                } else {
46
                    // Override scalar value
47 7
                    $original[$key] = $value;
48
                }
49 2
            } elseif (array_keys($original[$key]) === range(0, count($original[$key]) - 1)) {
50
                // Uniquely append to array with numeric keys
51 2
                $original[$key] = array_unique(array_merge($original[$key], $value));
52
            } else {
53
                // Merge all other arrays
54 7
                $original[$key] = array_merge_alternate($original[$key], $value);
55
            }
56
        } else {
57
            // Simply add new key/value
58 2
            $original[$key] = $value;
59
        }
60
    }
61
62 16
    return $original;
63
}
64
65
/**
66
 * Determines if the given string contains the given value.
67
 *
68
 * @param string $haystack
69
 * @param string $needle
70
 * @return bool
71
 */
72
function str_contains(string $haystack, string $needle)
73
{
74 5
    return strpos($haystack, $needle) !== false;
75
}
76
77
/**
78
 * Checks if string stars with given prefix.
79
 *
80
 * @param string $string
81
 * @param string $startString
82
 * @return bool
83
 */
84
function starts_with(string $string, string $startString)
85
{
86
    $len = strlen($startString);
87
    return (substr($string, 0, $len) === $startString);
88
}
89
90
/**
91
 * Take array of key/value and create string of it.
92
 *
93
 * This function used for create environment string.
94
 *
95
 * @param array $array
96
 *
97
 * @return string
98
 */
99
function array_to_string(array $array): string
100
{
101 1
    return implode(' ', array_map(
102
        function ($key, $value) {
103 1
            return sprintf("%s='%s'", $key, $value);
104 1
        },
105 1
        array_keys($array),
106
        $array
107
    ));
108
}
109
110
/**
111
 * Check if var is closure.
112
 *
113
 * @param $var
114
 * @return bool
115
 */
116
function is_closure($var)
117
{
118 39
    return is_object($var) && ($var instanceof \Closure);
119
}
120
121
/**
122
 * Check if all elements satisfy predicate.
123
 *
124
 * @param array $array
125
 * @param \Closure $predicate
126
 * @return bool
127
 */
128
function array_all(array $array, $predicate)
129
{
130 13
    foreach ($array as $key => $value) {
131 13
        if (!$predicate($value, $key)) {
132 1
            return false;
133
        }
134
    }
135 13
    return true;
136
}
137
138
/**
139
 * Cleanup CRLF new line endings.
140
 * Issue #2111
141
 *
142
 * @param $string
143
 * @return string
144
 */
145
function normalize_line_endings($string)
146
{
147 33
    return str_replace(["\r\n", "\r"], "\n", $string);
148
}
149
150
/**
151
 * Expand leading tilde (~) symbol in given path.
152
 *
153
 * @param string $path
154
 * @return string
155
 */
156
function parse_home_dir(string $path): string
157
{
158 3
    if ('~' === $path || 0 === strpos($path, '~/')) {
159 3
        if (isset($_SERVER['HOME'])) {
160 3
            $home = $_SERVER['HOME'];
161
        } elseif (isset($_SERVER['HOMEDRIVE'], $_SERVER['HOMEPATH'])) {
162
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
163
        } else {
164
            return $path;
165
        }
166
167 3
        return $home . substr($path, 1);
168
    }
169
170 1
    return $path;
171
}
172