| Conditions | 24 |
| Paths | 174 |
| Total Lines | 70 |
| 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 |
||
| 81 | public function call(string $method, array $data = [], string $requestType = 'get') |
||
| 82 | { |
||
| 83 | $rawRequest = $data; |
||
| 84 | $startTime = microtime(true); |
||
| 85 | $headers = $this->getHeaders(); |
||
| 86 | try { |
||
| 87 | if (\in_array($requestType, ['get', 'delete'])) { |
||
| 88 | $response = $this->httpClient->request($requestType, $method, ['headers' => $headers]); |
||
| 89 | } else { |
||
| 90 | $data = Json::encode($data); |
||
| 91 | if (Config::$encryptDataTransfer) { |
||
| 92 | $data = $this->encryptData($data); |
||
| 93 | } |
||
| 94 | $response = $this->httpClient->request($requestType, $method, ['headers' => $headers, 'body' => $data]); |
||
| 95 | } |
||
| 96 | $contentType = explode(';', $response->getHeaderLine('Content-Type')); |
||
| 97 | $rawResponse = $response->getBody()->getContents(); |
||
| 98 | if ($headers['Accept'] !== reset($contentType)) { |
||
| 99 | if (Config::$apiErrorLogs || Config::$apiAllLogs) { |
||
| 100 | \App\Log::info([ |
||
| 101 | 'request' => ['date' => date('Y-m-d H:i:s', $startTime), 'requestType' => strtoupper($requestType), 'method' => $method, 'headers' => $headers, 'rawBody' => $rawRequest, 'body' => $data], |
||
| 102 | 'response' => ['time' => round(microtime(true) - $startTime, 2), 'status' => $response->getStatusCode(), 'reasonPhrase' => $response->getReasonPhrase(), 'error' => "Invalid response content type\n Accept:{$headers['Accept']} <> {$response->getHeaderLine('Content-Type')}", 'headers' => $response->getHeaders(), 'rawBody' => $rawResponse], |
||
| 103 | ], 'Api'); |
||
| 104 | } |
||
| 105 | throw new Exceptions\AppException('Invalid response content type', 500); |
||
| 106 | } |
||
| 107 | $encryptedHeader = $response->getHeader('encrypted'); |
||
| 108 | if ($encryptedHeader && 1 == $response->getHeader('encrypted')[0]) { |
||
| 109 | $rawResponse = $this->decryptData($rawResponse); |
||
| 110 | } |
||
| 111 | $responseBody = Json::decode($rawResponse); |
||
| 112 | } catch (\Throwable $e) { |
||
| 113 | if (Config::$apiErrorLogs || Config::$apiAllLogs) { |
||
| 114 | \App\Log::info([ |
||
| 115 | 'request' => ['date' => date('Y-m-d H:i:s', $startTime), 'requestType' => strtoupper($requestType), 'method' => $method, 'headers' => $headers, 'rawBody' => $rawRequest, 'body' => $data], |
||
| 116 | 'response' => ['time' => round(microtime(true) - $startTime, 2), 'status' => (method_exists($response, 'getStatusCode') ? $response->getStatusCode() : '-'), 'reasonPhrase' => (method_exists($response, 'getReasonPhrase') ? $response->getReasonPhrase() : '-'), 'error' => $e->__toString(), 'headers' => (method_exists($response, 'getHeaders') ? $response->getHeaders() : '-'), 'rawBody' => ($rawResponse ?? '')], |
||
| 117 | ], 'Api'); |
||
| 118 | } |
||
| 119 | throw new Exceptions\AppException('An error occurred while communicating with the CRM', 500, $e); |
||
| 120 | } |
||
| 121 | if (\App\Config::$debugApi) { |
||
| 122 | $_SESSION['debugApi'][] = [ |
||
| 123 | 'date' => date('Y-m-d H:i:s', $startTime), |
||
| 124 | 'time' => round(microtime(true) - $startTime, 2), |
||
| 125 | 'method' => $method, |
||
| 126 | 'requestType' => strtoupper($requestType), |
||
| 127 | 'requestId' => RequestUtil::requestId(), |
||
| 128 | 'rawRequest' => [$headers, $rawRequest], |
||
| 129 | 'rawResponse' => $rawResponse, |
||
| 130 | 'response' => $responseBody, |
||
| 131 | 'trace' => Debug::getBacktrace(), |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | if (Config::$apiAllLogs || (Config::$apiErrorLogs && (isset($responseBody['error']) || empty($responseBody) || 200 !== $response->getStatusCode()))) { |
||
| 135 | \App\Log::info([ |
||
| 136 | 'request' => ['date' => date('Y-m-d H:i:s', $startTime), 'requestType' => strtoupper($requestType), 'method' => $method, 'headers' => $headers, 'rawBody' => $rawRequest, 'body' => $data], |
||
| 137 | 'response' => ['time' => round(microtime(true) - $startTime, 2), 'status' => $response->getStatusCode(), 'reasonPhrase' => $response->getReasonPhrase(), 'headers' => $response->getHeaders(), 'rawBody' => $rawResponse, 'body' => $responseBody], |
||
| 138 | ], 'Api'); |
||
| 139 | } |
||
| 140 | if (isset($responseBody['error'])) { |
||
| 141 | $_SESSION['systemError'][] = $responseBody['error']; |
||
| 142 | throw new Exceptions\AppException($responseBody['error']['message'], $responseBody['error']['code'] ?? 500); |
||
| 143 | } |
||
| 144 | if (empty($responseBody) || 200 !== $response->getStatusCode()) { |
||
| 145 | throw new Exceptions\AppException("API returned an error:\n" . $response->getReasonPhrase(), $response->getStatusCode()); |
||
| 146 | } |
||
| 147 | if (isset($responseBody['result'])) { |
||
| 148 | return $responseBody['result']; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 208 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.