Conditions | 20 |
Paths | 503 |
Total Lines | 92 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
180 | private function createRequest(string $method, UriInterface $uri, array $options): RequestInterface // phpcs:ignore |
||
181 | { |
||
182 | $request = $this->requestFactory->createRequest($method, $uri); |
||
183 | $headers = $options['headers'] ?? []; |
||
184 | |||
185 | if (!is_array($headers)) { |
||
186 | throw new InvalidArgumentException('Invalid value for "headers" option'); |
||
187 | } |
||
188 | |||
189 | // default headers |
||
190 | $headers['Accept'] = $headers['Accept'] ?? 'application/json'; |
||
191 | |||
192 | if (isset($options['user-agent'])) { |
||
193 | $headers['User-Agent'] = (string)$options['user-agent']; |
||
194 | } |
||
195 | |||
196 | if (isset($options['auth_basic'])) { |
||
197 | if (is_array($options['auth_basic'])) { |
||
198 | $options['auth_basic'] = implode(':', $options['auth_basic']); |
||
199 | } |
||
200 | |||
201 | if (!is_string($options['auth_basic'])) { |
||
202 | throw new InvalidArgumentException('Invalid value for "auth_basic" option'); |
||
203 | } |
||
204 | |||
205 | $headers['Authorization'] = 'Basic ' . base64_encode($options['auth_basic']); |
||
206 | } |
||
207 | |||
208 | if (isset($options['auth_bearer'])) { |
||
209 | if (!is_string($options['auth_bearer'])) { |
||
210 | throw new InvalidArgumentException('Invalid value for "auth_bearer" option'); |
||
211 | } |
||
212 | |||
213 | $headers['Authorization'] = 'Bearer ' . $options['auth_bearer']; |
||
214 | } |
||
215 | |||
216 | if (isset($options['multipart'])) { |
||
217 | if (!is_array($options['multipart'])) { |
||
218 | throw new InvalidArgumentException('Invalid value for "multipart" option'); |
||
219 | } |
||
220 | |||
221 | $multipartBuilder = new MultipartStreamBuilder($this->streamFactory); |
||
222 | foreach ($options['multipart'] as $name => $resource) { |
||
223 | $resourceOptions = []; |
||
224 | |||
225 | if ($resource instanceof FileStream) { |
||
226 | $resourceOptions['filename'] = $resource->getFilename(); |
||
227 | $resource = $resource->getHandle(); |
||
228 | } |
||
229 | |||
230 | $multipartBuilder->addResource($name, $resource, $resourceOptions); |
||
231 | $headers['Content-Type'] = sprintf( |
||
232 | 'multipart/form-data; boundary="%s"', |
||
233 | $multipartBuilder->getBoundary() |
||
234 | ); |
||
235 | $options['body'] = $multipartBuilder->build(); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | if (isset($options['json'])) { |
||
240 | if (!is_array($options['json'])) { |
||
241 | throw new InvalidArgumentException('Invalid value for "json" option'); |
||
242 | } |
||
243 | |||
244 | $headers['Content-Type'] = 'application/json'; |
||
245 | $json = self::normalizeJson($options['json']); |
||
246 | try { |
||
247 | $options['body'] = self::jsonEncode($json); |
||
248 | } catch (JsonException $e) { |
||
249 | throw new InvalidArgumentException('Invalid value for "json" option: ' . $e->getMessage()); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | if (isset($options['body'])) { |
||
254 | if (is_resource($options['body'])) { |
||
255 | $body = $this->streamFactory->createStreamFromResource($options['body']); |
||
256 | } elseif (is_string($options['body'])) { |
||
257 | $body = $this->streamFactory->createStream($options['body']); |
||
258 | } elseif ($options['body'] instanceof StreamInterface) { |
||
259 | $body = $options['body']; |
||
260 | } else { |
||
261 | throw new InvalidArgumentException('Invalid value for "body" option'); |
||
262 | } |
||
263 | |||
264 | $request = $request->withBody($body); |
||
265 | } |
||
266 | |||
267 | foreach ($headers as $name => $value) { |
||
268 | $request = $request->withHeader($name, $value); |
||
269 | } |
||
270 | |||
271 | return $request; |
||
272 | } |
||
321 |