| Conditions | 22 |
| Paths | 141 |
| Total Lines | 97 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| 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 |
||
| 85 | protected function afterRequest(HTTPRequest $request, HTTPResponse $response) |
||
| 86 | { |
||
| 87 | $debugbar = DebugBar::getDebugBar(); |
||
| 88 | if (!$debugbar) { |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | // Should not alter xml or json, only html |
||
| 93 | $contentType = $response->getHeader('Content-Type'); |
||
| 94 | if (!str_contains($contentType, 'text/html')) { |
||
| 95 | // We need to strip <link> and <script> tags added to the body |
||
| 96 | // Calling DebugBar::excludeRequirements(); won't work since the response is already generated |
||
| 97 | $body = (string)$response->getBody(); |
||
| 98 | if (str_contains($body, '/debugbar/')) { |
||
| 99 | $regex = '/^[ \t]*<(?:link|script)[^>]*?debugbar[^>]*?>(?:.*?<\/script>)?.*(?:\r?\n)?/mi'; |
||
| 100 | $cleanedBody = preg_replace($regex, '', $body); |
||
| 101 | $response->setBody($cleanedBody); |
||
| 102 | } |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | // Don't apply to assets |
||
| 107 | $dir = defined('ASSETS_DIR') ? ASSETS_DIR : 'assets'; |
||
| 108 | if (strpos($request->getURL(), "$dir/") === 0) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | DebugBar::setRequest($request); |
||
| 113 | |||
| 114 | // All queries have been displayed |
||
| 115 | if (DebugBar::getShowQueries()) { |
||
| 116 | exit(); |
||
| 117 | } |
||
| 118 | $script = DebugBar::renderDebugBar(); |
||
| 119 | |||
| 120 | // If the bar is not renderable, return early |
||
| 121 | if (!$script) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Use module to make sure it's deferred and does not cause side effects |
||
| 126 | $script = str_replace('<script type="text/javascript">', '<script type="module">', $script); |
||
| 127 | |||
| 128 | // Inject init script into the HTML response |
||
| 129 | $body = (string)$response->getBody(); |
||
| 130 | |||
| 131 | if (strpos($body, '</body>') !== false) { |
||
| 132 | $customScripts = ''; |
||
| 133 | if (DebugBar::$suppressJquery) { |
||
| 134 | // Move scripts after the latest script |
||
| 135 | $matches = []; |
||
| 136 | preg_match_all('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>/', $body, $matches); |
||
| 137 | $body = preg_replace('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>\n/', '', $body); |
||
| 138 | $customScripts = implode("\n", $matches[0]); |
||
| 139 | } |
||
| 140 | |||
| 141 | $scriptsToInsert = $customScripts . "\n" . $script; |
||
| 142 | // You can use a placeholder if somehow you have a </body> string somewhere else in the body |
||
| 143 | // @link https://github.com/lekoala/silverstripe-debugbar/issues/154 |
||
| 144 | $debugbarPlaceholder = "<!-- debugbar -->"; |
||
| 145 | if (strpos((string) $body, $debugbarPlaceholder) !== false) { |
||
| 146 | $body = str_replace($debugbarPlaceholder, $scriptsToInsert, $body); |
||
| 147 | } else { |
||
| 148 | if (Requirements::get_write_js_to_body()) { |
||
| 149 | // Replace the last occurence of </body> |
||
| 150 | $pos = strrpos((string) $body, '</body>'); |
||
| 151 | if ($pos !== false) { |
||
| 152 | $body = substr_replace($body, $scriptsToInsert . '</body>', $pos, strlen('</body>')); |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | // Replace the first occurence of </head> |
||
| 156 | $pos = strpos((string) $body, '</head>'); |
||
| 157 | if ($pos !== false) { |
||
| 158 | $body = substr_replace($body, $scriptsToInsert . '</head>', $pos, strlen('</head>')); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $response->setBody($body); |
||
| 163 | } |
||
| 164 | |||
| 165 | // Ajax support |
||
| 166 | if (Director::is_ajax() && !headers_sent()) { |
||
| 167 | if (DebugBar::isAdminUrl() && !DebugBar::config()->get('enabled_in_admin')) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | // Skip anything that is not a GET request |
||
| 171 | if (!$request->isGET()) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | // Always enable in admin because everything is mostly loaded through ajax |
||
| 175 | if (DebugBar::config()->get('ajax') || DebugBar::isAdminUrl()) { |
||
| 176 | $headers = $debugbar->getDataAsHeaders(); |
||
| 177 | |||
| 178 | // Prevent throwing js errors in case header size is too large |
||
| 179 | if (is_array($headers)) { |
||
| 180 | $maxHeaderLength = DebugBar::config()->get('max_header_length'); |
||
| 181 | $debugbar->sendDataInHeaders(null, 'phpdebugbar', $maxHeaderLength); |
||
| 182 | } |
||
| 187 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.