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 MWExceptionHandler 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 MWExceptionHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class MWExceptionHandler { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string $reservedMemory |
||
| 32 | */ |
||
| 33 | protected static $reservedMemory; |
||
| 34 | /** |
||
| 35 | * @var array $fatalErrorTypes |
||
| 36 | */ |
||
| 37 | protected static $fatalErrorTypes = [ |
||
| 38 | E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, |
||
| 39 | /* HHVM's FATAL_ERROR level */ 16777217, |
||
| 40 | ]; |
||
| 41 | /** |
||
| 42 | * @var bool $handledFatalCallback |
||
| 43 | */ |
||
| 44 | protected static $handledFatalCallback = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Install handlers with PHP. |
||
| 48 | */ |
||
| 49 | public static function installHandler() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Report an exception to the user |
||
| 60 | * @param Exception|Throwable $e |
||
| 61 | */ |
||
| 62 | protected static function report( $e ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * If there are any open database transactions, roll them back and log |
||
| 83 | * the stack trace of the exception that should have been caught so the |
||
| 84 | * transaction could be aborted properly. |
||
| 85 | * |
||
| 86 | * @since 1.23 |
||
| 87 | * @param Exception|Throwable $e |
||
| 88 | */ |
||
| 89 | public static function rollbackMasterChangesAndLog( $e ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Exception handler which simulates the appropriate catch() handling: |
||
| 109 | * |
||
| 110 | * try { |
||
| 111 | * ... |
||
| 112 | * } catch ( Exception $e ) { |
||
| 113 | * $e->report(); |
||
| 114 | * } catch ( Exception $e ) { |
||
| 115 | * echo $e->__toString(); |
||
| 116 | * } |
||
| 117 | * |
||
| 118 | * @since 1.25 |
||
| 119 | * @param Exception|Throwable $e |
||
| 120 | */ |
||
| 121 | public static function handleException( $e ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Handler for set_error_handler() callback notifications. |
||
| 145 | * |
||
| 146 | * Receive a callback from the interpreter for a raised error, create an |
||
| 147 | * ErrorException, and log the exception to the 'error' logging |
||
| 148 | * channel(s). If the raised error is a fatal error type (only under HHVM) |
||
| 149 | * delegate to handleFatalError() instead. |
||
| 150 | * |
||
| 151 | * @since 1.25 |
||
| 152 | * |
||
| 153 | * @param int $level Error level raised |
||
| 154 | * @param string $message |
||
| 155 | * @param string $file |
||
| 156 | * @param int $line |
||
| 157 | * @return bool |
||
| 158 | * |
||
| 159 | * @see logError() |
||
| 160 | */ |
||
| 161 | public static function handleError( |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Dual purpose callback used as both a set_error_handler() callback and |
||
| 208 | * a registered shutdown function. Receive a callback from the interpreter |
||
| 209 | * for a raised error or system shutdown, check for a fatal error, and log |
||
| 210 | * to the 'fatal' logging channel. |
||
| 211 | * |
||
| 212 | * Special handling is included for missing class errors as they may |
||
| 213 | * indicate that the user needs to install 3rd-party libraries via |
||
| 214 | * Composer or other means. |
||
| 215 | * |
||
| 216 | * @since 1.25 |
||
| 217 | * |
||
| 218 | * @param int $level Error level raised |
||
| 219 | * @param string $message Error message |
||
| 220 | * @param string $file File that error was raised in |
||
| 221 | * @param int $line Line number error was raised at |
||
| 222 | * @param array $context Active symbol table point of error |
||
| 223 | * @param array $trace Backtrace at point of error (undocumented HHVM |
||
| 224 | * feature) |
||
| 225 | * @return bool Always returns false |
||
| 226 | */ |
||
| 227 | public static function handleFatalError( |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Generate a string representation of an exception's stack trace |
||
| 306 | * |
||
| 307 | * Like Exception::getTraceAsString, but replaces argument values with |
||
| 308 | * argument type or class name. |
||
| 309 | * |
||
| 310 | * @param Exception|Throwable $e |
||
| 311 | * @return string |
||
| 312 | * @see prettyPrintTrace() |
||
| 313 | */ |
||
| 314 | public static function getRedactedTraceAsString( $e ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Generate a string representation of a stacktrace. |
||
| 320 | * |
||
| 321 | * @param array $trace |
||
| 322 | * @param string $pad Constant padding to add to each line of trace |
||
| 323 | * @return string |
||
| 324 | * @since 1.26 |
||
| 325 | */ |
||
| 326 | public static function prettyPrintTrace( array $trace, $pad = '' ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return a copy of an exception's backtrace as an array. |
||
| 364 | * |
||
| 365 | * Like Exception::getTrace, but replaces each element in each frame's |
||
| 366 | * argument array with the name of its class (if the element is an object) |
||
| 367 | * or its type (if the element is a PHP primitive). |
||
| 368 | * |
||
| 369 | * @since 1.22 |
||
| 370 | * @param Exception|Throwable $e |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public static function getRedactedTrace( $e ) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Redact a stacktrace generated by Exception::getTrace(), |
||
| 379 | * debug_backtrace() or similar means. Replaces each element in each |
||
| 380 | * frame's argument array with the name of its class (if the element is an |
||
| 381 | * object) or its type (if the element is a PHP primitive). |
||
| 382 | * |
||
| 383 | * @since 1.26 |
||
| 384 | * @param array $trace Stacktrace |
||
| 385 | * @return array Stacktrace with arugment values converted to data types |
||
| 386 | */ |
||
| 387 | public static function redactTrace( array $trace ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get the ID for this exception. |
||
| 400 | * |
||
| 401 | * The ID is saved so that one can match the one output to the user (when |
||
| 402 | * $wgShowExceptionDetails is set to false), to the entry in the debug log. |
||
| 403 | * |
||
| 404 | * @since 1.22 |
||
| 405 | * @deprecated since 1.27: Exception IDs are synonymous with request IDs. |
||
| 406 | * @param Exception|Throwable $e |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public static function getLogId( $e ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * If the exception occurred in the course of responding to a request, |
||
| 416 | * returns the requested URL. Otherwise, returns false. |
||
| 417 | * |
||
| 418 | * @since 1.23 |
||
| 419 | * @return string|false |
||
| 420 | */ |
||
| 421 | public static function getURL() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get a message formatting the exception message and its origin. |
||
| 431 | * |
||
| 432 | * @since 1.22 |
||
| 433 | * @param Exception|Throwable $e |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public static function getLogMessage( $e ) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param Exception|Throwable $e |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public static function getPublicLogMessage( $e ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get a PSR-3 log event context from an Exception. |
||
| 461 | * |
||
| 462 | * Creates a structured array containing information about the provided |
||
| 463 | * exception that can be used to augment a log message sent to a PSR-3 |
||
| 464 | * logger. |
||
| 465 | * |
||
| 466 | * @param Exception|Throwable $e |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | public static function getLogContext( $e ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get a structured representation of an Exception. |
||
| 478 | * |
||
| 479 | * Returns an array of structured data (class, message, code, file, |
||
| 480 | * backtrace) derived from the given exception. The backtrace information |
||
| 481 | * will be redacted as per getRedactedTraceAsArray(). |
||
| 482 | * |
||
| 483 | * @param Exception|Throwable $e |
||
| 484 | * @return array |
||
| 485 | * @since 1.26 |
||
| 486 | */ |
||
| 487 | public static function getStructuredExceptionData( $e ) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Serialize an Exception object to JSON. |
||
| 520 | * |
||
| 521 | * The JSON object will have keys 'id', 'file', 'line', 'message', and |
||
| 522 | * 'url'. These keys map to string values, with the exception of 'line', |
||
| 523 | * which is a number, and 'url', which may be either a string URL or or |
||
| 524 | * null if the exception did not occur in the context of serving a web |
||
| 525 | * request. |
||
| 526 | * |
||
| 527 | * If $wgLogExceptionBacktrace is true, it will also have a 'backtrace' |
||
| 528 | * key, mapped to the array return value of Exception::getTrace, but with |
||
| 529 | * each element in each frame's "args" array (if set) replaced with the |
||
| 530 | * argument's class name (if the argument is an object) or type name (if |
||
| 531 | * the argument is a PHP primitive). |
||
| 532 | * |
||
| 533 | * @par Sample JSON record ($wgLogExceptionBacktrace = false): |
||
| 534 | * @code |
||
| 535 | * { |
||
| 536 | * "id": "c41fb419", |
||
| 537 | * "type": "MWException", |
||
| 538 | * "file": "/var/www/mediawiki/includes/cache/MessageCache.php", |
||
| 539 | * "line": 704, |
||
| 540 | * "message": "Non-string key given", |
||
| 541 | * "url": "/wiki/Main_Page" |
||
| 542 | * } |
||
| 543 | * @endcode |
||
| 544 | * |
||
| 545 | * @par Sample JSON record ($wgLogExceptionBacktrace = true): |
||
| 546 | * @code |
||
| 547 | * { |
||
| 548 | * "id": "dc457938", |
||
| 549 | * "type": "MWException", |
||
| 550 | * "file": "/vagrant/mediawiki/includes/cache/MessageCache.php", |
||
| 551 | * "line": 704, |
||
| 552 | * "message": "Non-string key given", |
||
| 553 | * "url": "/wiki/Main_Page", |
||
| 554 | * "backtrace": [{ |
||
| 555 | * "file": "/vagrant/mediawiki/extensions/VisualEditor/VisualEditor.hooks.php", |
||
| 556 | * "line": 80, |
||
| 557 | * "function": "get", |
||
| 558 | * "class": "MessageCache", |
||
| 559 | * "type": "->", |
||
| 560 | * "args": ["array"] |
||
| 561 | * }] |
||
| 562 | * } |
||
| 563 | * @endcode |
||
| 564 | * |
||
| 565 | * @since 1.23 |
||
| 566 | * @param Exception|Throwable $e |
||
| 567 | * @param bool $pretty Add non-significant whitespace to improve readability (default: false). |
||
| 568 | * @param int $escaping Bitfield consisting of FormatJson::.*_OK class constants. |
||
| 569 | * @return string|false JSON string if successful; false upon failure |
||
| 570 | */ |
||
| 571 | public static function jsonSerializeException( $e, $pretty = false, $escaping = 0 ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Log an exception to the exception log (if enabled). |
||
| 578 | * |
||
| 579 | * This method must not assume the exception is an MWException, |
||
| 580 | * it is also used to handle PHP exceptions or exceptions from other libraries. |
||
| 581 | * |
||
| 582 | * @since 1.22 |
||
| 583 | * @param Exception|Throwable $e |
||
| 584 | */ |
||
| 585 | public static function logException( $e ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Log an exception that wasn't thrown but made to wrap an error. |
||
| 605 | * |
||
| 606 | * @since 1.25 |
||
| 607 | * @param ErrorException $e |
||
| 608 | * @param string $channel |
||
| 609 | */ |
||
| 610 | protected static function logError( ErrorException $e, $channel ) { |
||
| 632 | } |
||
| 633 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.