| Conditions | 9 |
| Paths | 49 |
| Total Lines | 53 |
| Code Lines | 22 |
| 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) $url .= $key . '=' . urlencode($value) . '&'; |
||
| 52 | |||
| 53 | // trim last & |
||
| 54 | $url = trim($url, '&'); |
||
| 55 | |||
| 56 | if ($this->api_key) { |
||
| 57 | $url .= '&key=' . $this->api_key; |
||
| 58 | } |
||
| 59 | |||
| 60 | // init curl |
||
| 61 | $curl = curl_init(); |
||
| 62 | |||
| 63 | // set options |
||
| 64 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 65 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 66 | curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
||
| 67 | if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
||
| 68 | |||
| 69 | // execute |
||
| 70 | $response = curl_exec($curl); |
||
| 71 | |||
| 72 | // fetch errors |
||
| 73 | $errorNumber = curl_errno($curl); |
||
| 74 | $errorMessage = curl_error($curl); |
||
| 75 | |||
| 76 | // close curl |
||
| 77 | curl_close($curl); |
||
| 78 | |||
| 79 | // we have errors |
||
| 80 | if ($errorNumber != '') throw new Exception($errorMessage); |
||
| 81 | |||
| 82 | // redefine response as json decoded |
||
| 83 | $response = json_decode($response); |
||
| 84 | |||
| 85 | // API returns with an error |
||
| 86 | if (isset($response->error_message)) { |
||
| 87 | throw new Exception($response->error_message); |
||
| 88 | } |
||
| 89 | |||
| 90 | // return the content |
||
| 91 | return $response->results; |
||
| 92 | } |
||
| 93 | |||
| 195 |