| Conditions | 6 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 42 | public function makeRequest( |
||
| 43 | $path, |
||
| 44 | $method, |
||
| 45 | array $parameters = array(), |
||
| 46 | $timeout = 30, |
||
| 47 | $verify = false, |
||
| 48 | $debug = false |
||
|
|
|||
| 49 | ) { |
||
| 50 | $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); |
||
| 51 | if (!in_array($method, $allowedMethods)) { |
||
| 52 | throw new \InvalidArgumentException(sprintf( |
||
| 53 | 'Method "%s" is not valid. Allowed methods are %s', |
||
| 54 | $method, |
||
| 55 | implode(', ', $allowedMethods) |
||
| 56 | )); |
||
| 57 | } |
||
| 58 | |||
| 59 | $parameters = array_merge($this->defaultParameters, $parameters); |
||
| 60 | |||
| 61 | $path = $this->url . $path; |
||
| 62 | |||
| 63 | if (self::METHOD_GET === $method && sizeof($parameters)) { |
||
| 64 | $path .= '?' . http_build_query($parameters, '', '&'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $ch = curl_init(); |
||
| 68 | curl_setopt($ch, CURLOPT_URL, $path); |
||
| 69 | curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); |
||
| 70 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); |
||
| 71 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 72 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
| 73 | curl_setopt($ch, CURLOPT_FAILONERROR, false); |
||
| 74 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify); |
||
| 75 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify); |
||
| 76 | |||
| 77 | if (self::METHOD_POST === $method) { |
||
| 78 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 79 | curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); |
||
| 80 | } |
||
| 81 | |||
| 82 | $responseBody = curl_exec($ch); |
||
| 83 | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 84 | $errno = curl_errno($ch); |
||
| 85 | $error = curl_error($ch); |
||
| 86 | |||
| 87 | curl_close($ch); |
||
| 88 | |||
| 89 | if ($errno) { |
||
| 90 | throw new CurlException($error, $errno); |
||
| 91 | } |
||
| 92 | |||
| 93 | return new ApiResponse($statusCode, $responseBody); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.