| Conditions | 25 | 
| Paths | 7560 | 
| Total Lines | 179 | 
| Code Lines | 93 | 
| Lines | 29 | 
| Ratio | 16.2 % | 
| 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 | ||
| 416 | public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [], | ||
| 417 | string $apiUrl = null): array | ||
| 418 |     { | ||
| 419 | /** Building url */ | ||
| 420 |         if (null === $apiUrl) { | ||
| 421 | $apiUrl = $this->getApiUrl(); | ||
| 422 | } | ||
| 423 | $url = $apiUrl . $url; | ||
| 424 | $query = []; | ||
| 425 | |||
| 426 | /** | ||
| 427 | * OAuth2 Key/Secret authentication | ||
| 428 | * | ||
| 429 | * @see https://developer.github.com/v3/#oauth2-keysecret | ||
| 430 | */ | ||
| 431 |         if (null !== $this->getClientId() && null !== $this->getClientSecret()) { | ||
| 432 | $query['client_id'] = $this->getClientId(); | ||
| 433 | $query['client_secret'] = $this->getClientSecret(); | ||
| 434 | } /** | ||
| 435 | * Basic authentication via OAuth2 Token (sent as a parameter) | ||
| 436 | * | ||
| 437 | * @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter | ||
| 438 |          */ else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) { | ||
| 439 | $query['access_token'] = $this->getToken(); | ||
| 440 | } | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Pagination | ||
| 444 | * Requests that return multiple items will be paginated to 30 items by default. | ||
| 445 | * You can specify further pages with the ?page parameter. | ||
| 446 | * For some resources, you can also set a custom page size up to 100 with the ?per_page parameter. | ||
| 447 | * Note that for technical reasons not all endpoints respect the ?per_page parameter, | ||
| 448 | * | ||
| 449 | * @see https://developer.github.com/v3/#pagination | ||
| 450 | */ | ||
| 451 |         if (null !== $this->getPagination()) { | ||
| 452 |             if (null !== $this->getPagination()->getPage()) { | ||
| 453 | $query['page'] = $this->getPagination()->getPage(); | ||
| 454 | } | ||
| 455 |             if (null !== $this->getPagination()->getLimit()) { | ||
| 456 | $query['per_page'] = $this->getPagination()->getLimit(); | ||
| 457 | } | ||
| 458 | } | ||
| 459 | |||
| 460 | /** | ||
| 461 | * Add URL-encoded query string parameters | ||
| 462 | */ | ||
| 463 |         if (!empty($query)) { | ||
| 464 | $url .= (strstr($url, '?') !== false ? '&' : '?'); | ||
| 465 | $url .= http_build_query($query); | ||
| 466 | } | ||
| 467 | |||
| 468 | /** Call curl */ | ||
| 469 | $curl = new CurlClient(); | ||
| 470 | $curl->setOption([ | ||
| 471 | CURLOPT_USERAGENT => self::USER_AGENT, | ||
| 472 | CURLOPT_TIMEOUT => $this->getTimeout(), | ||
| 473 | CURLOPT_HEADER => false, // Use $client->getHeaders() to get full header | ||
| 474 | CURLOPT_FOLLOWLOCATION => true, | ||
| 475 | CURLOPT_HTTPHEADER => [ | ||
| 476 | 'Accept: ' . $this->getAccept(), | ||
| 477 | 'Content-Type: ' . $this->getContentType() | ||
| 478 | ], | ||
| 479 | CURLOPT_URL => $url | ||
| 480 | ]); | ||
| 481 | |||
| 482 | /** | ||
| 483 | * Basic authentication via username and Password | ||
| 484 | * | ||
| 485 | * @see https://developer.github.com/v3/auth/#via-username-and-password | ||
| 486 | */ | ||
| 487 |         if (!empty($this->getHttpAuth())) { | ||
| 488 |             if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) { | ||
| 489 | $curl->setOption([ | ||
| 490 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | ||
| 491 | CURLOPT_USERPWD => $this->getHttpAuth()['username'] | ||
| 492 | ]); | ||
| 493 |             } else { | ||
| 494 | $curl->setOption([ | ||
| 495 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | ||
| 496 |                     CURLOPT_USERPWD  => sprintf('%s:%s', $this->getHttpAuth()['username'], | ||
| 497 | $this->getHttpAuth()['password']) | ||
| 498 | ]); | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 |         if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) { | ||
| 503 | /** | ||
| 504 | * Basic authentication via OAuth token | ||
| 505 | * | ||
| 506 | * @see https://developer.github.com/v3/auth/#via-oauth-tokens | ||
| 507 | **/ | ||
| 508 |             if ($this->getAuthentication() === self::OAUTH_AUTH) { | ||
| 509 | $curl->setOption([ | ||
| 510 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | ||
| 511 |                     CURLOPT_USERPWD  => sprintf('%s:x-oauth-basic', $this->getToken()) | ||
| 512 | ]); | ||
| 513 | } /** | ||
| 514 | * Basic authentication via OAuth2 Token (sent in a header) | ||
| 515 | * | ||
| 516 | * @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header | ||
| 517 |              */ else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) { | ||
| 518 | $curl->setOption([ | ||
| 519 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | ||
| 520 |                     CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())] | ||
| 521 | ]); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | /** Methods */ | ||
| 526 |         switch ($method) { | ||
| 527 | /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */ | ||
| 528 | case Request::METHOD_DELETE: | ||
|  | |||
| 529 | /** @see http://tools.ietf.org/html/rfc5789 */ | ||
| 530 | View Code Duplication | case Request::METHOD_PATCH: | |
| 531 | $curl->setOption([ | ||
| 532 | CURLOPT_CUSTOMREQUEST => $method, | ||
| 533 | CURLOPT_POST => true, | ||
| 534 | CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) | ||
| 535 | ]); | ||
| 536 | break; | ||
| 537 | |||
| 538 | /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */ | ||
| 539 | case Request::METHOD_GET: | ||
| 540 | $curl->setOption(CURLOPT_HTTPGET, true); | ||
| 541 | break; | ||
| 542 | |||
| 543 | /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */ | ||
| 544 | case Request::METHOD_HEAD: | ||
| 545 | $curl->setOption([ | ||
| 546 | CURLOPT_CUSTOMREQUEST => $method, | ||
| 547 | CURLOPT_NOBODY => true | ||
| 548 | ]); | ||
| 549 | break; | ||
| 550 | |||
| 551 | /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */ | ||
| 552 | View Code Duplication | case Request::METHOD_POST: | |
| 553 | $curl->setOption([ | ||
| 554 | CURLOPT_POST => true, | ||
| 555 | CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) | ||
| 556 | ]); | ||
| 557 | break; | ||
| 558 | |||
| 559 | /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */ | ||
| 560 | case Request::METHOD_PUT: | ||
| 561 | $curl->setOption([ | ||
| 562 | CURLOPT_CUSTOMREQUEST => $method, | ||
| 563 | CURLOPT_PUT => true, | ||
| 564 | CURLOPT_HTTPHEADER => [ | ||
| 565 | 'X-HTTP-Method-Override: PUT', | ||
| 566 | 'Content-type: application/x-www-form-urlencoded' | ||
| 567 | ] | ||
| 568 | ]); | ||
| 569 | break; | ||
| 570 | |||
| 571 | default: | ||
| 572 | break; | ||
| 573 | } | ||
| 574 | |||
| 575 | View Code Duplication |         $curl->success(function (CurlClient $instance) { | |
| 576 | $this->headers = $instance->getHeaders(); | ||
| 577 | $this->success = $instance->getResponse(); | ||
| 578 | $data = json_decode($this->success, true); | ||
| 579 |             if (JSON_ERROR_NONE === json_last_error()) { | ||
| 580 | $this->success = $data; | ||
| 581 | } | ||
| 582 | }); | ||
| 583 | View Code Duplication |         $curl->error(function (CurlClient $instance) { | |
| 584 | $this->headers = $instance->getHeaders(); | ||
| 585 | $this->failure = $instance->getResponse(); | ||
| 586 | $data = json_decode($this->failure, true); | ||
| 587 |             if (JSON_ERROR_NONE === json_last_error()) { | ||
| 588 | $this->failure = $data; | ||
| 589 | } | ||
| 590 | }); | ||
| 591 | $curl->perform(); | ||
| 592 | |||
| 593 | return (array)($this->success ?? $this->failure); | ||
| 594 | } | ||
| 595 | |||
| 634 | } |