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
Push — master ( bd815b...ed15ea )
by Yong
04:55
created

Request::removeRedundantQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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