| Conditions | 40 |
| Paths | 13486 |
| Total Lines | 201 |
| Code Lines | 114 |
| 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 |
||
| 272 | public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '') |
||
| 273 | { |
||
| 274 | // Make sure we're properly connected |
||
| 275 | if (!$this->_curl) { |
||
| 276 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 277 | throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected"); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) { |
||
| 281 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 282 | throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong host"); |
||
| 283 | } |
||
| 284 | |||
| 285 | // set URL |
||
| 286 | curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString()); |
||
| 287 | |||
| 288 | // ensure correct curl call |
||
| 289 | $curlValue = true; |
||
| 290 | switch ($method) { |
||
| 291 | case Zend_Http_Client::GET: |
||
| 292 | $curlMethod = CURLOPT_HTTPGET; |
||
| 293 | break; |
||
| 294 | |||
| 295 | case Zend_Http_Client::POST: |
||
| 296 | $curlMethod = CURLOPT_POST; |
||
| 297 | break; |
||
| 298 | |||
| 299 | case Zend_Http_Client::PUT: |
||
| 300 | // There are two different types of PUT request, either a Raw Data string has been set |
||
| 301 | // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used. |
||
| 302 | if(is_resource($body)) { |
||
| 303 | $this->_config['curloptions'][CURLOPT_INFILE] = $body; |
||
| 304 | } |
||
| 305 | if (isset($this->_config['curloptions'][CURLOPT_INFILE])) { |
||
| 306 | // Now we will probably already have Content-Length set, so that we have to delete it |
||
| 307 | // from $headers at this point: |
||
| 308 | foreach ($headers AS $k => $header) { |
||
| 309 | if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) { |
||
| 310 | if(is_resource($body)) { |
||
| 311 | $this->_config['curloptions'][CURLOPT_INFILESIZE] = (int)$m[1]; |
||
| 312 | } |
||
| 313 | unset($headers[$k]); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) { |
||
| 318 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 319 | throw new Zend_Http_Client_Adapter_Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE."); |
||
| 320 | } |
||
| 321 | |||
| 322 | if(is_resource($body)) { |
||
| 323 | $body = ''; |
||
| 324 | } |
||
| 325 | |||
| 326 | $curlMethod = CURLOPT_PUT; |
||
| 327 | } else { |
||
| 328 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 329 | $curlValue = "PUT"; |
||
| 330 | } |
||
| 331 | break; |
||
| 332 | |||
| 333 | case Zend_Http_Client::PATCH: |
||
| 334 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 335 | $curlValue = "PATCH"; |
||
| 336 | break; |
||
| 337 | |||
| 338 | case Zend_Http_Client::DELETE: |
||
| 339 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 340 | $curlValue = "DELETE"; |
||
| 341 | break; |
||
| 342 | |||
| 343 | case Zend_Http_Client::OPTIONS: |
||
| 344 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 345 | $curlValue = "OPTIONS"; |
||
| 346 | break; |
||
| 347 | |||
| 348 | case Zend_Http_Client::TRACE: |
||
| 349 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 350 | $curlValue = "TRACE"; |
||
| 351 | break; |
||
| 352 | |||
| 353 | case Zend_Http_Client::HEAD: |
||
| 354 | $curlMethod = CURLOPT_CUSTOMREQUEST; |
||
| 355 | $curlValue = "HEAD"; |
||
| 356 | break; |
||
| 357 | |||
| 358 | default: |
||
| 359 | // For now, through an exception for unsupported request methods |
||
| 360 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 361 | throw new Zend_Http_Client_Adapter_Exception("Method currently not supported"); |
||
| 362 | } |
||
| 363 | |||
| 364 | if(is_resource($body) && $curlMethod != CURLOPT_PUT) { |
||
| 365 | require_once 'Zend/Http/Client/Adapter/Exception.php'; |
||
| 366 | throw new Zend_Http_Client_Adapter_Exception("Streaming requests are allowed only with PUT"); |
||
| 367 | } |
||
| 368 | |||
| 369 | // get http version to use |
||
| 370 | $curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0; |
||
| 371 | |||
| 372 | // mark as HTTP request and set HTTP method |
||
| 373 | curl_setopt($this->_curl, CURLOPT_HTTP_VERSION, $curlHttp); |
||
| 374 | curl_setopt($this->_curl, $curlMethod, $curlValue); |
||
| 375 | |||
| 376 | if($this->out_stream) { |
||
| 377 | // headers will be read into the response |
||
| 378 | curl_setopt($this->_curl, CURLOPT_HEADER, false); |
||
| 379 | curl_setopt($this->_curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader")); |
||
| 380 | // and data will be written into the file |
||
| 381 | curl_setopt($this->_curl, CURLOPT_FILE, $this->out_stream); |
||
| 382 | } else { |
||
| 383 | // ensure headers are also returned |
||
| 384 | curl_setopt($this->_curl, CURLOPT_HEADER, true); |
||
| 385 | curl_setopt($this->_curl, CURLINFO_HEADER_OUT, true); |
||
| 386 | |||
| 387 | // ensure actual response is returned |
||
| 388 | curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true); |
||
| 389 | } |
||
| 390 | |||
| 391 | // set additional headers |
||
| 392 | $headers['Accept'] = ''; |
||
| 393 | curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers); |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Make sure POSTFIELDS is set after $curlMethod is set: |
||
| 397 | * @link http://de2.php.net/manual/en/function.curl-setopt.php#81161 |
||
| 398 | */ |
||
| 399 | if ($method == Zend_Http_Client::POST) { |
||
| 400 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body); |
||
| 401 | } elseif ($curlMethod == CURLOPT_PUT) { |
||
| 402 | // this covers a PUT by file-handle: |
||
| 403 | // Make the setting of this options explicit (rather than setting it through the loop following a bit lower) |
||
| 404 | // to group common functionality together. |
||
| 405 | curl_setopt($this->_curl, CURLOPT_INFILE, $this->_config['curloptions'][CURLOPT_INFILE]); |
||
| 406 | curl_setopt($this->_curl, CURLOPT_INFILESIZE, $this->_config['curloptions'][CURLOPT_INFILESIZE]); |
||
| 407 | unset($this->_config['curloptions'][CURLOPT_INFILE]); |
||
| 408 | unset($this->_config['curloptions'][CURLOPT_INFILESIZE]); |
||
| 409 | } elseif ($method == Zend_Http_Client::PUT) { |
||
| 410 | // This is a PUT by a setRawData string, not by file-handle |
||
| 411 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body); |
||
| 412 | } elseif ($method == Zend_Http_Client::PATCH) { |
||
| 413 | // This is a PATCH by a setRawData string |
||
| 414 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body); |
||
| 415 | } elseif ($method == Zend_Http_Client::DELETE) { |
||
| 416 | // This is a DELETE by a setRawData string |
||
| 417 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body); |
||
| 418 | } elseif ($method == Zend_Http_Client::OPTIONS) { |
||
| 419 | // This is an OPTIONS by a setRawData string |
||
| 420 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body); |
||
| 421 | } |
||
| 422 | |||
| 423 | // set additional curl options |
||
| 424 | if (isset($this->_config['curloptions'])) { |
||
| 425 | foreach ((array)$this->_config['curloptions'] as $k => $v) { |
||
| 426 | if (!in_array($k, $this->_invalidOverwritableCurlOptions)) { |
||
| 427 | if (curl_setopt($this->_curl, $k, $v) == false) { |
||
| 428 | require_once 'Zend/Http/Client/Exception.php'; |
||
| 429 | throw new Zend_Http_Client_Exception(sprintf("Unknown or erroreous cURL option '%s' set", $k)); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | // send the request |
||
| 436 | $response = curl_exec($this->_curl); |
||
| 437 | |||
| 438 | // if we used streaming, headers are already there |
||
| 439 | if(!is_resource($this->out_stream)) { |
||
| 440 | $this->_response = $response; |
||
| 441 | } |
||
| 442 | |||
| 443 | $request = curl_getinfo($this->_curl, CURLINFO_HEADER_OUT); |
||
| 444 | $request .= $body; |
||
| 445 | |||
| 446 | if (empty($this->_response)) { |
||
| 447 | require_once 'Zend/Http/Client/Exception.php'; |
||
| 448 | throw new Zend_Http_Client_Exception("Error in cURL request: " . curl_error($this->_curl)); |
||
| 449 | } |
||
| 450 | |||
| 451 | // cURL automatically decodes chunked-messages, this means we have to disallow the Zend_Http_Response to do it again |
||
| 452 | if (stripos($this->_response, "Transfer-Encoding: chunked\r\n")) { |
||
| 453 | $this->_response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->_response); |
||
| 454 | } |
||
| 455 | |||
| 456 | // Eliminate multiple HTTP responses. |
||
| 457 | do { |
||
| 458 | $parts = preg_split('|(?:\r?\n){2}|m', $this->_response, 2); |
||
| 459 | $again = false; |
||
| 460 | |||
| 461 | if (isset($parts[1]) && preg_match("|^HTTP/1\.[01](.*?)\r\n|mi", $parts[1])) { |
||
| 462 | $this->_response = $parts[1]; |
||
| 463 | $again = true; |
||
| 464 | } |
||
| 465 | } while ($again); |
||
| 466 | |||
| 467 | // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string: |
||
| 468 | if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) { |
||
| 469 | $this->_response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->_response); |
||
| 470 | } |
||
| 471 | |||
| 472 | return $request; |
||
| 473 | } |
||
| 533 |
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