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 ( 7eb4cd...eb9699 )
by Yong
05:21
created

RpcRequest::resolveVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 Exception;
10
use Ramsey\Uuid\Uuid;
11
use RuntimeException;
12
13
/**
14
 * RESTful RPC Request.
15
 *
16
 * @package   AlibabaCloud\Client\Request
17
 */
18
class RpcRequest extends Request
19
{
20
21
    /**
22
     * @var string
23
     */
24
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
25
26
    /**
27
     * Resolve request parameter.
28
     *
29
     * @throws ClientException
30
     * @throws ServerException
31
     */
32 60
    public function resolveParameters()
33
    {
34 60
        $this->resolveCommonParameters();
35 58
        $this->options['query']['Signature'] = $this->signature();
36 57
        $this->repositionParameters();
37 57
    }
38
39
    /**
40
     * Resolve Common Parameters.
41
     *
42
     * @throws ClientException
43
     * @throws Exception
44
     */
45 62
    private function resolveCommonParameters()
46
    {
47 62
        if (isset($this->options['query'])) {
48 27
            foreach ($this->options['query'] as $key => $value) {
49 27
                $this->options['query'][$key] = self::booleanValueToString($value);
50 27
            }
51 27
        }
52
53 62
        $signature = $this->httpClient()->getSignature();
54 62
        if (!isset($this->options['query']['AccessKeyId']) && $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

54
        if (!isset($this->options['query']['AccessKeyId']) && $this->credential()->/** @scrutinizer ignore-call */ getAccessKeyId()) {
Loading history...
55 53
            $this->options['query']['AccessKeyId'] = $this->credential()->getAccessKeyId();
56 53
        }
57
58 60
        if (!isset($this->options['query']['RegionId'])) {
59 60
            $this->options['query']['RegionId'] = $this->realRegionId();
60 60
        }
61
62 60
        if (!isset($this->options['query']['Format'])) {
63 60
            $this->options['query']['Format'] = $this->format;
64 60
        }
65
66 60
        if (!isset($this->options['query']['SignatureMethod'])) {
67 60
            $this->options['query']['SignatureMethod'] = $signature->getMethod();
68 60
        }
69
70 60
        if (!isset($this->options['query']['SignatureVersion'])) {
71 60
            $this->options['query']['SignatureVersion'] = $signature->getVersion();
72 60
        }
73
74 60
        if (!isset($this->options['query']['SignatureType']) && $signature->getType()) {
75 12
            $this->options['query']['SignatureType'] = $signature->getType();
76 12
        }
77
78 60
        if (!isset($this->options['query']['SignatureNonce'])) {
79 60
            $this->options['query']['SignatureNonce'] = Uuid::uuid1()->toString();
80 60
        }
81
82 60
        if (!isset($this->options['query']['Timestamp'])) {
83 60
            $this->options['query']['Timestamp'] = gmdate($this->dateTimeFormat);
84 60
        }
85
86 60
        if (!isset($this->options['query']['Action'])) {
87 60
            $this->options['query']['Action'] = $this->action;
88 60
        }
89
90 60
        $this->resolveVersion();
91 60
        $this->resolveSecurityToken();
92 60
        $this->resolveBearerToken();
93 60
    }
94
95 60
    private function resolveVersion()
96
    {
97 60
        if (!isset($this->options['query']['Version'])) {
98 60
            $this->options['query']['Version'] = $this->version;
99 60
        }
100 60
    }
101
102
    /**
103
     * Adjust parameter position
104
     */
105 57
    private function repositionParameters()
106
    {
107 57
        if ($this->method === 'POST' || $this->method === 'PUT') {
108 32
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
109 32
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
110 32
            }
111 32
            unset($this->options['query']);
112 32
        }
113 57
    }
114
115
    /**
116
     * Convert a Boolean value to a string.
117
     *
118
     * @param bool|string $value
119
     *
120
     * @return string
121
     */
122 34
    private static function booleanValueToString($value)
123
    {
124 34
        if (is_bool($value)) {
125 3
            return $value ? 'true' : 'false';
126
        }
127
128 31
        return $value;
129
    }
130
131
    /**
132
     * @throws ClientException
133
     * @throws ServerException
134
     */
135 60
    private function resolveSecurityToken()
136
    {
137 60
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
138
            $this->options['query']['SecurityToken'] = $this->credential()->getSecurityToken();
139
        }
140 60
    }
141
142
    /**
143
     * @throws ClientException
144
     * @throws ServerException
145
     */
146 60
    private function resolveBearerToken()
147
    {
148 60
        if ($this->credential() instanceof BearerTokenCredential) {
149 7
            $this->options['query']['BearerToken'] = $this->credential()->getBearerToken();
150 7
        }
151 60
    }
152
153
    /**
154
     * Sign the parameters.
155
     *
156
     * @return mixed
157
     * @throws ClientException
158
     * @throws ServerException
159
     */
160 60
    private function signature()
161
    {
162 60
        return $this->httpClient()
163 60
                    ->getSignature()
164 60
                    ->sign(
165 60
                        $this->stringToSign(),
166 60
                        $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

166
                        $this->credential()->/** @scrutinizer ignore-call */ getAccessKeySecret() . '&'
Loading history...
167 60
                    );
168
    }
169
170
    /**
171
     * @return string
172
     */
173 60
    public function stringToSign()
174
    {
175 60
        $query       = isset($this->options['query']) ? $this->options['query'] : [];
176 60
        $form_params = isset($this->options['form_params']) ? $this->options['form_params'] : [];
177 60
        $parameters  = \AlibabaCloud\Client\arrayMerge([$query, $form_params]);
178 60
        ksort($parameters);
179 60
        $canonicalizedQuery = '';
180 60
        foreach ($parameters as $key => $value) {
181 60
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
182 60
        }
183
184 60
        return $this->method . '&%2F&' . $this->percentEncode(substr($canonicalizedQuery, 1));
185
    }
186
187
    /**
188
     * @param string $string
189
     *
190
     * @return null|string|string[]
191
     */
192 63
    private function percentEncode($string)
193
    {
194 63
        $result = urlencode($string);
195 63
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
196 63
        $result = preg_replace('/%7E/', '~', $result);
197
198 63
        return $result;
199
    }
200
201
    /**
202
     * Magic method for set or get request parameters.
203
     *
204
     * @param string $name
205
     * @param mixed  $arguments
206
     *
207
     * @return $this
208
     */
209 2
    public function __call($name, $arguments)
210
    {
211 2
        if (\strpos($name, 'get') === 0) {
212 1
            $parameterName = $this->propertyNameByMethodName($name);
213
214 1
            return $this->__get($parameterName);
215
        }
216
217 2
        if (\strpos($name, 'with') === 0) {
218 1
            $parameterName = $this->propertyNameByMethodName($name, 4);
219 1
            $this->__set($parameterName, $arguments[0]);
220 1
            $this->options['query'][$parameterName] = $arguments[0];
221
222 1
            return $this;
223
        }
224
225 2
        if (\strpos($name, 'set') === 0) {
226 1
            $parameterName = $this->propertyNameByMethodName($name);
227 1
            $withMethod    = "with$parameterName";
228
229 1
            return $this->$withMethod($arguments[0]);
230
        }
231
232 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
233
    }
234
}
235