| Conditions | 12 |
| Paths | 14 |
| Total Lines | 56 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 13 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 62 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 63 | { |
||
| 64 | if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { |
||
| 65 | throw new RuntimeException('This middleware needs FormatNegotiator executed before'); |
||
| 66 | } |
||
| 67 | |||
| 68 | $renderer = $this->debugBar->getJavascriptRenderer(); |
||
| 69 | |||
| 70 | //Is an asset? |
||
| 71 | $path = $request->getUri()->getPath(); |
||
| 72 | $renderPath = $renderer->getBaseUrl(); |
||
| 73 | |||
| 74 | if (strpos($path, $renderPath) === 0) { |
||
| 75 | $file = $renderer->getBasePath().substr($path, strlen($renderPath)); |
||
| 76 | |||
| 77 | if (file_exists($file)) { |
||
| 78 | $body = Middleware::createStream(); |
||
| 79 | $body->write(file_get_contents($file)); |
||
| 80 | |||
| 81 | return $response->withBody($body); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $response = $next($request, $response); |
||
| 86 | |||
| 87 | //Fix the render baseUrl |
||
| 88 | $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl()); |
||
| 89 | $renderer->setBaseUrl($renderPath); |
||
| 90 | |||
| 91 | $ajax = Utils\Helpers::isAjax($request); |
||
| 92 | |||
| 93 | //Redirection response |
||
| 94 | if (Utils\Helpers::isRedirect($response)) { |
||
| 95 | if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { |
||
| 96 | $this->debugBar->stackData(); |
||
| 97 | } |
||
| 98 | |||
| 99 | //Html response |
||
| 100 | } elseif (FormatNegotiator::getFormat($request) === 'html') { |
||
| 101 | if (!$ajax) { |
||
| 102 | $response = $this->inject($response, $renderer->renderHead(), 'head'); |
||
| 103 | } |
||
| 104 | |||
| 105 | $response = $this->inject($response, $renderer->render(!$ajax), 'body'); |
||
| 106 | |||
| 107 | //Ajax response |
||
| 108 | } elseif ($ajax && $this->captureAjax) { |
||
| 109 | $headers = $this->debugBar->getDataAsHeaders(); |
||
| 110 | |||
| 111 | foreach ($headers as $name => $value) { |
||
| 112 | $response = $response->withHeader($name, $value); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $response; |
||
| 117 | } |
||
| 118 | } |
||
| 119 |