| Conditions | 22 |
| Paths | 137 |
| Total Lines | 107 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 108 | protected function executeRequest(RequestInterface $request) |
||
| 109 | { |
||
| 110 | $this->logger->debug( |
||
| 111 | sprintf('HTTP Request %s %s', $request->getMethod(), $request->getUrl()), |
||
| 112 | ['headers' => $request->getHeaders(), 'body' => $request->toArray()] |
||
| 113 | ); |
||
| 114 | |||
| 115 | if (!$request->validate()) { |
||
| 116 | throw new \RuntimeException('Request entity is not valid'); |
||
| 117 | } |
||
| 118 | |||
| 119 | $ch = curl_init(); |
||
| 120 | |||
| 121 | if ($ch === false) { |
||
| 122 | throw new \RuntimeException('Could not get cURL resource'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $options = [ |
||
| 126 | CURLOPT_URL => $request->getUrl(), |
||
| 127 | CURLOPT_HTTPHEADER => $request->getHeaders(), |
||
| 128 | CURLOPT_HTTP_VERSION => $request->getProtocolVersion(), |
||
| 129 | ]; |
||
| 130 | |||
| 131 | $customMethods = [ |
||
| 132 | Request::METHOD_PUT, |
||
| 133 | Request::METHOD_PATCH, |
||
| 134 | Request::METHOD_DELETE, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | if ($request->getMethod() === Request::METHOD_POST) { |
||
| 138 | $options[CURLOPT_POST] = true; |
||
| 139 | $options[CURLOPT_POSTFIELDS] = $request->toArray(); |
||
| 140 | } elseif ($request->getMethod() === Request::METHOD_GET && $request->toArray()) { |
||
| 141 | $paramChar = strpos('?', $request->getUrl()) === false ? '?' : '&'; |
||
| 142 | $options[CURLOPT_URL] = $request->getUrl() . $paramChar . http_build_query($request->toArray()); |
||
| 143 | } elseif (in_array($request->getMethod(), $customMethods)) { |
||
| 144 | $options[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
||
| 145 | $options[CURLOPT_POSTFIELDS] = $request->toArray(); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ( |
||
| 149 | isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS]) |
||
| 150 | && $request->getHeader('Content-Type') == 'application/x-www-form-urlencoded' |
||
| 151 | ) { |
||
| 152 | $options[CURLOPT_POSTFIELDS] = http_build_query($options[CURLOPT_POSTFIELDS]); |
||
| 153 | } elseif ( |
||
| 154 | isset($options[CURLOPT_POSTFIELDS]) && is_array($options[CURLOPT_POSTFIELDS]) |
||
| 155 | && $request->getHeader('Content-Type') == 'application/json' |
||
| 156 | ) { |
||
| 157 | $options[CURLOPT_POSTFIELDS] = json_encode($options[CURLOPT_POSTFIELDS]); |
||
| 158 | } |
||
| 159 | |||
| 160 | curl_setopt_array($ch, $this->getRequestOptions($options)); |
||
| 161 | |||
| 162 | $result = curl_exec($ch); |
||
| 163 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 164 | $errorMessage = curl_error($ch); |
||
| 165 | $errorNumber = curl_errno($ch); |
||
| 166 | |||
| 167 | curl_close($ch); |
||
| 168 | |||
| 169 | $this->logger->debug( |
||
| 170 | sprintf('HTTP Response %s %s', $request->getMethod(), $request->getUrl()), |
||
| 171 | ['httpCode' => $httpCode, 'body' => $result, 'curlError' => $errorMessage] |
||
| 172 | ); |
||
| 173 | |||
| 174 | if ($errorNumber !== 0) { |
||
| 175 | throw new PayeverCommunicationException($errorMessage, $errorNumber); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($httpCode >= 400) { |
||
| 179 | $message = $result; |
||
| 180 | $data = json_decode($result, true); |
||
| 181 | |||
| 182 | if (isset($data['error_description'])) { |
||
| 183 | $message = $data['error_description']; |
||
| 184 | } elseif (isset($data['message'])) { |
||
| 185 | $message = $data['message']; |
||
| 186 | } |
||
| 187 | |||
| 188 | if (isset($data['error'])) { |
||
| 189 | $message = sprintf( |
||
| 190 | '%s: %s', |
||
| 191 | $data['error'], |
||
| 192 | $message |
||
| 193 | ? (is_array($message) ? json_encode($message) : $message) |
||
| 194 | : 'Unknown' |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | throw new PayeverCommunicationException($message, $httpCode); |
||
| 199 | } |
||
| 200 | |||
| 201 | $response = new Response(); |
||
| 202 | |||
| 203 | $response |
||
| 204 | ->setRequestEntity($request->getRequestEntity()) |
||
| 205 | ->setResponseEntity($request->getResponseEntity()) |
||
| 206 | ->load($result); |
||
| 207 | |||
| 208 | if ($response->isFailed()) { |
||
| 209 | throw new PayeverCommunicationException( |
||
| 210 | $response->getResponseEntity()->getErrorDescription() |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $response; |
||
| 215 | } |
||
| 319 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.