| Conditions | 10 |
| Paths | 13 |
| Total Lines | 40 |
| Code Lines | 20 |
| 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 |
||
| 34 | public function __invoke(RequestInterface $request, array $options) |
||
| 35 | { |
||
| 36 | $fn = $this->nextHandler; |
||
| 37 | |||
| 38 | // Don't do anything if the request has no body. |
||
| 39 | if (isset(self::$skipMethods[$request->getMethod()]) |
||
| 40 | || $request->getBody()->getSize() === 0 |
||
| 41 | ) { |
||
| 42 | return $fn($request, $options); |
||
| 43 | } |
||
| 44 | |||
| 45 | $modify = []; |
||
| 46 | |||
| 47 | // Add a default content-type if possible. |
||
| 48 | if (!$request->hasHeader('Content-Type')) { |
||
| 49 | if ($uri = $request->getBody()->getMetadata('uri')) { |
||
| 50 | if ($type = Psr7\mimetype_from_filename($uri)) { |
||
| 51 | $modify['set_headers']['Content-Type'] = $type; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | // Add a default content-length or transfer-encoding header. |
||
| 57 | if (!isset(self::$skipMethods[$request->getMethod()]) |
||
| 58 | && !$request->hasHeader('Content-Length') |
||
| 59 | && !$request->hasHeader('Transfer-Encoding') |
||
| 60 | ) { |
||
| 61 | $size = $request->getBody()->getSize(); |
||
| 62 | if ($size !== null) { |
||
| 63 | $modify['set_headers']['Content-Length'] = $size; |
||
| 64 | } else { |
||
| 65 | $modify['set_headers']['Transfer-Encoding'] = 'chunked'; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | // Add the expect header if needed. |
||
| 70 | $this->addExpectHeader($request, $options, $modify); |
||
| 71 | |||
| 72 | return $fn(Psr7\modify_request($request, $modify), $options); |
||
| 73 | } |
||
| 74 | |||
| 113 |