| Conditions | 18 |
| Paths | 69 |
| Total Lines | 76 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 3 |
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 |
||
| 84 | protected function afterRequest(HTTPRequest $request, HTTPResponse $response) |
||
| 85 | { |
||
| 86 | $debugbar = DebugBar::getDebugBar(); |
||
| 87 | if (!$debugbar) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | DebugBar::setRequest($request); |
||
| 91 | |||
| 92 | // All queries have been displayed |
||
| 93 | if (DebugBar::getShowQueries()) { |
||
| 94 | exit(); |
||
| 95 | } |
||
| 96 | |||
| 97 | $script = DebugBar::renderDebugBar(); |
||
| 98 | |||
| 99 | // If the bar is not renderable, return early |
||
| 100 | if (!$script) { |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Use module to make sure it's deferred and does not cause side effects |
||
| 105 | $script = str_replace('<script type="text/javascript">', '<script type="module">', $script); |
||
| 106 | |||
| 107 | // Inject init script into the HTML response |
||
| 108 | $body = (string)$response->getBody(); |
||
| 109 | if (strpos($body, '</body>') !== false) { |
||
| 110 | $customScripts = ''; |
||
| 111 | if (DebugBar::$suppressJquery) { |
||
| 112 | // Move scripts after the latest script |
||
| 113 | $matches = []; |
||
| 114 | preg_match_all('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>/', $body, $matches); |
||
| 115 | $body = preg_replace('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>\n/', '', $body); |
||
| 116 | $customScripts = implode("\n", $matches[0]); |
||
| 117 | } |
||
| 118 | |||
| 119 | $scriptsToInsert = $customScripts . "\n" . $script; |
||
| 120 | // You can use a placeholder if somehow you have a </body> string somewhere else in the body |
||
| 121 | // @link https://github.com/lekoala/silverstripe-debugbar/issues/154 |
||
| 122 | $debugbarPlaceholder = "<!-- debugbar -->"; |
||
| 123 | if (strpos($body, $debugbarPlaceholder) !== false) { |
||
| 124 | $body = str_replace($debugbarPlaceholder, $scriptsToInsert, $body); |
||
| 125 | } else { |
||
| 126 | if (Requirements::get_write_js_to_body()) { |
||
| 127 | // Replace the last occurence of </body> |
||
| 128 | $pos = strrpos($body, '</body>'); |
||
| 129 | if ($pos !== false) { |
||
| 130 | $body = substr_replace($body, $scriptsToInsert . '</body>', $pos, strlen('</body>')); |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | // Replace the first occurence of </head> |
||
| 134 | $pos = strpos($body, '</head>'); |
||
| 135 | if ($pos !== false) { |
||
| 136 | $body = substr_replace($body, $scriptsToInsert . '</head>', $pos, strlen('</head>')); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | $response->setBody($body); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Ajax support |
||
| 144 | if (Director::is_ajax() && !headers_sent()) { |
||
| 145 | if (DebugBar::isAdminUrl() && !DebugBar::config()->get('enabled_in_admin')) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | // Skip anything that is not a GET request |
||
| 149 | if (!$request->isGET()) { |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | // Always enable in admin because everything is mostly loaded through ajax |
||
| 153 | if (DebugBar::config()->get('ajax') || DebugBar::isAdminUrl()) { |
||
| 154 | $headers = $debugbar->getDataAsHeaders(); |
||
| 155 | |||
| 156 | // Prevent throwing js errors in case header size is too large |
||
| 157 | if (is_array($headers)) { |
||
| 158 | $maxHeaderLength = DebugBar::config()->get('max_header_length'); |
||
| 159 | $debugbar->sendDataInHeaders(null, 'phpdebugbar', $maxHeaderLength); |
||
| 160 | } |
||
| 165 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.