| Conditions | 88 | 
| Paths | > 20000 | 
| Total Lines | 313 | 
| Code Lines | 202 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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  | 
            ||
| 244 |     function sendRequest($url,$data='',$method='GET'){ | 
            ||
| 245 | $this->start = $this->_time();  | 
            ||
| 246 | $this->error = '';  | 
            ||
| 247 | $this->status = 0;  | 
            ||
| 248 | $this->status = 0;  | 
            ||
| 249 | $this->resp_body = '';  | 
            ||
| 250 | $this->resp_headers = array();  | 
            ||
| 251 | |||
| 252 | // don't accept gzip if truncated bodies might occur  | 
            ||
| 253 | if($this->max_bodysize &&  | 
            ||
| 254 | !$this->max_bodysize_abort &&  | 
            ||
| 255 |            $this->headers['Accept-encoding'] == 'gzip'){ | 
            ||
| 256 | unset($this->headers['Accept-encoding']);  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | // parse URL into bits  | 
            ||
| 260 | $uri = parse_url($url);  | 
            ||
| 261 | $server = $uri['host'];  | 
            ||
| 262 | $path = $uri['path'];  | 
            ||
| 263 | if(empty($path)) $path = '/';  | 
            ||
| 264 | if(!empty($uri['query'])) $path .= '?'.$uri['query'];  | 
            ||
| 265 | if(!empty($uri['port'])) $port = $uri['port'];  | 
            ||
| 266 | if(isset($uri['user'])) $this->user = $uri['user'];  | 
            ||
| 267 | if(isset($uri['pass'])) $this->pass = $uri['pass'];  | 
            ||
| 268 | |||
| 269 | // proxy setup  | 
            ||
| 270 |         if($this->proxy_host && (!$this->proxy_except || !preg_match('/'.$this->proxy_except.'/i',$url)) ){ | 
            ||
| 271 | $request_url = $url;  | 
            ||
| 272 | $server = $this->proxy_host;  | 
            ||
| 273 | $port = $this->proxy_port;  | 
            ||
| 274 | if (empty($port)) $port = 8080;  | 
            ||
| 275 | $use_tls = $this->proxy_ssl;  | 
            ||
| 276 |         }else{ | 
            ||
| 277 | $request_url = $path;  | 
            ||
| 278 | if (!isset($port)) $port = ($uri['scheme'] == 'https') ? 443 : 80;  | 
            ||
| 279 | $use_tls = ($uri['scheme'] == 'https');  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 | // add SSL stream prefix if needed - needs SSL support in PHP  | 
            ||
| 283 |         if($use_tls) { | 
            ||
| 284 |             if(!in_array('ssl', stream_get_transports())) { | 
            ||
| 285 | $this->status = -200;  | 
            ||
| 286 | $this->error = 'This PHP version does not support SSL - cannot connect to server';  | 
            ||
| 287 | }  | 
            ||
| 288 | $server = 'ssl://'.$server;  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | // prepare headers  | 
            ||
| 292 | $headers = $this->headers;  | 
            ||
| 293 | $headers['Host'] = $uri['host'];  | 
            ||
| 294 | if(!empty($uri['port'])) $headers['Host'].= ':'.$uri['port'];  | 
            ||
| 295 | $headers['User-Agent'] = $this->agent;  | 
            ||
| 296 | $headers['Referer'] = $this->referer;  | 
            ||
| 297 | |||
| 298 |         if($method == 'POST'){ | 
            ||
| 299 |             if(is_array($data)){ | 
            ||
| 300 |                 if($headers['Content-Type'] == 'multipart/form-data'){ | 
            ||
| 301 | $headers['Content-Type'] = 'multipart/form-data; boundary='.$this->boundary;  | 
            ||
| 302 | $data = $this->_postMultipartEncode($data);  | 
            ||
| 303 |                 }else{ | 
            ||
| 304 | $headers['Content-Type'] = 'application/x-www-form-urlencoded';  | 
            ||
| 305 | $data = $this->_postEncode($data);  | 
            ||
| 306 | }  | 
            ||
| 307 | }  | 
            ||
| 308 | $headers['Content-Length'] = strlen($data);  | 
            ||
| 309 |         }elseif($method == 'GET'){ | 
            ||
| 310 | $data = ''; //no data allowed on GET requests  | 
            ||
| 311 | }  | 
            ||
| 312 |         if($this->user) { | 
            ||
| 313 | $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);  | 
            ||
| 314 | }  | 
            ||
| 315 |         if($this->proxy_user) { | 
            ||
| 316 | $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);  | 
            ||
| 317 | }  | 
            ||
| 318 | |||
| 319 | // already connected?  | 
            ||
| 320 | $connectionId = $this->_uniqueConnectionId($server,$port);  | 
            ||
| 321 |         $this->_debug('connection pool', self::$connections); | 
            ||
| 322 | $socket = null;  | 
            ||
| 323 |         if (isset(self::$connections[$connectionId])) { | 
            ||
| 324 |             $this->_debug('reusing connection', $connectionId); | 
            ||
| 325 | $socket = self::$connections[$connectionId];  | 
            ||
| 326 | }  | 
            ||
| 327 |         if (is_null($socket) || feof($socket)) { | 
            ||
| 328 |             $this->_debug('opening connection', $connectionId); | 
            ||
| 329 | // open socket  | 
            ||
| 330 | $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);  | 
            ||
| 331 |             if (!$socket){ | 
            ||
| 332 | $this->status = -100;  | 
            ||
| 333 | $this->error = "Could not connect to $server:$port\n$errstr ($errno)";  | 
            ||
| 334 | return false;  | 
            ||
| 335 | }  | 
            ||
| 336 | |||
| 337 | // try establish a CONNECT tunnel for SSL  | 
            ||
| 338 |             try { | 
            ||
| 339 |                 if($this->_ssltunnel($socket, $request_url)){ | 
            ||
| 340 | // no keep alive for tunnels  | 
            ||
| 341 | $this->keep_alive = false;  | 
            ||
| 342 | // tunnel is authed already  | 
            ||
| 343 | if(isset($headers['Proxy-Authentication'])) unset($headers['Proxy-Authentication']);  | 
            ||
| 344 | }  | 
            ||
| 345 |             } catch (HTTPClientException $e) { | 
            ||
| 346 | $this->status = $e->getCode();  | 
            ||
| 347 | $this->error = $e->getMessage();  | 
            ||
| 348 | fclose($socket);  | 
            ||
| 349 | return false;  | 
            ||
| 350 | }  | 
            ||
| 351 | |||
| 352 | // keep alive?  | 
            ||
| 353 |             if ($this->keep_alive) { | 
            ||
| 354 | self::$connections[$connectionId] = $socket;  | 
            ||
| 355 |             } else { | 
            ||
| 356 | unset(self::$connections[$connectionId]);  | 
            ||
| 357 | }  | 
            ||
| 358 | }  | 
            ||
| 359 | |||
| 360 |         if ($this->keep_alive && !$this->proxy_host) { | 
            ||
| 361 | // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive  | 
            ||
| 362 | // connection token to a proxy server. We still do keep the connection the  | 
            ||
| 363 | // proxy alive (well except for CONNECT tunnels)  | 
            ||
| 364 | $headers['Connection'] = 'Keep-Alive';  | 
            ||
| 365 |         } else { | 
            ||
| 366 | $headers['Connection'] = 'Close';  | 
            ||
| 367 | }  | 
            ||
| 368 | |||
| 369 |         try { | 
            ||
| 370 | //set non-blocking  | 
            ||
| 371 | stream_set_blocking($socket, 0);  | 
            ||
| 372 | |||
| 373 | // build request  | 
            ||
| 374 | $request = "$method $request_url HTTP/".$this->http.HTTP_NL;  | 
            ||
| 375 | $request .= $this->_buildHeaders($headers);  | 
            ||
| 376 | $request .= $this->_getCookies();  | 
            ||
| 377 | $request .= HTTP_NL;  | 
            ||
| 378 | $request .= $data;  | 
            ||
| 379 | |||
| 380 |             $this->_debug('request',$request); | 
            ||
| 381 | $this->_sendData($socket, $request, 'request');  | 
            ||
| 382 | |||
| 383 | // read headers from socket  | 
            ||
| 384 | $r_headers = '';  | 
            ||
| 385 |             do{ | 
            ||
| 386 | $r_line = $this->_readLine($socket, 'headers');  | 
            ||
| 387 | $r_headers .= $r_line;  | 
            ||
| 388 | }while($r_line != "\r\n" && $r_line != "\n");  | 
            ||
| 389 | |||
| 390 |             $this->_debug('response headers',$r_headers); | 
            ||
| 391 | |||
| 392 | // check if expected body size exceeds allowance  | 
            ||
| 393 |             if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){ | 
            ||
| 394 |                 if($match[1] > $this->max_bodysize){ | 
            ||
| 395 | if ($this->max_bodysize_abort)  | 
            ||
| 396 |                         throw new HTTPClientException('Reported content length exceeds allowed response size'); | 
            ||
| 397 | else  | 
            ||
| 398 | $this->error = 'Reported content length exceeds allowed response size';  | 
            ||
| 399 | }  | 
            ||
| 400 | }  | 
            ||
| 401 | |||
| 402 | // get Status  | 
            ||
| 403 |             if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) | 
            ||
| 404 |                 throw new HTTPClientException('Server returned bad answer '.$r_headers); | 
            ||
| 405 | |||
| 406 | $this->status = $m[2];  | 
            ||
| 407 | |||
| 408 | // handle headers and cookies  | 
            ||
| 409 | $this->resp_headers = $this->_parseHeaders($r_headers);  | 
            ||
| 410 |             if(isset($this->resp_headers['set-cookie'])){ | 
            ||
| 411 |                 foreach ((array) $this->resp_headers['set-cookie'] as $cookie){ | 
            ||
| 412 |                     list($cookie)   = explode(';',$cookie,2); | 
            ||
| 413 |                     list($key,$val) = explode('=',$cookie,2); | 
            ||
| 414 | $key = trim($key);  | 
            ||
| 415 |                     if($val == 'deleted'){ | 
            ||
| 416 |                         if(isset($this->cookies[$key])){ | 
            ||
| 417 | unset($this->cookies[$key]);  | 
            ||
| 418 | }  | 
            ||
| 419 |                     }elseif($key){ | 
            ||
| 420 | $this->cookies[$key] = $val;  | 
            ||
| 421 | }  | 
            ||
| 422 | }  | 
            ||
| 423 | }  | 
            ||
| 424 | |||
| 425 |             $this->_debug('Object headers',$this->resp_headers); | 
            ||
