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

RpcRequest::resolveParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use Exception;
6
use Ramsey\Uuid\Uuid;
7
use RuntimeException;
8
use AlibabaCloud\Client\Credentials\StsCredential;
9
use AlibabaCloud\Client\Exception\ClientException;
10
use AlibabaCloud\Client\Exception\ServerException;
11
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
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
     */
31 50
    public function resolveParameter()
32
    {
33 50
        $this->resolveBoolInParameters();
34 50
        $this->resolveCommonParameters();
35 47
        $this->repositionParameters();
36 47
    }
37
38
    /**
39
     * Convert a Boolean value to a string
40
     */
41 50
    private function resolveBoolInParameters()
42
    {
43 50
        if (isset($this->options['query'])) {
44 25
            $this->options['query'] = array_map(
45 25
                static function ($value) {
46 25
                    return self::boolToString($value);
47 25
                },
48 25
                $this->options['query']
49 25
            );
50 25
        }
51 50
    }
52
53
    /**
54
     * Convert a Boolean value to a string.
55
     *
56
     * @param bool|string $value
57
     *
58
     * @return string
59
     */
60 32
    public static function boolToString($value)
61
    {
62 32
        if (is_bool($value)) {
63 3
            return $value ? 'true' : 'false';
64
        }
65
66 29
        return $value;
67
    }
68
69
    /**
70
     * Resolve Common Parameters.
71
     *
72
     * @throws ClientException
73
     * @throws Exception
74
     */
75 52
    private function resolveCommonParameters()
76
    {
77 52
        $signature                                  = $this->httpClient()->getSignature();
78 52
        $this->options['query']['RegionId']         = $this->realRegionId();
79 52
        $this->options['query']['Format']           = $this->format;
80 52
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
81 52
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
82 52
        $this->options['query']['SignatureNonce']   = Uuid::uuid1()->toString();
83 52
        $this->options['query']['Timestamp']        = gmdate($this->dateTimeFormat);
84 52
        $this->options['query']['Action']           = $this->action;
85 52
        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

85
        if ($this->credential()->/** @scrutinizer ignore-call */ getAccessKeyId()) {
Loading history...
86 43
            $this->options['query']['AccessKeyId'] = $this->credential()->getAccessKeyId();
87 43
        }
88 50
        if ($signature->getType()) {
89 12
            $this->options['query']['SignatureType'] = $signature->getType();
90 12
        }
91 50
        if (!isset($this->options['query']['Version'])) {
92 50
            $this->options['query']['Version'] = $this->version;
93 50
        }
94 50
        $this->resolveSecurityToken();
95 50
        $this->resolveBearerToken();
96 50
        $this->options['query']['Signature'] = $this->signature();
97 49
    }
98
99
    /**
100
     * @throws ClientException
101
     * @throws ServerException
102
     */
103 50
    private function resolveSecurityToken()
104
    {
105 50
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
106
            $this->options['query']['SecurityToken'] = $this->credential()->getSecurityToken();
107
        }
108 50
    }
109
110
    /**
111
     * @throws ClientException
112
     * @throws ServerException
113
     */
114 50
    private function resolveBearerToken()
115
    {
116 50
        if ($this->credential() instanceof BearerTokenCredential) {
117 7
            $this->options['query']['BearerToken'] = $this->credential()->getBearerToken();
118 7
        }
119 50
    }
120
121
    /**
122
     * Sign the parameters.
123
     *
124
     * @return mixed
125
     * @throws ClientException
126
     * @throws ServerException
127
     */
128 52
    private function signature()
129
    {
130 52
        return $this->httpClient()
131 52
                    ->getSignature()
132 52
                    ->sign(
133 52
                        $this->stringToSign(),
134 52
                        $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

134
                        $this->credential()->/** @scrutinizer ignore-call */ getAccessKeySecret() . '&'
Loading history...
135 52
                    );
136
    }
137
138
    /**
139
     * @return string
140
     */
141 52
    public function stringToSign()
142
    {
143 52
        $query      = isset($this->options['query']) ? $this->options['query'] : [];
144 52
        $form       = isset($this->options['form_params']) ? $this->options['form_params'] : [];
145 52
        $parameters = \AlibabaCloud\Client\arrayMerge([$query, $form]);
146 52
        ksort($parameters);
147 52
        $canonicalized = '';
148 52
        foreach ($parameters as $key => $value) {
149 52
            $canonicalized .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
150 52
        }
151
152 52
        return $this->method . '&%2F&' . $this->percentEncode(substr($canonicalized, 1));
153
    }
154
155
    /**
156
     * @param string $string
157
     *
158
     * @return null|string|string[]
159
     */
160 55
    private function percentEncode($string)
161
    {
162 55
        $result = urlencode($string);
163 55
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
164 55
        $result = preg_replace('/%7E/', '~', $result);
165
166 55
        return $result;
167
    }
168
169
    /**
170
     * Adjust parameter position
171
     */
172 47
    private function repositionParameters()
173
    {
174 47
        if ($this->method === 'POST' || $this->method === 'PUT') {
175 23
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
176 23
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
177 23
            }
178 23
            unset($this->options['query']);
179 23
        }
180 47
    }
181
182
    /**
183
     * Magic method for set or get request parameters.
184
     *
185
     * @param string $name
186
     * @param mixed  $arguments
187
     *
188
     * @return $this
189
     */
190 3
    public function __call($name, $arguments)
191
    {
192 3
        if (strncmp($name, 'get', 3) === 0) {
193 1
            $parameterName = $this->propertyNameByMethodName($name);
194
195 1
            return $this->__get($parameterName);
196
        }
197
198 3
        if (strncmp($name, 'with', 4) === 0) {
199 1
            $parameterName = $this->propertyNameByMethodName($name, 4);
200 1
            $this->__set($parameterName, $arguments[0]);
201 1
            $this->options['query'][$parameterName] = $arguments[0];
202
203 1
            return $this;
204
        }
205
206 2
        if (strncmp($name, 'set', 3) === 0) {
207 1
            $parameterName = $this->propertyNameByMethodName($name);
208 1
            $withMethod    = "with$parameterName";
209
210 1
            throw new RuntimeException("Please use $withMethod instead of $name");
211
        }
212
213 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
214
    }
215
}
216