| Conditions | 9 |
| Paths | 49 |
| Total Lines | 59 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 40 | protected function doCall($parameters = array()) |
||
| 41 | { |
||
| 42 | // check if curl is available |
||
| 43 | if (!function_exists('curl_init')) { |
||
| 44 | throw Exception::CurlNotInstalled(); |
||
| 45 | } |
||
| 46 | |||
| 47 | // define url |
||
| 48 | $url = ($this->https ? 'https://' : 'http://') . self::API_URL . '?'; |
||
| 49 | |||
| 50 | // add every parameter to the url |
||
| 51 | foreach ($parameters as $key => $value) { |
||
| 52 | $url .= $key . '=' . urlencode($value) . '&'; |
||
| 53 | } |
||
| 54 | |||
| 55 | // trim last & |
||
| 56 | $url = trim($url, '&'); |
||
| 57 | |||
| 58 | if ($this->api_key) { |
||
| 59 | $url .= '&key=' . $this->api_key; |
||
| 60 | } |
||
| 61 | |||
| 62 | // init curl |
||
| 63 | $curl = curl_init(); |
||
| 64 | |||
| 65 | // set options |
||
| 66 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 67 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 68 | curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
||
| 69 | if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { |
||
| 70 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
||
| 71 | } |
||
| 72 | |||
| 73 | // execute |
||
| 74 | $response = curl_exec($curl); |
||
| 75 | |||
| 76 | // fetch errors |
||
| 77 | $errorNumber = curl_errno($curl); |
||
| 78 | $errorMessage = curl_error($curl); |
||
| 79 | |||
| 80 | // close curl |
||
| 81 | curl_close($curl); |
||
| 82 | |||
| 83 | // we have errors |
||
| 84 | if ($errorNumber != '') { |
||
| 85 | throw new Exception($errorMessage); |
||
| 86 | } |
||
| 87 | |||
| 88 | // redefine response as json decoded |
||
| 89 | $response = json_decode($response); |
||
| 90 | |||
| 91 | // API returns with an error |
||
| 92 | if (isset($response->error_message)) { |
||
| 93 | throw new Exception($response->error_message); |
||
| 94 | } |
||
| 95 | |||
| 96 | // return the content |
||
| 97 | return $response->results; |
||
| 98 | } |
||
| 99 | |||
| 211 |