| 426 | |||
| 427 | // check server status code to follow redirect  | 
            ||
| 428 |             if($this->status == 301 || $this->status == 302 ){ | 
            ||
| 429 |                 if (empty($this->resp_headers['location'])){ | 
            ||
| 430 |                     throw new HTTPClientException('Redirect but no Location Header found'); | 
            ||
| 431 |                 }elseif($this->redirect_count == $this->max_redirect){ | 
            ||
| 432 |                     throw new HTTPClientException('Maximum number of redirects exceeded'); | 
            ||
| 433 |                 }else{ | 
            ||
| 434 | // close the connection because we don't handle content retrieval here  | 
            ||
| 435 | // that's the easiest way to clean up the connection  | 
            ||
| 436 | fclose($socket);  | 
            ||
| 437 | unset(self::$connections[$connectionId]);  | 
            ||
| 438 | |||
| 439 | $this->redirect_count++;  | 
            ||
| 440 | $this->referer = $url;  | 
            ||
| 441 | // handle non-RFC-compliant relative redirects  | 
            ||
| 442 |                     if (!preg_match('/^http/i', $this->resp_headers['location'])){ | 
            ||
| 443 |                         if($this->resp_headers['location'][0] != '/'){ | 
            ||
| 444 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].  | 
            ||
| 445 | dirname($uri['path']).'/'.$this->resp_headers['location'];  | 
            ||
| 446 |                         }else{ | 
            ||
| 447 | $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uri['port'].  | 
            ||
| 448 | $this->resp_headers['location'];  | 
            ||
| 449 | }  | 
            ||
| 450 | }  | 
            ||
| 451 | // perform redirected request, always via GET (required by RFC)  | 
            ||
| 452 | return $this->sendRequest($this->resp_headers['location'],array(),'GET');  | 
            ||
| 453 | }  | 
            ||
| 454 | }  | 
            ||
| 455 | |||
| 456 | // check if headers are as expected  | 
            ||
| 457 | if($this->header_regexp && !preg_match($this->header_regexp,$r_headers))  | 
            ||
| 458 |                 throw new HTTPClientException('The received headers did not match the given regexp'); | 
            ||
| 459 | |||
| 460 | //read body (with chunked encoding if needed)  | 
            ||
| 461 | $r_body = '';  | 
            ||
| 462 | if((isset($this->resp_headers['transfer-encoding']) && $this->resp_headers['transfer-encoding'] == 'chunked')  | 
            ||
| 463 |             || (isset($this->resp_headers['transfer-coding']) && $this->resp_headers['transfer-coding'] == 'chunked')){ | 
            ||
| 464 | $abort = false;  | 
            ||
| 465 |                 do { | 
            ||
| 466 | $chunk_size = '';  | 
            ||
| 467 |                     while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->_readData($socket,1,'chunk'))){ | 
            ||
| 468 | // read chunksize until \r  | 
            ||
| 469 | $chunk_size .= $byte;  | 
            ||
| 470 | if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks  | 
            ||
| 471 |                             throw new HTTPClientException('Allowed response size exceeded'); | 
            ||
| 472 | }  | 
            ||
| 473 | $this->_readLine($socket, 'chunk'); // readtrailing \n  | 
            ||
| 474 | $chunk_size = hexdec($chunk_size);  | 
            ||
| 475 | |||
| 476 |                     if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){ | 
            ||
| 477 | if ($this->max_bodysize_abort)  | 
            ||
| 478 |                             throw new HTTPClientException('Allowed response size exceeded'); | 
            ||
| 479 | $this->error = 'Allowed response size exceeded';  | 
            ||
| 480 | $chunk_size = $this->max_bodysize - strlen($r_body);  | 
            ||
| 481 | $abort = true;  | 
            ||
| 482 | }  | 
            ||
