Conditions | 33 |
Paths | 352 |
Total Lines | 162 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 1122 |
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 |
||
244 | protected function templateNotFound($filename) |
||
245 | { |
||
246 | global $context, $txt, $scripturl, $modSettings, $boardurl; |
||
247 | global $maintenance, $mtitle, $mmessage; |
||
248 | |||
249 | @ob_end_clean(); |
||
250 | if (!empty($modSettings['enableCompressedOutput'])) |
||
251 | ob_start('ob_gzhandler'); |
||
252 | else |
||
253 | ob_start(); |
||
254 | |||
255 | if (isset($_GET['debug'])) |
||
256 | header('Content-Type: application/xhtml+xml; charset=UTF-8'); |
||
257 | |||
258 | // Don't cache error pages!! |
||
259 | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
||
260 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
||
261 | header('Cache-Control: no-cache'); |
||
262 | |||
263 | if (!isset($txt['template_parse_error'])) |
||
264 | { |
||
265 | $txt['template_parse_error'] = 'Template Parse Error!'; |
||
266 | $txt['template_parse_error_message'] = 'It seems something has gone sour on the forum with the template system. This problem should only be temporary, so please come back later and try again. If you continue to see this message, please contact the administrator.<br /><br />You can also try <a href="javascript:location.reload();">refreshing this page</a>.'; |
||
267 | $txt['template_parse_error_details'] = 'There was a problem loading the <span style="font-family: monospace;"><strong>%1$s</strong></span> template or language file. Please check the syntax and try again - remember, single quotes (<span style="font-family: monospace;">\'</span>) often have to be escaped with a slash (<span style="font-family: monospace;">\\</span>). To see more specific error information from PHP, try <a href="%2$s%1$s" class="extern">accessing the file directly</a>.<br /><br />You may want to try to <a href="javascript:location.reload();">refresh this page</a> or <a href="%3$s">use the default theme</a>.'; |
||
268 | $txt['template_parse_undefined'] = 'An undefined error occurred during the parsing of this template'; |
||
269 | } |
||
270 | |||
271 | // First, let's get the doctype and language information out of the way. |
||
272 | echo '<!DOCTYPE html> |
||
273 | <html ', !empty($context['right_to_left']) ? 'dir="rtl"' : '', '> |
||
274 | <head> |
||
275 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'; |
||
276 | |||
277 | if (!empty($maintenance) && !allowedTo('admin_forum')) { |
||
278 | echo ' |
||
279 | <title>', $mtitle, '</title> |
||
280 | </head> |
||
281 | <body> |
||
282 | <h3>', $mtitle, '</h3> |
||
283 | ', $mmessage, ' |
||
284 | </body> |
||
285 | </html>'; |
||
286 | } |
||
287 | elseif (!allowedTo('admin_forum')) |
||
288 | { |
||
289 | echo ' |
||
290 | <title>', $txt['template_parse_error'], '</title> |
||
291 | </head> |
||
292 | <body> |
||
293 | <h3>', $txt['template_parse_error'], '</h3> |
||
294 | ', $txt['template_parse_error_message'], ' |
||
295 | </body> |
||
296 | </html>'; |
||
297 | } |
||
298 | else |
||
299 | { |
||
300 | require_once(SUBSDIR . '/Package.subs.php'); |
||
301 | |||
302 | $error = fetch_web_data($boardurl . strtr($filename, array(BOARDDIR => '', strtr(BOARDDIR, '\\', '/') => ''))); |
||
303 | if (empty($error) && ini_get('track_errors') && !empty($php_errormsg)) |
||
304 | $error = $php_errormsg; |
||
305 | elseif (empty($error)) |
||
306 | $error = $txt['template_parse_undefined']; |
||
307 | |||
308 | $error = strtr($error, array('<b>' => '<strong>', '</b>' => '</strong>')); |
||
309 | |||
310 | echo ' |
||
311 | <title>', $txt['template_parse_error'], '</title> |
||
312 | </head> |
||
313 | <body> |
||
314 | <h3>', $txt['template_parse_error'], '</h3> |
||
315 | ', sprintf($txt['template_parse_error_details'], strtr($filename, array(BOARDDIR => '', strtr(BOARDDIR, '\\', '/') => '')), $boardurl, $scripturl . '?theme=1'); |
||
316 | |||
317 | if (!empty($error)) |
||
318 | echo ' |
||
319 | <hr /> |
||
320 | |||
321 | <div style="margin: 0 20px;"><span style="font-family: monospace;">', strtr(strtr($error, array('<strong>' . BOARDDIR => '<strong>...', '<strong>' . strtr(BOARDDIR, '\\', '/') => '<strong>...')), '\\', '/'), '</span></div>'; |
||
322 | |||
323 | // I know, I know... this is VERY COMPLICATED. Still, it's good. |
||
324 | if (preg_match('~ <strong>(\d+)</strong><br( /)?' . '>$~i', $error, $match) != 0) |
||
325 | { |
||
326 | $data = file($filename); |
||
327 | $data2 = highlight_php_code(implode('', $data)); |
||
328 | $data2 = preg_split('~\<br( /)?\>~', $data2); |
||
329 | |||
330 | // Fix the PHP code stuff... |
||
331 | if (!isBrowser('gecko')) |
||
332 | $data2 = str_replace("\t", '<span style="white-space: pre;">' . "\t" . '</span>', $data2); |
||
333 | else |
||
334 | $data2 = str_replace('<pre style="display: inline;">' . "\t" . '</pre>', "\t", $data2); |
||
335 | |||
336 | // Now we get to work around a bug in PHP where it doesn't escape <br />s! |
||
337 | $j = -1; |
||
338 | foreach ($data as $line) |
||
339 | { |
||
340 | $j++; |
||
341 | |||
342 | if (substr_count($line, '<br />') == 0) |
||
343 | continue; |
||
344 | |||
345 | $n = substr_count($line, '<br />'); |
||
346 | for ($i = 0; $i < $n; $i++) |
||
347 | { |
||
348 | $data2[$j] .= '<br />' . $data2[$j + $i + 1]; |
||
349 | unset($data2[$j + $i + 1]); |
||
350 | } |
||
351 | $j += $n; |
||
352 | } |
||
353 | $data2 = array_values($data2); |
||
354 | array_unshift($data2, ''); |
||
355 | |||
356 | echo ' |
||
357 | <div style="margin: 2ex 20px; width: 96%; overflow: auto;"><pre style="margin: 0;">'; |
||
358 | |||
359 | // Figure out what the color coding was before... |
||
360 | $line = max($match[1] - 9, 1); |
||
361 | $last_line = ''; |
||
362 | for ($line2 = $line - 1; $line2 > 1; $line2--) |
||
363 | if (strpos($data2[$line2], '<') !== false) |
||
364 | { |
||
365 | if (preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line2], $color_match) != 0) |
||
366 | $last_line = $color_match[1]; |
||
367 | break; |
||
368 | } |
||
369 | |||
370 | // Show the relevant lines... |
||
371 | for ($n = min($match[1] + 4, count($data2) + 1); $line <= $n; $line++) |
||
372 | { |
||
373 | if ($line == $match[1]) |
||
374 | echo '</pre><div style="background: #ffb0b5;"><pre style="margin: 0;">'; |
||
375 | |||
376 | echo '<span style="color: black;">', sprintf('%' . strlen($n) . 's', $line), ':</span> '; |
||
377 | if (isset($data2[$line]) && $data2[$line] != '') |
||
378 | echo substr($data2[$line], 0, 2) == '</' ? preg_replace('~^</[^>]+>~', '', $data2[$line]) : $last_line . $data2[$line]; |
||
379 | |||
380 | if (isset($data2[$line]) && preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line], $color_match) != 0) |
||
381 | { |
||
382 | $last_line = $color_match[1]; |
||
383 | echo '</', substr($last_line, 1, 4), '>'; |
||
384 | } |
||
385 | elseif ($last_line != '' && strpos($data2[$line], '<') !== false) |
||
386 | $last_line = ''; |
||
387 | elseif ($last_line != '' && $data2[$line] != '') |
||
388 | echo '</', substr($last_line, 1, 4), '>'; |
||
389 | |||
390 | if ($line == $match[1]) |
||
391 | echo '</pre></div><pre style="margin: 0;">'; |
||
392 | else |
||
393 | echo "\n"; |
||
394 | } |
||
395 | |||
396 | echo '</pre></div>'; |
||
397 | } |
||
398 | |||
399 | echo ' |
||
400 | </body> |
||
401 | </html>'; |
||
402 | } |
||
403 | |||
404 | die; |
||
405 | } |
||
406 | |||
449 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.