Conditions | 13 |
Paths | 108 |
Total Lines | 64 |
Code Lines | 35 |
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 |
||
60 | public function request(string $method, string $url, array $options = []): ResponseInterface |
||
61 | { |
||
62 | if (isset($options['body'])) { |
||
63 | if (isset($options['headers'])) { |
||
64 | $options['headers'] = self::normalizeHeaders($options['headers']); |
||
65 | } |
||
66 | |||
67 | $json = false; |
||
68 | if (!isset($options['headers']['content-type'][0])) { |
||
69 | // Content-Type default to JSON-LD if a body is set, but no Content-Type is defined |
||
70 | $options['headers']['content-type'] = $options['headers']['content-type'] ?? ['application/ld+json']; |
||
71 | $json = true; |
||
72 | } |
||
73 | |||
74 | if ( |
||
75 | (\is_array($options['body']) || $options['body'] instanceof \JsonSerializable) && |
||
76 | ( |
||
77 | $json || |
||
78 | preg_match('/\bjson\b/i', $options['headers']['content-type'][0]) |
||
79 | ) |
||
80 | ) { |
||
81 | // Encode the JSON |
||
82 | $options['json'] = $options['body']; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // TODO: remove when https://github.com/symfony/symfony/pull/30547 will be merged |
||
87 | if (isset($options['bearer']) && \is_string($options['bearer'])) { |
||
88 | $options['headers']['authorization'] = ['Bearer '.$options['bearer']]; |
||
89 | } |
||
90 | |||
91 | $basic = $options['auth'] ?? null; |
||
92 | [$url, $options] = $this->prepareRequest($method, $url, $options, self::OPTIONS_DEFAULT); |
||
93 | |||
94 | $server = []; |
||
95 | // Convert headers to a $_SERVER-like array |
||
96 | foreach ($options['headers'] as $key => $value) { |
||
97 | if ('content-type' === $key) { |
||
98 | $server['CONTENT_TYPE'] = $value[0] ?? ''; |
||
99 | |||
100 | continue; |
||
101 | } |
||
102 | |||
103 | // BrowserKit doesn't support setting several headers with the same name |
||
104 | $server['HTTP_'.strtoupper(str_replace('-', '_', $key))] = $value[0] ?? ''; |
||
105 | } |
||
106 | |||
107 | if ($basic) { |
||
108 | $credentials = explode(':', $basic, 2); |
||
109 | $server['PHP_AUTH_USER'] = $credentials[0]; |
||
110 | $server['PHP_AUTH_PW'] = $credentials[1] ?? ''; |
||
111 | } |
||
112 | |||
113 | $info = [ |
||
114 | 'redirect_count' => 0, |
||
115 | 'redirect_url' => null, |
||
116 | 'http_method' => $method, |
||
117 | 'start_time' => microtime(true), |
||
118 | 'data' => $options['data'] ?? null, |
||
119 | 'url' => $url, |
||
120 | ]; |
||
121 | $this->fwbClient->request($method, implode('', $url), [], [], $server, $options['body'] ?? null); |
||
122 | |||
123 | return new Response($this->fwbClient->getResponse(), $this->fwbClient->getInternalResponse(), $info); |
||
124 | } |
||
203 |