| 483 | |||
| 484 |                     if ($chunk_size > 0) { | 
            ||
| 485 | $r_body .= $this->_readData($socket, $chunk_size, 'chunk');  | 
            ||
| 486 | $this->_readData($socket, 2, 'chunk'); // read trailing \r\n  | 
            ||
| 487 | }  | 
            ||
| 488 | } while ($chunk_size && !$abort);  | 
            ||
| 489 |             }elseif(isset($this->resp_headers['content-length']) && !isset($this->resp_headers['transfer-encoding'])){ | 
            ||
| 490 | /* RFC 2616  | 
            ||
| 491 | * If a message is received with both a Transfer-Encoding header field and a Content-Length  | 
            ||
| 492 | * header field, the latter MUST be ignored.  | 
            ||
| 493 | */  | 
            ||
| 494 | |||
| 495 | // read up to the content-length or max_bodysize  | 
            ||
| 496 | // for keep alive we need to read the whole message to clean up the socket for the next read  | 
            ||
| 497 |                 if(!$this->keep_alive && $this->max_bodysize && $this->max_bodysize < $this->resp_headers['content-length']){ | 
            ||
| 498 | $length = $this->max_bodysize;  | 
            ||
| 499 |                 }else{ | 
            ||
| 500 | $length = $this->resp_headers['content-length'];  | 
            ||
| 501 | }  | 
            ||
| 502 | |||
| 503 | $r_body = $this->_readData($socket, $length, 'response (content-length limited)', true);  | 
            ||
| 504 |             }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){ | 
            ||
| 505 | $r_body = $this->_readData($socket, $this->max_bodysize, 'response (content-length limited)', true);  | 
            ||
| 506 |             }else{ | 
            ||
| 507 | // read entire socket  | 
            ||
| 508 |                 while (!feof($socket)) { | 
            ||
| 509 | $r_body .= $this->_readData($socket, 4096, 'response (unlimited)', true);  | 
            ||
| 510 | }  | 
            ||
| 511 | }  | 
            ||
| 512 | |||
| 513 | // recheck body size, we might had to read the whole body, so we abort late or trim here  | 
            ||
| 514 |             if($this->max_bodysize){ | 
            ||
| 515 |                 if(strlen($r_body) > $this->max_bodysize){ | 
            ||
| 516 |                     if ($this->max_bodysize_abort) { | 
            ||
| 517 |                         throw new HTTPClientException('Allowed response size exceeded'); | 
            ||
| 518 |                     } else { | 
            ||
| 519 | $this->error = 'Allowed response size exceeded';  | 
            ||
| 520 | }  | 
            ||
| 521 | }  | 
            ||
| 522 | }  | 
            ||
| 523 | |||
| 524 |         } catch (HTTPClientException $err) { | 
            ||
| 525 | $this->error = $err->getMessage();  | 
            ||
| 526 | if ($err->getCode())  | 
            ||
| 527 | $this->status = $err->getCode();  | 
            ||
| 528 | unset(self::$connections[$connectionId]);  | 
            ||
| 529 | fclose($socket);  | 
            ||
| 530 | return false;  | 
            ||
| 531 | }  | 
            ||
| 532 | |||
| 533 | if (!$this->keep_alive ||  | 
            ||
| 534 |                 (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) { | 
            ||
| 535 | // close socket  | 
            ||
| 536 | fclose($socket);  | 
            ||
| 537 | unset(self::$connections[$connectionId]);  | 
            ||
| 538 | }  | 
            ||
| 539 | |||
| 540 | // decode gzip if needed  | 
            ||
| 541 | if(isset($this->resp_headers['content-encoding']) &&  | 
            ||
| 542 | $this->resp_headers['content-encoding'] == 'gzip' &&  | 
            ||
| 543 |            strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){ | 
            ||
| 544 | $this->resp_body = @gzinflate(substr($r_body, 10));  | 
            ||
| 545 |             if($this->resp_body === false){ | 
            ||
| 546 | $this->error = 'Failed to decompress gzip encoded content';  | 
            ||
| 547 | $this->resp_body = $r_body;  | 
            ||
| 548 | }  | 
            ||
| 549 |         }else{ | 
            ||
| 550 | $this->resp_body = $r_body;  | 
            ||
| 551 | }  | 
            ||
| 552 | |||
| 553 |         $this->_debug('response body',$this->resp_body); | 
            ||
| 554 | $this->redirect_count = 0;  | 
            ||
| 555 | return true;  | 
            ||
| 556 | }  | 
            ||
| 557 | |||
| 934 | 
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.