| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 116 |
| 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 |
||
| 91 | public static function makeRequst($options) { |
||
| 92 | $errorHandler = new myErrorHandler(); |
||
| 93 | |||
| 94 | $old_error_handler = set_error_handler(array($errorHandler, "proc_error")); |
||
|
|
|||
| 95 | |||
| 96 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
||
| 97 | $query = isset($options['query']) ? array_filter($options['query'], function($x) { return !is_null($x); } ) : array(); |
||
| 98 | |||
| 99 | $body = isset($options['body']) ? $options['body'] : null; |
||
| 100 | $file = isset($options['FILE']) ? $options['FILE'] : null; |
||
| 101 | $headers = array( |
||
| 102 | "User-Agent: Route4Me php-sdk" |
||
| 103 | ); |
||
| 104 | |||
| 105 | if (isset($options['HTTPHEADER'])) { |
||
| 106 | $headers[] = $options['HTTPHEADER']; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (isset($options['HTTPHEADERS'])) { |
||
| 110 | foreach ($options['HTTPHEADERS'] As $header) { |
||
| 111 | $headers[] = $header; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $ch = curl_init(); |
||
| 116 | |||
| 117 | $url = isset($options['url']) ? $options['url'].'?'.http_build_query(array_merge( |
||
| 118 | $query, array('api_key' => self::getApiKey()) |
||
| 119 | )) : ""; |
||
| 120 | |||
| 121 | $baseUrl = self::getBaseUrl(); |
||
| 122 | |||
| 123 | $curlOpts = array( |
||
| 124 | CURLOPT_URL => $baseUrl.$url, |
||
| 125 | CURLOPT_RETURNTRANSFER => true, |
||
| 126 | CURLOPT_TIMEOUT => 80, |
||
| 127 | CURLOPT_FOLLOWLOCATION => true, |
||
| 128 | CURLOPT_SSL_VERIFYHOST => FALSE, |
||
| 129 | CURLOPT_SSL_VERIFYPEER => FALSE, |
||
| 130 | CURLOPT_HTTPHEADER => $headers |
||
| 131 | ); |
||
| 132 | |||
| 133 | curl_setopt_array($ch, $curlOpts); |
||
| 134 | |||
| 135 | if ($file!=null) { |
||
| 136 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
||
| 137 | $fp=fopen($file, 'r'); |
||
| 138 | curl_setopt($ch, CURLOPT_INFILE, $fp); |
||
| 139 | curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); |
||
| 140 | } |
||
| 141 | |||
| 142 | switch ($method) { |
||
| 143 | case 'DELETE': |
||
| 144 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 145 | break; |
||
| 146 | case 'DELETEARRAY': |
||
| 147 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 148 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 149 | break; |
||
| 150 | case 'PUT': |
||
| 151 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
| 152 | break; |
||
| 153 | case 'POST': |
||
| 154 | if (isset($body)) { |
||
| 155 | $bodyData = json_encode($body); |
||
| 156 | if (isset($options['HTTPHEADER'])) { |
||
| 157 | if (strpos($options['HTTPHEADER'], "multipart/form-data")>0) { |
||
| 158 | $bodyData = $body; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | case 'ADD': |
||
| 166 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (is_numeric(array_search($method, array('DELETE', 'PUT')))) { |
||
| 170 | if (isset($body)) { |
||
| 171 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = curl_exec($ch); |
||
| 176 | |||
| 177 | $isxml = FALSE; |
||
| 178 | $jxml = ""; |
||
| 179 | if (strpos($result, '<?xml')>-1) { |
||
| 180 | $xml = simplexml_load_string($result); |
||
| 181 | //$jxml = json_encode($xml); |
||
| 182 | $jxml = self::object2array($xml); |
||
| 183 | $isxml = TRUE; |
||
| 184 | } |
||
| 185 | |||
| 186 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 187 | curl_close($ch); |
||
| 188 | |||
| 189 | if (200==$code) { |
||
| 190 | if ($isxml) { |
||
| 191 | $json = $jxml; |
||
| 192 | } else { |
||
| 193 | $json = json_decode($result, true); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (isset($json['errors'])) { |
||
| 197 | throw new ApiError(implode(', ', $json['errors'])); |
||
| 198 | } else { |
||
| 199 | return $json; |
||
| 200 | } |
||
| 201 | } elseif (409==$code) { |
||
| 202 | throw new ApiError('Wrong API key'); |
||
| 203 | } else { |
||
| 204 | throw new ApiError('Something wrong'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 349 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.