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