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

RpcRequest::resolveBoolInParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 7
cts 7
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
     */
31 60
    public function resolveParameters()
32
    {
33 60
        $this->resolveBoolInParameters();
34 60
        $this->resolveCommonParameters();
35 57
        $this->repositionParameters();
36 57
    }
37
38 60
    private function resolveBoolInParameters()
39
    {
40 60
        if (isset($this->options['query'])) {
41 25
            $this->options['query'] = array_map(
42 25
                static function($value) {
43 25
                    return self::boolToString($value);
44 25
                },
45 25
                $this->options['query']
46 25
            );
47 25
        }
48 60
    }
49
50
    /**
51
     * Resolve Common Parameters.
52
     *
53
     * @throws ClientException
54
     * @throws Exception
55
     */
56 62
    private function resolveCommonParameters()
57
    {
58 62
        $signature                                  = $this->httpClient()->getSignature();
59 62
        $this->options['query']['RegionId']         = $this->realRegionId();
60 62
        $this->options['query']['Format']           = $this->format;
61 62
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
62 62
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
63 62
        $this->options['query']['SignatureNonce']   = Uuid::uuid1()->toString();
64 62
        $this->options['query']['Timestamp']        = gmdate($this->dateTimeFormat);
65 62
        $this->options['query']['Action']           = $this->action;
66 62
        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

66
        if ($this->credential()->/** @scrutinizer ignore-call */ getAccessKeyId()) {
Loading history...
67 53
            $this->options['query']['AccessKeyId'] = $this->credential()->getAccessKeyId();
68 53
        }
69 60
        if ($signature->getType()) {
70 12
            $this->options['query']['SignatureType'] = $signature->getType();
71 12
        }
72 60
        if (!isset($this->options['query']['Version'])) {
73 60
            $this->options['query']['Version'] = $this->version;
74 60
        }
75 60
        $this->resolveSecurityToken();
76 60
        $this->resolveBearerToken();
77 60
        $this->options['query']['Signature'] = $this->signature();
78 59
    }
79
80
    /**
81
     * Adjust parameter position
82
     */
83 57
    private function repositionParameters()
84
    {
85 57
        if ($this->method === 'POST' || $this->method === 'PUT') {
86 32
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
87 32
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
88 32
            }
89 32
            unset($this->options['query']);
90 32
        }
91 57
    }
92
93
    /**
94
     * Convert a Boolean value to a string.
95
     *
96
     * @param bool|string $value
97
     *
98
     * @return string
99
     */
100 32
    public static function boolToString($value)
101
    {
102 32
        if (is_bool($value)) {
103 3
            return $value ? 'true' : 'false';
104
        }
105
106 29
        return $value;
107
    }
108
109
    /**
110
     * @throws ClientException
111
     * @throws ServerException
112
     */
113 60
    private function resolveSecurityToken()
114
    {
115 60
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
116
            $this->options['query']['SecurityToken'] = $this->credential()->getSecurityToken();
117
        }
118 60
    }
119
120
    /**
121
     * @throws ClientException
122
     * @throws ServerException
123
     */
124 60
    private function resolveBearerToken()
125
    {
126 60
        if ($this->credential() instanceof BearerTokenCredential) {
127 7
            $this->options['query']['BearerToken'] = $this->credential()->getBearerToken();
128 7
        }
129 60
    }
130
131
    /**
132
     * Sign the parameters.
133
     *
134
     * @return mixed
135
     * @throws ClientException
136
     * @throws ServerException
137
     */
138 62
    private function signature()
139
    {
140 62
        return $this->httpClient()
141 62
                    ->getSignature()
142 62
                    ->sign(
143 62
                        $this->stringToSign(),
144 62
                        $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

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