| Conditions | 10 |
| Paths | 26 |
| Total Lines | 52 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 176 | private function sendRequest($method, $uri, $authBearer, $lang, array $multipart = [], $queryParams = []) |
||
| 177 | { |
||
| 178 | $httpClient = new Client(['base_uri' => $this->plugin->getApiUrl()]); |
||
| 179 | |||
| 180 | $options = []; |
||
| 181 | $options['headers'] = [ |
||
| 182 | 'Authorization' => "Bearer $authBearer", |
||
| 183 | 'Accept-Language' => $lang, |
||
| 184 | ]; |
||
| 185 | |||
| 186 | if ($queryParams) { |
||
| 187 | $options['query'] = $queryParams; |
||
| 188 | } else { |
||
| 189 | $options['multipart'] = $multipart; |
||
| 190 | } |
||
| 191 | |||
| 192 | try { |
||
| 193 | $responseBody = $httpClient |
||
| 194 | ->request( |
||
| 195 | $method, |
||
| 196 | $uri, |
||
| 197 | $options |
||
| 198 | ) |
||
| 199 | ->getBody() |
||
| 200 | ->getContents(); |
||
| 201 | |||
| 202 | return json_decode($responseBody, true); |
||
| 203 | } catch (RequestException $requestException) { |
||
| 204 | if (!$requestException->hasResponse()) { |
||
| 205 | throw new \Exception($requestException->getMessage()); |
||
| 206 | } |
||
| 207 | |||
| 208 | $responseBody = $requestException->getResponse()->getBody()->getContents(); |
||
| 209 | $json = json_decode($responseBody, true); |
||
| 210 | |||
| 211 | $message = ''; |
||
| 212 | |||
| 213 | if (isset($json['asserts'])) { |
||
| 214 | foreach ($json['asserts'] as $assert) { |
||
| 215 | if ('invalid_' === substr($assert['value'], 0, 8)) { |
||
| 216 | $message .= $assert['message'].PHP_EOL; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } elseif (empty($json['message'])) { |
||
| 220 | $message = $requestException->getMessage(); |
||
| 221 | } else { |
||
| 222 | $message = is_array($json['message']) ? implode(PHP_EOL, $json['message']) : $json['message']; |
||
| 223 | } |
||
| 224 | |||
| 225 | throw new \Exception($message); |
||
| 226 | } catch (Exception $exception) { |
||
| 227 | throw new \Exception($exception->getMessage()); |
||
| 228 | } |
||
| 231 |