Conditions | 12 |
Paths | 96 |
Total Lines | 43 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
45 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
46 | { |
||
47 | $response = $handler->handle($request); |
||
48 | $layout = $this->defaultLayout; |
||
49 | |||
50 | if ($response instanceof LayoutResponse) { |
||
51 | $layout = $response->getLayout(); |
||
52 | } elseif ($response->hasHeader('layout')) { |
||
53 | $layout = $response->getHeader('layout')[0]; |
||
54 | $layout = $layout === 'none' ? false : $layout; |
||
55 | $response = $response->withoutHeader('header'); |
||
56 | } |
||
57 | |||
58 | $contentType = $response->getHeader('Content-Type'); |
||
59 | $isHtmlResponse = $response instanceof HtmlResponse || get_class($response) === 'Laminas\Diactoros\Response\HtmlResponse'; |
||
60 | $hasHtmlContent = count($contentType) ? strstr($contentType[0], 'text/html') : true; |
||
61 | |||
62 | if ((!$isHtmlResponse || !$hasHtmlContent || !$layout)) { |
||
63 | return $response; |
||
64 | } |
||
65 | |||
66 | $body = [ |
||
67 | 'content' => $response->getBody()->getContents(), |
||
68 | 'config' => $this->config, |
||
69 | 'user' => null, |
||
70 | 'vars' => [], |
||
71 | ]; |
||
72 | |||
73 | if ($response instanceof Response) { |
||
74 | |||
75 | if ($response->hasAttribute('user')) { |
||
76 | $body['user'] = $response->getAttribute('user'); |
||
77 | } |
||
78 | |||
79 | if ($response->hasAttribute('vars')) { |
||
80 | $body['vars'] = $response->getAttribute('vars'); |
||
81 | } |
||
82 | |||
83 | } |
||
84 | |||
85 | $body = $this->viewEngine->render($layout, $body); |
||
86 | |||
87 | return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode()); |
||
88 | } |
||
105 | } |