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 (#134)
by Yong
05:39
created

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