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.
Passed
Pull Request — master (#73)
by Yong
08:31
created

Request::removeRedundantParameters()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 6
nc 8
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
6
use AlibabaCloud\Client\Exception\ClientException;
7
use AlibabaCloud\Client\Exception\ServerException;
8
use AlibabaCloud\Client\Filter;
9
use AlibabaCloud\Client\Http\GuzzleTrait;
10
use AlibabaCloud\Client\Request\Traits\AcsTrait;
11
use AlibabaCloud\Client\Request\Traits\ClientTrait;
12
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
13
use AlibabaCloud\Client\Request\Traits\MagicTrait;
14
use AlibabaCloud\Client\Result\Result;
15
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
16
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
17
use GuzzleHttp\Client;
18
use GuzzleHttp\Exception\GuzzleException;
19
use GuzzleHttp\Psr7\Uri;
20
21
/**
22
 * Class Request
23
 *
24
 * @package   AlibabaCloud\Client\Request
25
 *
26
 * @method string resolveParameters($credential)
27
 */
28
abstract class Request implements \ArrayAccess
29
{
30
    use DeprecatedTrait;
31
    use GuzzleTrait;
32
    use MagicTrait;
33
    use ClientTrait;
34
    use AcsTrait;
35
    use ArrayAccessTrait;
36
    use ObjectAccessTrait;
37
38
    /**
39
     * @var string
40
     */
41
    public $scheme = 'http';
42
43
    /**
44
     * @var string
45
     */
46
    public $method = 'GET';
47
48
    /**
49
     * @var string
50
     */
51
    public $format = 'JSON';
52
53
    /**
54
     * @var string
55
     */
56
    public $client;
57
58
    /**
59
     * @var Uri
60
     */
61
    public $uri;
62
63
    /**
64
     * @var Client
65
     */
66
    public $guzzle;
67
68
    /**
69
     * @var array The original parameters of the request.
70
     */
71
    public $data = [];
72
73
    /**
74
     * @var string
75
     */
76
    protected $stringToBeSigned = '';
77
78
    /**
79
     * @var array
80
     */
81
    private $userAgent = [];
82
83
    /**
84
     * Request constructor.
85
     *
86
     * @param array $options
87
     *
88
     * @throws ClientException
89
     */
90 200
    public function __construct(array $options = [])
91
    {
92 200
        $this->client                     = CredentialsProvider::getDefaultName();
93 200
        $this->uri                        = new Uri();
94 200
        $this->uri                        = $this->uri->withScheme($this->scheme);
95 200
        $this->guzzle                     = new Client();
96 200
        $this->options['http_errors']     = false;
97 200
        $this->options['timeout']         = ALIBABA_CLOUD_TIMEOUT;
98 200
        $this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT;
99 200
        if ($options !== []) {
100 1
            $this->options($options);
101 1
        }
102
103 200
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
104 1
            $this->options['debug'] = true;
105 1
        }
106 200
    }
107
108
    /**
109
     * @param string $name
110
     * @param string $value
111
     *
112
     * @return $this
113
     * @throws ClientException
114
     */
115 5
    public function appendUserAgent($name, $value)
116
    {
117 5
        Filter::name($name);
118
119 3
        Filter::value($value);
120
121 1
        if (!UserAgent::isGuarded($name)) {
122 1
            $this->userAgent[$name] = $value;
123 1
        }
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param array $userAgent
130
     *
131
     * @return $this
132
     */
133 2
    public function withUserAgent(array $userAgent)
134
    {
135 2
        $this->userAgent = UserAgent::clean($userAgent);
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * Set the response data format.
142
     *
143
     * @param string $format
144
     *
145
     * @return $this
146
     * @throws ClientException
147
     */
148 16
    public function format($format)
149
    {
150 16
        Filter::format($format);
151
152 12
        $this->format = \strtoupper($format);
153
154 12
        return $this;
155
    }
156
157
    /**
158
     * Set the request body.
159
     *
160
     * @param string $body
161
     *
162
     * @return $this
163
     * @throws ClientException
164
     */
165 7
    public function body($body)
166
    {
167 7
        Filter::body($body);
168
169 5
        $this->options['body'] = $body;
170
171 5
        return $this;
172
    }
173
174
    /**
175
     * Set the json as body.
176
     *
177
     * @param array|object $content
178
     *
179
     * @return $this
180
     * @throws ClientException
181
     */
182 3
    public function jsonBody($content)
183
    {
184 3
        if (!\is_array($content) && !\is_object($content)) {
185 1
            throw new ClientException(
186 1
                'jsonBody only accepts an array or object',
187
                \ALIBABA_CLOUD_INVALID_ARGUMENT
188 1
            );
189
        }
190
191 2
        return $this->body(\json_encode($content));
192
    }
193
194
    /**
195
     * Set the request scheme.
196
     *
197
     * @param string $scheme
198
     *
199
     * @return $this
200
     * @throws ClientException
201
     */
202 28
    public function scheme($scheme)
203
    {
204 28
        Filter::scheme($scheme);
205
206 26
        $this->scheme = \strtolower($scheme);
207 26
        $this->uri    = $this->uri->withScheme($this->scheme);
208
209 26
        return $this;
210
    }
211
212
    /**
213
     * Set the request host.
214
     *
215
     * @param string $host
216
     *
217
     * @return $this
218
     * @throws ClientException
219
     */
220 31
    public function host($host)
221
    {
222 31
        Filter::host($host);
223
224 29
        $this->uri = $this->uri->withHost($host);
225
226 29
        return $this;
227
    }
228
229
    /**
230
     * @param string $method
231
     *
232
     * @return $this
233
     * @throws ClientException
234
     */
235 73
    public function method($method)
236
    {
237 73
        $this->method = Filter::method($method);
238
239 71
        return $this;
240
    }
241
242
    /**
243
     * @param string $clientName
244
     *
245
     * @return $this
246
     * @throws ClientException
247
     */
248 85
    public function client($clientName)
249
    {
250 85
        Filter::clientName($clientName);
251
252 83
        $this->client = $clientName;
253
254 83
        return $this;
255
    }
256
257
    /**
258
     * @return bool
259
     * @throws ClientException
260
     */
261 1
    public function isDebug()
262
    {
263 1
        if (isset($this->options['debug'])) {
264 1
            return $this->options['debug'] === true;
265
        }
266
267 1
        if (isset($this->httpClient()->options['debug'])) {
268 1
            return $this->httpClient()->options['debug'] === true;
269
        }
270
271 1
        return false;
272
    }
273
274
    /**
275
     * @return Result
276
     * @throws ClientException
277
     * @throws ServerException
278
     */
279 74
    public function request()
280
    {
281 74
        $this->removeRedundantParameters();
282
283 74
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
284
285 74
        $this->resolveUri();
286
287 71
        $this->resolveParameters($this->credential());
288
289 54
        if (isset($this->options['form_params'])) {
290 31
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
291 31
                self::getPostHttpBody($this->options['form_params'])
292 31
            );
293 31
        }
294
295 54
        $this->mergeOptionsIntoClient();
296
297 54
        $result = new Result($this->response(), $this);
298
299 47
        if (!$result->isSuccess()) {
300 26
            throw new ServerException($result);
301
        }
302
303 21
        return $result;
304
    }
305
306
    /**
307
     * Remove redundant parameters
308
     *
309
     * @codeCoverageIgnore
310
     */
311
    private function removeRedundantParameters()
312
    {
313
        if (isset($this->options['query']) && $this->options['query'] === []) {
314
            unset($this->options['query']);
315
        }
316
317
        if (isset($this->options['headers']) && $this->options['headers'] === []) {
318
            unset($this->options['headers']);
319
        }
320
321
        if (isset($this->options['form_params']) && $this->options['form_params'] === []) {
322
            unset($this->options['form_params']);
323
        }
324
    }
325
326
    /**
327
     * @param array $post
328
     *
329
     * @return bool|string
330
     */
331 34
    public static function getPostHttpBody(array $post)
332
    {
333 34
        $content = '';
334 34
        foreach ($post as $apiKey => $apiValue) {
335 34
            $content .= "$apiKey=" . urlencode($apiValue) . '&';
336 34
        }
337
338 34
        return substr($content, 0, -1);
339
    }
340
341
    /**
342
     * @throws ClientException
343
     */
344 54
    private function response()
345
    {
346
        try {
347 54
            return $this->guzzle->request(
348 54
                $this->method,
349 54
                (string)$this->uri,
350 54
                $this->options
351 54
            );
352 7
        } catch (GuzzleException $e) {
353 7
            throw new ClientException(
354 7
                $e->getMessage(),
355 7
                \ALIBABA_CLOUD_SERVER_UNREACHABLE,
356
                $e
357 7
            );
358
        }
359
    }
360
361
    /**
362
     * @return string
363
     */
364 26
    public function stringToBeSigned()
365
    {
366 26
        return $this->stringToBeSigned;
367
    }
368
}
369