| Conditions | 21 |
| Paths | 864 |
| Total Lines | 81 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 133 | public function execute($url, $method='GET', $parameters=[], $headers=[]){ |
||
| 134 | $client = clone $this; |
||
| 135 | $client->url = $url; |
||
|
|
|||
| 136 | $client->handle = curl_init(); |
||
| 137 | $curlopt = [ |
||
| 138 | CURLOPT_HEADER => TRUE, |
||
| 139 | CURLOPT_RETURNTRANSFER => TRUE, |
||
| 140 | CURLOPT_USERAGENT => $client->options['user_agent'] |
||
| 141 | ]; |
||
| 142 | |||
| 143 | if($client->options['username'] && $client->options['password']) |
||
| 144 | $curlopt[CURLOPT_USERPWD] = sprintf("%s:%s", |
||
| 145 | $client->options['username'], $client->options['password']); |
||
| 146 | |||
| 147 | if(count($client->options['headers']) || count($headers)){ |
||
| 148 | $curlopt[CURLOPT_HTTPHEADER] = []; |
||
| 149 | $headers = array_merge($client->options['headers'], $headers); |
||
| 150 | foreach($headers as $key => $values){ |
||
| 151 | foreach(is_array($values)? $values : [$values] as $value){ |
||
| 152 | $curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | if($client->options['format']) |
||
| 158 | $client->url .= '.'.$client->options['format']; |
||
| 159 | |||
| 160 | // Allow passing parameters as a pre-encoded string (or something that |
||
| 161 | // allows casting to a string). Parameters passed as strings will not be |
||
| 162 | // merged with parameters specified in the default options. |
||
| 163 | if(is_array($parameters)){ |
||
| 164 | $parameters = array_merge($client->options['parameters'], $parameters); |
||
| 165 | $parameters_string = http_build_query($parameters); |
||
| 166 | |||
| 167 | // http_build_query automatically adds an array index to repeated |
||
| 168 | // parameters which is not desirable on most systems. This hack |
||
| 169 | // reverts "key[0]=foo&key[1]=bar" to "key[]=foo&key[]=bar" |
||
| 170 | if(!$client->options['build_indexed_queries']) |
||
| 171 | $parameters_string = preg_replace( |
||
| 172 | "/%5B[0-9]+%5D=/simU", "%5B%5D=", $parameters_string); |
||
| 173 | } |
||
| 174 | else |
||
| 175 | $parameters_string = (string) $parameters; |
||
| 176 | |||
| 177 | if(strtoupper($method) == 'POST'){ |
||
| 178 | $curlopt[CURLOPT_POST] = TRUE; |
||
| 179 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
||
| 180 | } |
||
| 181 | elseif(strtoupper($method) == 'HEAD'){ |
||
| 182 | $curlopt[CURLOPT_NOBODY] = TRUE; |
||
| 183 | } |
||
| 184 | elseif(strtoupper($method) != 'GET'){ |
||
| 185 | $curlopt[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
||
| 186 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
||
| 187 | } |
||
| 188 | elseif($parameters_string){ |
||
| 189 | $client->url .= strpos($client->url, '?')? '&' : '?'; |
||
| 190 | $client->url .= $parameters_string; |
||
| 191 | } |
||
| 192 | |||
| 193 | if($client->options['base_url']){ |
||
| 194 | if($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/') |
||
| 195 | $client->url = '/' . $client->url; |
||
| 196 | $client->url = $client->options['base_url'] . $client->url; |
||
| 197 | } |
||
| 198 | $curlopt[CURLOPT_URL] = $client->url; |
||
| 199 | |||
| 200 | if($client->options['curl_options']){ |
||
| 201 | // array_merge would reset our numeric keys. |
||
| 202 | foreach($client->options['curl_options'] as $key => $value){ |
||
| 203 | $curlopt[$key] = $value; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | curl_setopt_array($client->handle, $curlopt); |
||
| 207 | |||
| 208 | $client->parse_response(curl_exec($client->handle)); |
||
| 209 | $client->info = (object) curl_getinfo($client->handle); |
||
| 210 | $client->error = curl_error($client->handle); |
||
| 211 | |||
| 212 | curl_close($client->handle); |
||
| 213 | return $client; |
||
| 214 | } |
||
| 281 |