| Conditions | 4 |
| Paths | 8 |
| Total Lines | 65 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 20 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 26 | public static function exceptionHandler(Exception $exception) |
||
| 27 | { |
||
| 28 | /** @global $siteConfiguration SiteConfiguration */ |
||
| 29 | global $siteConfiguration; |
||
| 30 | |||
| 31 | $errorDocument = <<<HTML |
||
| 32 | <!DOCTYPE html> |
||
| 33 | <html lang="en"><head> |
||
| 34 | <meta charset="utf-8"> |
||
| 35 | <title>Oops! Something went wrong!</title> |
||
| 36 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||
| 37 | <link href="{$siteConfiguration->getBaseUrl()}/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet"> |
||
| 38 | <style> |
||
| 39 | body { |
||
| 40 | padding-top: 60px; |
||
| 41 | } |
||
| 42 | </style> |
||
| 43 | <link href="{$siteConfiguration->getBaseUrl()}/lib/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet"> |
||
| 44 | </head><body><div class="container"> |
||
| 45 | <h1>Oops! Something went wrong!</h1> |
||
| 46 | <p>We'll work on fixing this for you, so why not come back later?</p> |
||
| 47 | <p class="muted">If our trained monkeys ask, tell them this error ID: <code>$1$</code></p> |
||
| 48 | $2$ |
||
| 49 | </div></body></html> |
||
| 50 | HTML; |
||
| 51 | |||
| 52 | $errorData = self::getExceptionData($exception); |
||
| 53 | $errorData['server'] = $_SERVER; |
||
| 54 | $errorData['get'] = $_GET; |
||
| 55 | $errorData['post'] = $_POST; |
||
| 56 | |||
| 57 | $state = serialize($errorData); |
||
| 58 | $errorId = sha1($state); |
||
| 59 | |||
| 60 | // Save the error for later analysis |
||
| 61 | file_put_contents($siteConfiguration->getErrorLog() . '/' . $errorId . '.log', $state); |
||
| 62 | |||
| 63 | // clear and discard any content that's been saved to the output buffer |
||
| 64 | if (ob_get_level() > 0) { |
||
| 65 | ob_end_clean(); |
||
| 66 | } |
||
| 67 | |||
| 68 | // push error ID into the document. |
||
| 69 | $message = str_replace('$1$', $errorId, $errorDocument); |
||
| 70 | |||
| 71 | if ($siteConfiguration->getDebuggingTraceEnabled()) { |
||
| 72 | ob_start(); |
||
| 73 | var_dump($errorData); |
||
|
1 ignored issue
–
show
|
|||
| 74 | $textErrorData = ob_get_contents(); |
||
| 75 | ob_end_clean(); |
||
| 76 | |||
| 77 | $message = str_replace('$2$', $textErrorData, $message); |
||
| 78 | } |
||
| 79 | else { |
||
| 80 | $message = str_replace('$2$', "", $message); |
||
| 81 | } |
||
| 82 | |||
| 83 | // While we *shouldn't* have sent headers by now due to the output buffering, PHPUnit does weird things. |
||
| 84 | // This is "only" needed for the tests, but it's a good idea to wrap this anyway. |
||
| 85 | if (!headers_sent()) { |
||
| 86 | header('HTTP/1.1 500 Internal Server Error'); |
||
| 87 | } |
||
| 88 | |||
| 89 | // output the document |
||
| 90 | print $message; |
||
| 91 | } |
||
| 125 | } |