| Conditions | 12 |
| Paths | 54 |
| Total Lines | 56 |
| Code Lines | 30 |
| 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 |
||
| 59 | public function request(string $method, string $url, array $options = []): ResponseInterface |
||
| 60 | { |
||
| 61 | if (isset($options['body'])) { |
||
| 62 | if (isset($options['headers'])) { |
||
| 63 | $options['headers'] = self::normalizeHeaders($options['headers']); |
||
| 64 | } |
||
| 65 | |||
| 66 | $json = false; |
||
| 67 | if (!isset($options['headers']['content-type'][0])) { |
||
| 68 | // Content-Type default to JSON-LD if a body is set, but no Content-Type is defined |
||
| 69 | $options['headers']['content-type'] = $options['headers']['content-type'] ?? ['application/ld+json']; |
||
| 70 | $json = true; |
||
| 71 | } |
||
| 72 | |||
| 73 | if ( |
||
| 74 | (\is_array($options['body']) || $options['body'] instanceof \JsonSerializable) && |
||
| 75 | ( |
||
| 76 | $json || |
||
| 77 | false !== preg_match('#^application/(?:.+\+)?json$#', $options['headers']['content-type'][0]) |
||
| 78 | ) |
||
| 79 | ) { |
||
| 80 | // Encode the JSON |
||
| 81 | $options['json'] = $options['body']; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | if (isset($options['bearer']) && \is_string($options['bearer'])) { |
||
| 86 | $options['headers']['authorization'] = ['Bearer '.$options['bearer']]; |
||
| 87 | } |
||
| 88 | |||
| 89 | [$url, $options] = $this->prepareRequest($method, $url, $options, self::OPTIONS_DEFAULT); |
||
| 90 | |||
| 91 | $server = []; |
||
| 92 | // Convert headers to a $_SERVER-like array |
||
| 93 | foreach ($options['headers'] as $key => $value) { |
||
| 94 | if ('content-type' === strtolower($key)) { |
||
| 95 | $server['CONTENT_TYPE'] = $value[0] ?? ''; |
||
| 96 | |||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | // BrowserKit doesn't support setting several headers with the same name |
||
| 101 | $server['HTTP_'.strtoupper(str_replace('-', '_', $key))] = $value[0] ?? ''; |
||
| 102 | } |
||
| 103 | |||
| 104 | $info = [ |
||
| 105 | 'redirect_count' => 0, |
||
| 106 | 'redirect_url' => null, |
||
| 107 | 'http_method' => $method, |
||
| 108 | 'start_time' => microtime(true), |
||
| 109 | 'data' => $options['data'] ?? null, |
||
| 110 | 'url' => $url, |
||
| 111 | ]; |
||
| 112 | $this->fwbClient->request($method, implode('', $url), [], [], $server, $options['body'] ?? null); |
||
| 113 | |||
| 114 | return new Response($this->fwbClient->getResponse(), $this->fwbClient->getInternalResponse(), $info); |
||
| 115 | } |
||
| 194 |