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 (#227)
by Yong
03:55
created

RpcRequest::repositionParameters()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 12
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 12
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
6
use AlibabaCloud\Client\Credentials\StsCredential;
7
use AlibabaCloud\Client\Exception\ClientException;
8
use AlibabaCloud\Client\Exception\ServerException;
9
use AlibabaCloud\Client\Support\Arrays;
10
use AlibabaCloud\Client\Support\Sign;
11
use Exception;
12
use RuntimeException;
13
14
/**
15
 * RESTful RPC Request.
16
 *
17
 * @package   AlibabaCloud\Client\Request
18
 */
19
class RpcRequest extends Request
20
{
21
22
    /**
23
     * @var string
24
     */
25
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
26
27
    /**
28
     * Resolve request parameter.
29
     *
30
     * @throws ClientException
31
     */
32 61
    public function resolveParameter()
33
    {
34 61
        $this->resolveBoolInParameters();
35 61
        $this->resolveCommonParameters();
36 58
        $this->repositionParameters();
37 58
    }
38
39
    /**
40
     * Convert a Boolean value to a string
41
     */
42 61
    private function resolveBoolInParameters()
43
    {
44 61
        if (isset($this->options['query'])) {
45 25
            $this->options['query'] = array_map(
46 25
                static function ($value) {
47 25
                    return self::boolToString($value);
48 25
                },
49 25
                $this->options['query']
50 25
            );
51 25
        }
52 61
    }
53
54
    /**
55
     * Convert a Boolean value to a string.
56
     *
57
     * @param bool|string $value
58
     *
59
     * @return string
60
     */
61 32
    public static function boolToString($value)
62
    {
63 32
        if (is_bool($value)) {
64 3
            return $value ? 'true' : 'false';
65
        }
66
67 29
        return $value;
68
    }
69
70
    /**
71
     * Resolve Common Parameters.
72
     *
73
     * @throws ClientException
74
     * @throws Exception
75
     */
76 63
    private function resolveCommonParameters()
77
    {
78 63
        $signature                                  = $this->httpClient()->getSignature();
79 63
        $this->options['query']['RegionId']         = $this->realRegionId();
80 63
        $this->options['query']['Format']           = $this->format;
81 63
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
82 63
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
83 63
        $this->options['query']['SignatureNonce']   = Sign::uuid($this->product . $this->action);
84 63
        $this->options['query']['Timestamp']        = gmdate($this->dateTimeFormat);
85 63
        $this->options['query']['Action']           = $this->action;
86 63
        if ($this->credential()->getAccessKeyId()) {
0 ignored issues
show
Bug introduced by
The method getAccessKeyId() does not exist on AlibabaCloud\Client\Cred...ls\CredentialsInterface. It seems like you code against a sub-type of said class. However, the method does not exist in AlibabaCloud\Client\Cred...ls\RsaKeyPairCredential or AlibabaCloud\Client\Cred...ls\EcsRamRoleCredential. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        if ($this->credential()->/** @scrutinizer ignore-call */ getAccessKeyId()) {
Loading history...
87 54
            $this->options['query']['AccessKeyId'] = $this->credential()->getAccessKeyId();
88 54
        }
89 61
        if ($signature->getType()) {
90 12
            $this->options['query']['SignatureType'] = $signature->getType();
91 12
        }
92 61
        if (!isset($this->options['query']['Version'])) {
93 61
            $this->options['query']['Version'] = $this->version;
94 61
        }
95 61
        $this->resolveSecurityToken();
96 61
        $this->resolveBearerToken();
97 61
        $this->options['query']['Signature'] = $this->signature();
98 60
    }
99
100
    /**
101
     * @throws ClientException
102
     * @throws ServerException
103
     */
104 62
    private function resolveSecurityToken()
105
    {
106 62
        if (!$this->credential() instanceof StsCredential) {
107 59
            return;
108
        }
109
110 3
        if (!$this->credential()->getSecurityToken()) {
111 2
            return;
112
        }
113
114 1
        $this->options['query']['SecurityToken'] = $this->credential()->getSecurityToken();
115 1
    }
116
117
    /**
118
     * @throws ClientException
119
     * @throws ServerException
120
     */
121 61
    private function resolveBearerToken()
122
    {
123 61
        if ($this->credential() instanceof BearerTokenCredential) {
124 7
            $this->options['query']['BearerToken'] = $this->credential()->getBearerToken();
125 7
        }
126 61
    }
127
128
    /**
129
     * Sign the parameters.
130
     *
131
     * @return mixed
132
     * @throws ClientException
133
     * @throws ServerException
134
     */
135 63
    private function signature()
136
    {
137 63
        return $this->httpClient()
138 63
                    ->getSignature()
139 63
                    ->sign(
140 63
                        $this->stringToSign(),
141 63
                        $this->credential()->getAccessKeySecret() . '&'
0 ignored issues
show
Bug introduced by
The method getAccessKeySecret() does not exist on AlibabaCloud\Client\Cred...ls\CredentialsInterface. It seems like you code against a sub-type of said class. However, the method does not exist in AlibabaCloud\Client\Cred...ls\RsaKeyPairCredential or AlibabaCloud\Client\Cred...ls\EcsRamRoleCredential. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
                        $this->credential()->/** @scrutinizer ignore-call */ getAccessKeySecret() . '&'
Loading history...
142 63
                    );
143
    }
144
145
    /**
146
     * @return string
147
     */
148 63
    public function stringToSign()
149
    {
150 63
        $query       = isset($this->options['query']) ? $this->options['query'] : [];
151 63
        $form_params = isset($this->options['form_params']) ? $this->options['form_params'] : [];
152 63
        $parameters  = Arrays::merge([$query, $form_params]);
153
154 63
        return Sign::rpcString($this->method, $parameters);
155
    }
156
157
    /**
158
     * Adjust parameter position
159
     */
160 58
    private function repositionParameters()
161
    {
162 58
        if ($this->method === 'POST' || $this->method === 'PUT') {
163 34
            foreach ($this->options['query'] as $api_key => $api_value) {
164 34
                $this->options['form_params'][$api_key] = $api_value;
165 34
            }
166 34
            unset($this->options['query']);
167 34
        }
168 58
169
        if (isset($this->options['form_params'])) {
170
            foreach ($this->options['form_params'] as $key => $value) {
171
                if ($value === null || $value === '') {
172
                    unset($this->options['form_params'][$key]);
173
                }
174
            }
175
        }
176
177
        if (isset($this->options['query'])) {
178 3
            foreach ($this->options['query'] as $key => $value) {
179
                if ($value === null || $value === '') {
180 3
                    unset($this->options['query'][$key]);
181 1
                }
182
            }
183 1
        }
184
    }
185
186 3
    /**
187 1
     * Magic method for set or get request parameters.
188 1
     *
189 1
     * @param string $name
190
     * @param mixed  $arguments
191 1
     *
192
     * @return $this
193
     */
194 2
    public function __call($name, $arguments)
195 1
    {
196 1
        if (strncmp($name, 'get', 3) === 0) {
197
            $parameter_name = \mb_strcut($name, 3);
198 1
199
            return $this->__get($parameter_name);
200
        }
201 1
202
        if (strncmp($name, 'with', 4) === 0) {
203
            $parameter_name = \mb_strcut($name, 4);
204
            $this->__set($parameter_name, $arguments[0]);
205
            $this->options['query'][$parameter_name] = $arguments[0];
206
207
            return $this;
208
        }
209
210
        if (strncmp($name, 'set', 3) === 0) {
211
            $parameter_name = \mb_strcut($name, 3);
212
            $with_method    = "with$parameter_name";
213
214
            throw new RuntimeException("Please use $with_method instead of $name");
215
        }
216
217
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
218
    }
219
}
220