| Conditions | 28 |
| Paths | 185 |
| Total Lines | 159 |
| Code Lines | 98 |
| 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 |
||
| 287 | public function exec($force_connection_method = null) |
||
| 288 | { |
||
| 289 | if ($force_connection_method !== self::FORCE_SOCKET && self::isCurlAvailable()) { |
||
| 290 | $ch = curl_init(); |
||
| 291 | |||
| 292 | curl_setopt($ch, CURLOPT_URL, sprintf( |
||
| 293 | "%s://%s%s%s", |
||
| 294 | $this->_scheme, |
||
| 295 | $this->_host, |
||
| 296 | (!is_null($this->_port) ? ':' . $this->_port : null), |
||
| 297 | $this->_path |
||
| 298 | )); |
||
| 299 | curl_setopt($ch, CURLOPT_HEADER, $this->_returnHeaders); |
||
| 300 | curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); |
||
| 301 | curl_setopt($ch, CURLOPT_PORT, $this->_port); |
||
| 302 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 303 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); |
||
| 304 | |||
| 305 | if (ini_get('safe_mode') == 0 && ini_get('open_basedir') == '') { |
||
| 306 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
| 307 | } |
||
| 308 | |||
| 309 | switch ($this->_method) { |
||
| 310 | case 'POST': |
||
| 311 | curl_setopt($ch, CURLOPT_POST, 1); |
||
| 312 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postfields); |
||
| 313 | break; |
||
| 314 | case 'PUT': |
||
| 315 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
||
| 316 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postfields); |
||
| 317 | $this->setopt('HTTPHEADER', array('Content-Length:' => strlen($this->_postfields))); |
||
| 318 | break; |
||
| 319 | case 'DELETE': |
||
| 320 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
||
| 321 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postfields); |
||
| 322 | break; |
||
| 323 | } |
||
| 324 | |||
| 325 | if (is_array($this->_headers) && !empty($this->_headers)) { |
||
| 326 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_headers); |
||
| 327 | } |
||
| 328 | |||
| 329 | if (is_array($this->_custom_opt) && !empty($this->_custom_opt)) { |
||
| 330 | foreach ($this->_custom_opt as $opt => $value) { |
||
| 331 | curl_setopt($ch, $opt, $value); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | // Grab the result |
||
| 336 | $result = curl_exec($ch); |
||
| 337 | |||
| 338 | $this->_info_last = curl_getinfo($ch); |
||
| 339 | $this->_info_last['curl_error'] = curl_errno($ch); |
||
| 340 | |||
| 341 | // Close the connection |
||
| 342 | curl_close($ch); |
||
| 343 | |||
| 344 | return $result; |
||
| 345 | } |
||
| 346 | |||
| 347 | $start = precision_timer(); |
||
| 348 | |||
| 349 | if (is_null($this->_port)) { |
||
| 350 | $this->_port = (!is_null($this->_scheme) ? self::$ports[$this->_scheme] : 80); |
||
| 351 | } |
||
| 352 | |||
| 353 | // No CURL is available, use attempt to use normal sockets |
||
| 354 | $handle = @fsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout); |
||
| 355 | |||
| 356 | if ($handle === false) { |
||
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | $query = $this->_method . ' ' . $this->_path . ' HTTP/1.1' . PHP_EOL; |
||
| 361 | $query .= 'Host: '.$this->_host . PHP_EOL; |
||
| 362 | $query .= 'Content-type: '.$this->_content_type . PHP_EOL; |
||
| 363 | $query .= 'User-Agent: '.$this->_agent . PHP_EOL; |
||
| 364 | $query .= @implode(PHP_EOL, $this->_headers); |
||
| 365 | $query .= 'Content-length: ' . strlen($this->_postfields) . PHP_EOL; |
||
| 366 | $query .= 'Connection: close' . PHP_EOL . PHP_EOL; |
||
| 367 | |||
| 368 | if (in_array($this->_method, array('PUT', 'POST', 'DELETE'))) { |
||
| 369 | $query .= $this->_postfields; |
||
| 370 | } |
||
| 371 | |||
| 372 | // send request |
||
| 373 | if (!@fwrite($handle, $query)) { |
||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | stream_set_blocking($handle, false); |
||
| 378 | stream_set_timeout($handle, $this->_timeout); |
||
| 379 | |||
| 380 | $status = stream_get_meta_data($handle); |
||
| 381 | $response = $dechunked = ''; |
||
| 382 | |||
| 383 | // get header |
||
| 384 | while (!preg_match('/\\r\\n\\r\\n$/', $header) && !$status['timed_out']) { |
||
| 385 | $header .= @fread($handle, 1); |
||
| 386 | $status = stream_get_meta_data($handle); |
||
| 387 | } |
||
| 388 | |||
| 389 | $status = socket_get_status($handle); |
||
| 390 | |||
| 391 | // Get rest of the page data |
||
| 392 | while (!feof($handle) && !$status['timed_out']) { |
||
| 393 | $response .= fread($handle, 4096); |
||
| 394 | $status = stream_get_meta_data($handle); |
||
| 395 | } |
||
| 396 | |||
| 397 | @fclose($handle); |
||
| 398 | |||
| 399 | $end = precision_timer('stop', $start); |
||
| 400 | |||
| 401 | if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/', $header)) { |
||
| 402 | $fp = 0; |
||
| 403 | |||
| 404 | do { |
||
| 405 | $byte = ''; |
||
| 406 | $chunk_size = ''; |
||
| 407 | |||
| 408 | do { |
||
| 409 | $chunk_size .= $byte; |
||
| 410 | $byte = substr($response, $fp, 1); |
||
| 411 | $fp++; |
||
| 412 | } while ($byte !== "\r" && $byte !== "\\r"); |
||
| 413 | |||
| 414 | $chunk_size = hexdec($chunk_size); // convert to real number |
||
| 415 | |||
| 416 | if ($chunk_size == 0) { |
||
| 417 | break(1); |
||
| 418 | } |
||
| 419 | |||
| 420 | $fp++; |
||
| 421 | |||
| 422 | $dechunked .= substr($response, $fp, $chunk_size); |
||
| 423 | $fp += $chunk_size; |
||
| 424 | |||
| 425 | $fp += 2; |
||
| 426 | } while (true); |
||
| 427 | |||
| 428 | $response = $dechunked; |
||
| 429 | } |
||
| 430 | |||
| 431 | // Following code emulates part of the function curl_getinfo() |
||
| 432 | preg_match('/Content-Type:\s*([^\r\n]+)/i', $header, $match); |
||
| 433 | $content_type = $match[1]; |
||
| 434 | |||
| 435 | preg_match('/HTTP\/\d+.\d+\s+(\d+)/i', $header, $match); |
||
| 436 | $status = $match[1]; |
||
| 437 | |||
| 438 | $this->_info_last = array( |
||
| 439 | 'url' => $this->_url, |
||
| 440 | 'content_type' => $content_type, |
||
| 441 | 'http_code' => (int)$status, |
||
| 442 | 'total_time' => $end |
||
| 443 | ); |
||
| 444 | |||
| 445 | return ($this->_returnHeaders ? $header : null) . $response; |
||
| 446 | } |
||
| 465 |