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.

inOpenBasedir()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 2
dl 0
loc 24
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client;
4
5
use Closure;
6
use AlibabaCloud\Client\Support\Stringy;
7
use League\CLImate\CLImate;
8
use AlibabaCloud\Client\Exception\ClientException;
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 (empty($dirs)) {
36
        return true;
37
    }
38
39
    if (inDir($filename, $dirs)) {
40
        return true;
41
    }
42
43
    if ($throwException === false) {
44
        return false;
45
    }
46
47
    throw new ClientException(
48
        'open_basedir restriction in effect. '
49
        . "File($filename) is not within the allowed path(s): ($open_basedir)",
50
        'SDK.InvalidPath'
51
    );
52
}
53
54
/**
55
 * @param string $filename
56
 * @param array  $dirs
57
 *
58
 * @return bool
59
 */
60
function inDir($filename, array $dirs)
61
{
62
    foreach ($dirs as $dir) {
63
        if (!Stringy::endsWith($dir, DIRECTORY_SEPARATOR)) {
64
            $dir .= DIRECTORY_SEPARATOR;
65
        }
66
67
        if (0 === strpos($filename, $dir)) {
68
            return true;
69
        }
70
    }
71
72
    return false;
73
}
74
75
/**
76
 * @return bool
77
 */
78
function isWindows()
79
{
80
    return PATH_SEPARATOR === ';';
81
}
82
83
/**
84
 * @return CLImate
85
 */
86
function cliMate()
87
{
88
    return new CLImate();
89
}
90
91
/**
92
 * @param string      $string
93
 * @param string|null $flank
94
 * @param string|null $char
95
 * @param int|null    $length
96
 *
97
 * @return void
98
 */
99
function backgroundRed($string, $flank = null, $char = null, $length = null)
100
{
101
    cliMate()->br();
102
    if ($flank !== null) {
103
        cliMate()->backgroundRed()->flank($flank, $char, $length);
104
        cliMate()->br();
105
    }
106
    cliMate()->backgroundRed($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 backgroundGreen($string, $flank = null, $char = null, $length = null)
119
{
120
    cliMate()->br();
121
    if ($flank !== null) {
122
        cliMate()->backgroundGreen()->flank($flank, $char, $length);
123
    }
124
    cliMate()->backgroundGreen($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 backgroundBlue($string, $flank = null, $char = null, $length = null)
137
{
138
    cliMate()->br();
139
    if ($flank !== null) {
140
        cliMate()->backgroundBlue()->flank($flank, $char, $length);
141
    }
142
    cliMate()->backgroundBlue($string);
143
    cliMate()->br();
144
}
145
146
/**
147
 * @param string      $string
148
 * @param string|null $flank
149
 * @param string|null $char
150
 * @param int|null    $length
151
 *
152
 * @return void
153
 */
154
function backgroundMagenta($string, $flank = null, $char = null, $length = null)
155
{
156
    cliMate()->br();
157
    if ($flank !== null) {
158
        cliMate()->backgroundMagenta()->flank($flank, $char, $length);
159
    }
160
    cliMate()->backgroundMagenta($string);
161
    cliMate()->br();
162
}
163
164
/**
165
 * @param array $array
166
 */
167
function json(array $array)
168
{
169
    cliMate()->br();
170
    cliMate()->backgroundGreen()->json($array);
171
    cliMate()->br();
172
}
173
174
/**
175
 * @param array $array
176
 *
177
 * @return void
178
 */
179
function redTable($array)
180
{
181
    /**
182
     * @noinspection PhpUndefinedMethodInspection
183
     */
184
    cliMate()->redTable($array);
185
}
186
187
/**
188
 * @param mixed  $result
189
 * @param string $title
190
 *
191
 * @return void
192
 */
193
function block($result, $title)
194
{
195
    cliMate()->backgroundGreen()->flank($title, '--', 20);
196
    dump($result);
197
}
198
199
/**
200
 * Gets the value of an environment variable.
201
 *
202
 * @param string $key
203
 * @param mixed  $default
204
 *
205
 * @return mixed
206
 */
207
function env($key, $default = null)
208
{
209
    $value = getenv($key);
210
211
    if ($value === false) {
212
        return value($default);
213
    }
214
215
    if (envSubstr($value)) {
216
        return substr($value, 1, -1);
217
    }
218
219
    return envConversion($value);
220
}
221
222
/**
223
 * @param $value
224
 *
225
 * @return bool|string|null
226
 */
227
function envConversion($value)
228
{
229
    $key = strtolower($value);
230
231
    if ($key === 'null' || $key === '(null)') {
232
        return null;
233
    }
234
235
    $list = [
236
        'true'    => true,
237
        '(true)'  => true,
238
        'false'   => false,
239
        '(false)' => false,
240
        'empty'   => '',
241
        '(empty)' => '',
242
    ];
243
244
    return isset($list[$key]) ? $list[$key] : $value;
245
}
246
247
/**
248
 * @param $key
249
 *
250
 * @return bool|mixed
251
 * @throws ClientException
252
 */
253
function envNotEmpty($key)
254
{
255
    $value = env($key, false);
256
    if ($value !== false && !$value) {
257
        throw new ClientException(
258
            "Environment variable '$key' cannot be empty",
259
            SDK::INVALID_ARGUMENT
260
        );
261
    }
262
    if ($value) {
263
        return $value;
264
    }
265
266
    return false;
267
}
268
269
/**
270
 * @param $value
271
 *
272
 * @return bool
273
 */
274
function envSubstr($value)
275
{
276
    return ($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"';
277
}
278
279
/**
280
 * Return the default value of the given value.
281
 *
282
 * @param mixed $value
283
 *
284
 * @return mixed
285
 */
286
function value($value)
287
{
288
    return $value instanceof Closure ? $value() : $value;
289
}
290