| Conditions | 10 |
| Paths | 3 |
| Total Lines | 36 |
| Code Lines | 19 |
| 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 |
||
| 67 | public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
||
| 68 | { |
||
| 69 | $handler = $request->getAttribute($this->attribute); |
||
| 70 | |||
| 71 | if (!is_callable($handler)) { |
||
| 72 | return $delegate->process($request); |
||
| 73 | } |
||
| 74 | |||
| 75 | $converter = $this->converter ?: function ($value) { |
||
| 76 | if ((is_array($value) || is_object($value)) && !($value instanceof \Closure)) { |
||
| 77 | $response = Utils\Factory::createResponse(200); |
||
| 78 | $response->getBody()->write(json_encode($value)); |
||
| 79 | |||
| 80 | return $response->withHeader('content-type', 'application/json'); |
||
| 81 | } |
||
| 82 | throw new \RuntimeException("Incompatible return value returned in the route handler."); |
||
| 83 | }; |
||
| 84 | |||
| 85 | $handlerWrapper = function (ServerRequestInterface $request, ...$args) use ($handler, $converter) { |
||
| 86 | $result = call_user_func($handler, $request, ...$args); |
||
| 87 | |||
| 88 | if (($result instanceof ResponseInterface) || is_scalar($result)) { |
||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | |||
| 92 | if (is_object($result) && method_exists($result, '__toString')) { |
||
| 93 | return $result->__toString(); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $converter($result); |
||
| 97 | }; |
||
| 98 | |||
| 99 | $request = $request->withAttribute($this->attribute, $handlerWrapper); |
||
| 100 | |||
| 101 | return $delegate->process($request); |
||
| 102 | } |
||
| 103 | } |
||
| 104 |