| Conditions | 39 |
| Paths | 511 |
| Total Lines | 169 |
| Code Lines | 95 |
| 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 |
||
| 315 | public function read() |
||
| 316 | { |
||
| 317 | // First, read headers only |
||
| 318 | $response = ''; |
||
| 319 | $gotStatus = false; |
||
| 320 | |||
| 321 | while (($line = @fgets($this->socket)) !== false) { |
||
| 322 | $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false); |
||
| 323 | if ($gotStatus) { |
||
| 324 | $response .= $line; |
||
| 325 | if (rtrim($line) === '') break; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | $this->_checkSocketReadTimeout(); |
||
| 330 | |||
| 331 | $statusCode = Zend_Http_Response::extractCode($response); |
||
| 332 | |||
| 333 | // Handle 100 and 101 responses internally by restarting the read again |
||
| 334 | if ($statusCode == 100 || $statusCode == 101) return $this->read(); |
||
| 335 | |||
| 336 | // Check headers to see what kind of connection / transfer encoding we have |
||
| 337 | $headers = Zend_Http_Response::extractHeaders($response); |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Responses to HEAD requests and 204 or 304 responses are not expected |
||
| 341 | * to have a body - stop reading here |
||
| 342 | */ |
||
| 343 | if ($statusCode == 304 || $statusCode == 204 || |
||
| 344 | $this->method == Zend_Http_Client::HEAD) { |
||
| 345 | |||
| 346 | // Close the connection if requested to do so by the server |
||
| 347 | if (isset($headers['connection']) && $headers['connection'] == 'close') { |
||
| 348 | $this->close(); |
||
| 349 | } |
||
| 350 | return $response; |
||
| 351 | } |
||
| 352 | |||
| 353 | // If we got a 'transfer-encoding: chunked' header |
||
| 354 | if (isset($headers['transfer-encoding'])) { |
||
| 355 | |||
| 356 | if (strtolower($headers['transfer-encoding']) == 'chunked') { |
||
| 357 | |||
| 358 | do { |
||
| 359 | $line = @fgets($this->socket); |
||
| 360 | $this->_checkSocketReadTimeout(); |
||
| 361 | |||
| 362 | $chunk = $line; |
||
| 363 | |||
| 364 | // Figure out the next chunk size |
||
| 365 | $chunksize = trim($line); |
||
| 366 | if (! ctype_xdigit($chunksize)) { |
||
| 367 | $this->close(); |
||
| 368 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 369 | throw new Zend_Http_Client_Adapter_Exception('Invalid chunk size "' . |
||
| 370 | $chunksize . '" unable to read chunked body'); |
||
| 371 | } |
||
| 372 | |||
| 373 | // Convert the hexadecimal value to plain integer |
||
| 374 | $chunksize = hexdec($chunksize); |
||
| 375 | |||
| 376 | // Read next chunk |
||
| 377 | $read_to = ftell($this->socket) + $chunksize; |
||
| 378 | |||
| 379 | do { |
||
| 380 | $current_pos = ftell($this->socket); |
||
| 381 | if ($current_pos >= $read_to) break; |
||
| 382 | |||
| 383 | if($this->out_stream) { |
||
| 384 | if(stream_copy_to_stream($this->socket, $this->out_stream, $read_to - $current_pos) == 0) { |
||
| 385 | $this->_checkSocketReadTimeout(); |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | } else { |
||
| 389 | $line = @fread($this->socket, $read_to - $current_pos); |
||
| 390 | if ($line === false || strlen($line) === 0) { |
||
| 391 | $this->_checkSocketReadTimeout(); |
||
| 392 | break; |
||
| 393 | } |
||
| 394 | $chunk .= $line; |
||
| 395 | } |
||
| 396 | } while (! feof($this->socket)); |
||
| 397 | |||
| 398 | $chunk .= @fgets($this->socket); |
||
| 399 | $this->_checkSocketReadTimeout(); |
||
| 400 | |||
| 401 | if(!$this->out_stream) { |
||
| 402 | $response .= $chunk; |
||
| 403 | } |
||
| 404 | } while ($chunksize > 0); |
||
| 405 | } else { |
||
| 406 | $this->close(); |
||
| 407 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 408 | throw new Zend_Http_Client_Adapter_Exception('Cannot handle "' . |
||
| 409 | $headers['transfer-encoding'] . '" transfer encoding'); |
||
| 410 | } |
||
| 411 | |||
| 412 | // We automatically decode chunked-messages when writing to a stream |
||
| 413 | // this means we have to disallow the Zend_Http_Response to do it again |
||
| 414 | if ($this->out_stream) { |
||
| 415 | $response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response); |
||
| 416 | } |
||
| 417 | // Else, if we got the content-length header, read this number of bytes |
||
| 418 | } elseif (isset($headers['content-length'])) { |
||
| 419 | |||
| 420 | // If we got more than one Content-Length header (see ZF-9404) use |
||
| 421 | // the last value sent |
||
| 422 | if (is_array($headers['content-length'])) { |
||
| 423 | $contentLength = $headers['content-length'][count($headers['content-length']) - 1]; |
||
| 424 | } else { |
||
| 425 | $contentLength = $headers['content-length']; |
||
| 426 | } |
||
| 427 | |||
| 428 | $current_pos = ftell($this->socket); |
||
| 429 | $chunk = ''; |
||
| 430 | |||
| 431 | for ($read_to = $current_pos + $contentLength; |
||
| 432 | $read_to > $current_pos; |
||
| 433 | $current_pos = ftell($this->socket)) { |
||
| 434 | |||
| 435 | if($this->out_stream) { |
||
| 436 | if(@stream_copy_to_stream($this->socket, $this->out_stream, $read_to - $current_pos) == 0) { |
||
| 437 | $this->_checkSocketReadTimeout(); |
||
| 438 | break; |
||
| 439 | } |
||
| 440 | } else { |
||
| 441 | $chunk = @fread($this->socket, $read_to - $current_pos); |
||
| 442 | if ($chunk === false || strlen($chunk) === 0) { |
||
| 443 | $this->_checkSocketReadTimeout(); |
||
| 444 | break; |
||
| 445 | } |
||
| 446 | |||
| 447 | $response .= $chunk; |
||
| 448 | } |
||
| 449 | |||
| 450 | // Break if the connection ended prematurely |
||
| 451 | if (feof($this->socket)) break; |
||
| 452 | } |
||
| 453 | |||
| 454 | // Fallback: just read the response until EOF |
||
| 455 | } else { |
||
| 456 | |||
| 457 | do { |
||
| 458 | if($this->out_stream) { |
||
| 459 | if(@stream_copy_to_stream($this->socket, $this->out_stream) == 0) { |
||
| 460 | $this->_checkSocketReadTimeout(); |
||
| 461 | break; |
||
| 462 | } |
||
| 463 | } else { |
||
| 464 | $buff = @fread($this->socket, 8192); |
||
| 465 | if ($buff === false || strlen($buff) === 0) { |
||
| 466 | $this->_checkSocketReadTimeout(); |
||
| 467 | break; |
||
| 468 | } else { |
||
| 469 | $response .= $buff; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | } while (feof($this->socket) === false); |
||
| 474 | |||
| 475 | $this->close(); |
||
| 476 | } |
||
| 477 | |||
| 478 | // Close the connection if requested to do so by the server |
||
| 479 | if (isset($headers['connection']) && $headers['connection'] == 'close') { |
||
| 480 | $this->close(); |
||
| 481 | } |
||
| 482 | |||
| 483 | return $response; |
||
| 484 | } |
||
| 544 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths