| Conditions | 10 |
| Paths | 11 |
| Total Lines | 43 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| 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 |
||
| 71 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 72 | { |
||
| 73 | if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { |
||
| 74 | throw new RuntimeException('This middleware needs FormatNegotiator executed before'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $ajax = Utils\Helpers::isAjax($request); |
||
| 78 | $debugBar = $this->debugBar ?: new StandardDebugBar(); |
||
| 79 | |||
| 80 | //Redirection response |
||
| 81 | if (Utils\Helpers::isRedirect($response)) { |
||
| 82 | if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { |
||
| 83 | $debugBar->stackData(); |
||
| 84 | } |
||
| 85 | |||
| 86 | //Html response |
||
| 87 | } elseif (FormatNegotiator::getFormat($request) === 'html') { |
||
| 88 | $renderer = $debugBar->getJavascriptRenderer(); |
||
| 89 | |||
| 90 | ob_start(); |
||
| 91 | echo '<style>'; |
||
| 92 | $renderer->dumpCssAssets(); |
||
| 93 | echo '</style>'; |
||
| 94 | |||
| 95 | echo '<script>'; |
||
| 96 | $renderer->dumpJsAssets(); |
||
| 97 | echo '</script>'; |
||
| 98 | |||
| 99 | echo $renderer->render(!$ajax); |
||
| 100 | |||
| 101 | $response = $this->inject($response, ob_get_clean()); |
||
| 102 | |||
| 103 | //Ajax response |
||
| 104 | } elseif ($ajax && $this->captureAjax) { |
||
| 105 | $headers = $debugBar->getDataAsHeaders(); |
||
| 106 | |||
| 107 | foreach ($headers as $name => $value) { |
||
| 108 | $response = $response->withHeader($name, $value); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return $next($request, $response); |
||
| 113 | } |
||
| 114 | } |
||
| 115 |