| Conditions | 11 |
| Paths | 14 |
| Total Lines | 35 |
| Code Lines | 24 |
| 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 |
||
| 28 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 29 | { |
||
| 30 | $newScheme = null; |
||
| 31 | foreach ($this->protocolHeaders as $header => $data) { |
||
| 32 | if (!$request->hasHeader($header)) { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | $headerValues = $request->getHeader($header); |
||
| 36 | if (is_callable($data)) { |
||
| 37 | $newScheme = $data($headerValues, $header, $request); |
||
| 38 | if ($newScheme === null) { |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | if (!is_string($newScheme)) { |
||
| 42 | throw new \RuntimeException('The scheme is neither string nor null!'); |
||
| 43 | } |
||
| 44 | if ($newScheme === '') { |
||
| 45 | throw new \RuntimeException('The scheme cannot be an empty string!'); |
||
| 46 | } |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | $headerValue = strtolower($headerValues[0]); |
||
| 50 | foreach ($data as $protocol => $acceptedValues) { |
||
| 51 | if (!in_array($headerValue, $acceptedValues, true)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | $newScheme = $protocol; |
||
| 55 | break 2; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | $uri = $request->getUri(); |
||
| 59 | if ($newScheme !== null && $newScheme !== $uri->getScheme()) { |
||
| 60 | $request = $request->withUri($uri->withScheme($newScheme)); |
||
| 61 | } |
||
| 62 | return $handler->handle($request); |
||
| 63 | } |
||
| 142 |