Conditions | 12 |
Paths | 81 |
Total Lines | 59 |
Code Lines | 33 |
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 |
||
62 | public function request(string $method, string $url, array $options = []): ResponseInterface |
||
63 | { |
||
64 | if (isset($options['body'])) { |
||
65 | if (isset($options['headers'])) { |
||
66 | $options['headers'] = self::normalizeHeaders($options['headers']); |
||
67 | } |
||
68 | |||
69 | $json = false; |
||
70 | if (!isset($options['headers']['content-type'][0])) { |
||
71 | // Content-Type default to JSON-LD if a body is set, but no Content-Type is defined |
||
72 | $options['headers']['content-type'] = $options['headers']['content-type'] ?? ['application/ld+json']; |
||
73 | $json = true; |
||
74 | } |
||
75 | |||
76 | if ( |
||
77 | (\is_array($options['body']) || $options['body'] instanceof \JsonSerializable) && |
||
78 | ( |
||
79 | $json || |
||
80 | preg_match('/\bjson\b/i', $options['headers']['content-type'][0]) |
||
81 | ) |
||
82 | ) { |
||
83 | // Encode the JSON |
||
84 | $options['json'] = $options['body']; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | $basic = $options['auth_basic'] ?? null; |
||
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' === $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 | if ($basic) { |
||
105 | $credentials = is_array($basic) ? $basic : explode(':', $basic, 2); |
||
106 | $server['PHP_AUTH_USER'] = $credentials[0]; |
||
107 | $server['PHP_AUTH_PW'] = $credentials[1] ?? ''; |
||
108 | } |
||
109 | |||
110 | $info = [ |
||
111 | 'redirect_count' => 0, |
||
112 | 'redirect_url' => null, |
||
113 | 'http_method' => $method, |
||
114 | 'start_time' => microtime(true), |
||
115 | 'data' => $options['data'] ?? null, |
||
116 | 'url' => $url, |
||
117 | ]; |
||
118 | $this->fwbClient->request($method, implode('', $url), [], [], $server, $options['body'] ?? null); |
||
119 | |||
120 | return new Response($this->fwbClient->getResponse(), $this->fwbClient->getInternalResponse(), $info); |
||
121 | } |
||
200 |