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 (#73)
by Yong
08:31 queued 12s
created

Request::removeRedundantParameters()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 15.4039

Importance

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