Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Handle often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Handle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | abstract class Handle |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * Error Array |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | public static $errors = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * General Error Page |
||
| 52 | * |
||
| 53 | * Takes an error message as input |
||
| 54 | * and displays it using the specified template. |
||
| 55 | * |
||
| 56 | * @param string $message |
||
| 57 | * @param int $code |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public static function halt($message, $code = 404) |
||
|
|
|||
| 61 | { |
||
| 62 | Container::get('response')->setStatus($code); |
||
| 63 | |||
| 64 | if (!Container::get('config')->get('app_debug')) { |
||
| 65 | $message = '404 Not Found.'; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (Container::get('config')->get('app_debug')) { |
||
| 69 | $tplPath = null; |
||
| 70 | } else { |
||
| 71 | $tplPath = Container::get('config')->get('error_tpl'); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($tplPath == null || !Helper::isFile(Container::get('config')->get('app_full_path') . '/views/' . $tplPath . '.html')) { |
||
| 75 | $tpl = '<!DOCTYPE html> |
||
| 76 | <html lang="en"> |
||
| 77 | <head> |
||
| 78 | <meta http-equiv="content-type" content="text/html; charset=utf-8"> |
||
| 79 | <title>Oops! some error(s) occurred with your application</title> |
||
| 80 | <meta name="robots" content="NONE,NOARCHIVE"> |
||
| 81 | <style type="text/css"> |
||
| 82 | html * { padding:0; margin:0; } |
||
| 83 | body * { padding:10px 20px; } |
||
| 84 | body * * { padding:0; } |
||
| 85 | body { font-family:Avenir,Helvetica,Arial,sans-serif; -webkit-font-smoothing:antialiased; -moz-osx-font-smoothing:grayscale;background:#eee; } |
||
| 86 | body > div { border-bottom:1px solid #ddd; } |
||
| 87 | h1 { font-weight:normal; color:#5d5d5d;} |
||
| 88 | h1 span { font-size:60%; color:#666; font-weight:normal; } |
||
| 89 | table { border-collapse:collapse; width:100%; } |
||
| 90 | td, th { vertical-align:top; padding:2px 3px; } |
||
| 91 | th { width:12em; text-align:right; color:#666; padding-right:.5em; } |
||
| 92 | #info { background:#f6f6f6; } |
||
| 93 | #info p { font-size:16px; margin:5px; color:#5d5d5d; } |
||
| 94 | #info strong { font-size:17px; color:#696969; } |
||
| 95 | #summary { background:#eee; } |
||
| 96 | #explanation { background:#eee; border-bottom: 0px none; } |
||
| 97 | </style> |
||
| 98 | </head> |
||
| 99 | <body> |
||
| 100 | <div id="summary"> |
||
| 101 | <h1>Oops! some error(s) occurred with your application</span></h1> |
||
| 102 | </div> |
||
| 103 | <div id="info"> |
||
| 104 | <p><strong>Request Method: </strong>' . strtoupper($_SERVER['REQUEST_METHOD']) . '</p> |
||
| 105 | <p><strong>Request URL: </strong>' . Container::get('request')->getBaseUrl() . ltrim($_SERVER['REQUEST_URI'], '/') . '</p> |
||
| 106 | ' . $message . ' |
||
| 107 | </div> |
||
| 108 | |||
| 109 | <div id="explanation"> |
||
| 110 | <p> |
||
| 111 | You\'re seeing this error because you have <code>app_debug = True</code> in |
||
| 112 | your index.php file. Change that to <code>False</code>, and Kotori.php |
||
| 113 | will display a standard 404 page. |
||
| 114 | </p> |
||
| 115 | </div> |
||
| 116 | </body> |
||
| 117 | </html>'; |
||
| 118 | } else { |
||
| 119 | $tpl = file_get_contents(Container::get('config')->get('app_full_path') . '/views/' . $tplPath . '.html'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $tpl = str_replace('{$message}', $message, $tpl); |
||
| 123 | $tpl = HtmlCompress::construct()->compress($tpl); |
||
| 124 | exit($tpl); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Error Handler |
||
| 129 | * |
||
| 130 | * This function lets us invoke the exception class and |
||
| 131 | * display errors using the standard error template located |
||
| 132 | * in app/views/Public/error.html |
||
| 133 | * This function will send the error page directly to the |
||
| 134 | * browser and exit. |
||
| 135 | * |
||
| 136 | * @param string $errno |
||
| 137 | * @param int $errstr |
||
| 138 | * @param string $errfile |
||
| 139 | * @param int $errline |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public static function error($errno, $errstr, $errfile, $errline) |
||
| 143 | { |
||
| 144 | $type = self::getErrorType($errno); |
||
| 145 | $text = self::renderErrorText($type, $errstr, $errline, $errfile); |
||
| 146 | $txt = self::renderLogBody($type, $errstr, $errline, $errfile); |
||
| 147 | array_push(self::$errors, $text); |
||
| 148 | Container::get('logger')->warning($txt); |
||
| 149 | if (Container::get('request')->isCli()) { |
||
| 150 | self::outputCliError($errstr); |
||
| 151 | } else { |
||
| 152 | self::setDebugHeader($txt); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Exception Handler |
||
| 158 | * |
||
| 159 | * Sends uncaught exceptions to the logger and displays them |
||
| 160 | * only if display_errors is On so that they don't show up in |
||
| 161 | * production environments. |
||
| 162 | * |
||
| 163 | * @param Exception $exception |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public static function exception($exception) |
||
| 167 | { |
||
| 168 | $text = self::renderHaltBody(get_class($exception), $exception->getMessage(), $exception->getLine(), $exception->getFile()); |
||
| 169 | $txt = self::renderLogBody(get_class($exception), $exception->getMessage(), $exception->getLine(), $exception->getFile()); |
||
| 170 | Container::get('logger')->critical($txt); |
||
| 171 | View Code Duplication | if (Container::get('request')->isCli()) { |
|
| 172 | self::outputCliError($exception->getMessage()); |
||
| 173 | } else { |
||
| 174 | self::setDebugHeader($txt); |
||
| 175 | self::halt($text, Container::get('config')->get('app_debug') ? 500 : 404); |
||
| 176 | } |
||
| 177 | |||
| 178 | exit; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Shutdown Handler |
||
| 183 | * |
||
| 184 | * This is the shutdown handler that is declared in framework. |
||
| 185 | * The main reason we use this is to simulate |
||
| 186 | * a complete custom exception handler. |
||
| 187 | * |
||
| 188 | * E_STRICT is purposively neglected because such events may have |
||
| 189 | * been caught. Duplication or none? None is preferred for now. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public static function end() |
||
| 194 | { |
||
| 195 | $last_error = error_get_last(); |
||
| 196 | if (isset($last_error) && |
||
| 197 | ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { |
||
| 198 | $type = self::getErrorType($last_error['type']); |
||
| 199 | $text = self::renderHaltBody($type, $last_error['message'], $last_error['line'], $last_error['file']); |
||
| 200 | |||
| 201 | $txt = self::renderLogBody($type, $last_error['message'], $last_error['file'], $last_error['line']); |
||
| 202 | |||
| 203 | Container::get('logger')->error($txt); |
||
| 204 | View Code Duplication | if (Container::get('request')->isCli()) { |
|
| 205 | self::outputCliError($last_error['message']); |
||
| 206 | } else { |
||
| 207 | self::setDebugHeader($txt); |
||
| 208 | self::halt($text, Container::get('config')->get('app_debug') ? 500 : 404); |
||
| 209 | } |
||
| 210 | |||
| 211 | exit; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * output debug info to header |
||
| 217 | * |
||
| 218 | * @param string $txt |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | protected static function setDebugHeader($txt) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * output error info to cli |
||
| 230 | * |
||
| 231 | * @param string $message |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | protected static function outputCliError($message) |
||
| 235 | { |
||
| 236 | $application = new ConsoleApp(); |
||
| 237 | $output = new ConsoleOutput(); |
||
| 238 | $application->renderException(new ResponseException($message), $output); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * convert PHP ERROR to error detail |
||
| 243 | * |
||
| 244 | * @param int $errno |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | protected static function getErrorType($errno) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * render Halt Body |
||
| 299 | * |
||
| 300 | * @param string $type |
||
| 301 | * @param int $message |
||
| 302 | * @param int $line |
||
| 303 | * @param string $file |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected static function renderHaltBody($type, $message, $line, $file) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * render log body |
||
| 376 | * |
||
| 377 | * @param string $type |
||
| 378 | * @param int $message |
||
| 379 | * @param string $file |
||
| 380 | * @param int $line |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | protected static function renderLogBody($type, $message, $line, $file) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * render errors display in trace |
||
| 390 | * |
||
| 391 | * @param string $type |
||
| 392 | * @param int $message |
||
| 393 | * @param string $file |
||
| 394 | * @param int $line |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | // @codingStandardsIgnoreStart |
||
| 398 | protected static function renderErrorText($type, $message, $line, $file) |
||
| 402 | // @codingStandardsIgnoreEnd |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get source code from file |
||
| 406 | * |
||
| 407 | * @param string $file |
||
| 408 | * @param int $line |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | protected static function getSourceCode($file, $line) |
||
| 427 | } |
||
| 428 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: