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.
Test Failed
Pull Request — master (#64)
by Yong
06:11
created

env()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 11
nop 2
dl 0
loc 28
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlibabaCloud\Client;
4
5
use AlibabaCloud\Client\Exception\ClientException;
6
use Closure;
7
use League\CLImate\CLImate;
8
use Stringy\Stringy;
9
10
/*
11
|--------------------------------------------------------------------------
12
| Global Functions for Alibaba Cloud
13
|--------------------------------------------------------------------------
14
|
15
| Some common global functions are defined here.
16
| This file will be automatically loaded.
17
|
18
*/
19
20
/**
21
 * @param      $filename
22
 * @param bool $throwException
23
 *
24
 * @return bool
25
 * @throws ClientException
26
 */
27
function inOpenBasedir($filename, $throwException = false)
28
{
29
    $open_basedir = ini_get('open_basedir');
30
    if (!$open_basedir) {
31
        return true;
32
    }
33
34
    $dirs = explode(PATH_SEPARATOR, $open_basedir);
35
    if (!$dirs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dirs of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
        return true;
37
    }
38
39
    foreach ($dirs as $dir) {
40
        if (!Stringy::create($dir)->endsWith(DIRECTORY_SEPARATOR)) {
41
            $dir .= DIRECTORY_SEPARATOR;
42
        }
43
        if (false !== strpos($filename, $dir)) {
44
            return true;
45
        }
46
    }
47
48
    if ($throwException === false) {
49
        return false;
50
    }
51
52
    throw new ClientException('open_basedir restriction in effect. '
53
                              . "File($filename) is not within the allowed path(s): ($open_basedir)"
54
        , 'SDK.InvalidPath');
55
}
56
57
/**
58
 * @return bool
59
 */
60
function isWindows()
61
{
62
    return PATH_SEPARATOR === ';';
63
}
64
65
/**
66
 * @return CLImate
67
 */
68
function cliMate()
69
{
70
    return new CLImate();
71
}
72
73
/**
74
 * @param string      $string
75
 * @param string|null $flank
76
 * @param string|null $char
77
 * @param int|null    $length
78
 *
79
 * @return void
80
 */
81
function backgroundRed($string, $flank = null, $char = null, $length = null)
82
{
83
    cliMate()->br();
84
    if ($flank !== null) {
85
        cliMate()->backgroundRed()->flank($flank, $char, $length);
86
        cliMate()->br();
87
    }
88
    cliMate()->backgroundRed($string);
89
    cliMate()->br();
90
}
91
92
/**
93
 * @param string      $string
94
 * @param string|null $flank
95
 * @param string|null $char
96
 * @param int|null    $length
97
 *
98
 * @return void
99
 */
100
function backgroundGreen($string, $flank = null, $char = null, $length = null)
101
{
102
    cliMate()->br();
103
    if ($flank !== null) {
104
        cliMate()->backgroundGreen()->flank($flank, $char, $length);
105
    }
106
    cliMate()->backgroundGreen($string);
107
    cliMate()->br();
108
}
109
110
/**
111
 * @param string      $string
112
 * @param string|null $flank
113
 * @param string|null $char
114
 * @param int|null    $length
115
 *
116
 * @return void
117
 */
118
function backgroundBlue($string, $flank = null, $char = null, $length = null)
119
{
120
    cliMate()->br();
121
    if ($flank !== null) {
122
        cliMate()->backgroundBlue()->flank($flank, $char, $length);
123
    }
124
    cliMate()->backgroundBlue($string);
125
    cliMate()->br();
126
}
127
128
/**
129
 * @param string      $string
130
 * @param string|null $flank
131
 * @param string|null $char
132
 * @param int|null    $length
133
 *
134
 * @return void
135
 */
136
function backgroundMagenta($string, $flank = null, $char = null, $length = null)
137
{
138
    cliMate()->br();
139
    if ($flank !== null) {
140
        cliMate()->backgroundMagenta()->flank($flank, $char, $length);
141
    }
142
    cliMate()->backgroundMagenta($string);
143
    cliMate()->br();
144
}
145
146
/**
147
 * @param array $array
148
 */
149
function json(array $array)
150
{
151
    cliMate()->br();
152
    cliMate()->backgroundGreen()->json($array);
153
    cliMate()->br();
154
}
155
156
/**
157
 * @param array $array
158
 *
159
 * @return void
160
 */
161
function redTable($array)
162
{
163
    /**
164
     * @noinspection PhpUndefinedMethodInspection
165
     */
166
    cliMate()->redTable($array);
167
}
168
169
/**
170
 * @param mixed  $result
171
 * @param string $title
172
 *
173
 * @return void
174
 */
175
function block($result, $title)
176
{
177
    cliMate()->backgroundGreen()->flank($title, '--', 20);
178
    dump($result);
179
}
180
181
/**
182
 * @param array $arrays
183
 *
184
 * @return array
185
 */
186
function arrayMerge(array $arrays)
187
{
188
    $result = [];
189
    foreach ($arrays as $array) {
190
        foreach ($array as $key => $value) {
191
            if (is_int($key)) {
192
                $result[] = $value;
193
                continue;
194
            }
195
196
            if (isset($result[$key]) && is_array($result[$key])) {
197
                $result[$key] = arrayMerge(
198
                    [$result[$key], $value]
199
                );
200
                continue;
201
            }
202
203
            $result[$key] = $value;
204
        }
205
    }
206
207
    return $result;
208
}
209
210
/**
211
 * Gets the value of an environment variable.
212
 *
213
 * @param  string $key
214
 * @param  mixed  $default
215
 *
216
 * @return mixed
217
 */
218
function env($key, $default = null)
219
{
220
    $value = getenv($key);
221
222
    if ($value === false) {
223
        return value($default);
224
    }
225
226
    switch (strtolower($value)) {
227
        case 'true':
228
        case '(true)':
229
            return true;
230
        case 'false':
231
        case '(false)':
232
            return false;
233
        case 'empty':
234
        case '(empty)':
235
            return '';
236
        case 'null':
237
        case '(null)':
238
            return null;
239
    }
240
241
    if (envSubstr($value)) {
242
        return substr($value, 1, -1);
243
    }
244
245
    return $value;
246
}
247
248
/**
249
 * @param $value
250
 *
251
 * @return bool
252
 */
253
function envSubstr($value)
254
{
255
    return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"';
256
}
257
258
/**
259
 * Return the default value of the given value.
260
 *
261
 * @param  mixed $value
262
 *
263
 * @return mixed
264
 */
265
function value($value)
266
{
267
    return $value instanceof Closure ? $value() : $value;
268
}