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:
| 1 | <?php |
||
| 26 | class MWException extends Exception { |
||
| 27 | /** |
||
| 28 | * Should the exception use $wgOut to output the error? |
||
| 29 | * |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public function useOutputPage() { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Whether to log this exception in the exception debug log. |
||
| 41 | * |
||
| 42 | * @since 1.23 |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | public function isLoggable() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Can the extension use the Message class/wfMessage to get i18n-ed messages? |
||
| 51 | * |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | public function useMessageCache() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Run hook to allow extensions to modify the text of the exception |
||
| 68 | * |
||
| 69 | * @param string $name Class name of the exception |
||
| 70 | * @param array $args Arguments to pass to the callback functions |
||
| 71 | * @return string|null String to output or null if any hook has been called |
||
| 72 | */ |
||
| 73 | public function runHooks( $name, $args = [] ) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get a message from i18n |
||
| 79 | * |
||
| 80 | * @param string $key Message name |
||
| 81 | * @param string $fallback Default message if the message cache can't be |
||
| 82 | * called by the exception |
||
| 83 | * The function also has other parameters that are arguments for the message |
||
| 84 | * @return string Message with arguments replaced |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function msg( $key, $fallback /*[, params...] */ ) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * If $wgShowExceptionDetails is true, return a HTML message with a |
||
| 100 | * backtrace to the error, otherwise show a message to ask to set it to true |
||
| 101 | * to show that information. |
||
| 102 | * |
||
| 103 | * @return string Html to output |
||
| 104 | */ |
||
| 105 | public function getHTML() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the text to display when reporting the error on the command line. |
||
| 133 | * If $wgShowExceptionDetails is true, return a text message with a |
||
| 134 | * backtrace to the error. |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function getText() { |
|
| 139 | global $wgShowExceptionDetails; |
||
| 140 | |||
| 141 | if ( $wgShowExceptionDetails ) { |
||
| 142 | return MWExceptionHandler::getLogMessage( $this ) . |
||
| 143 | "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n"; |
||
| 144 | } else { |
||
| 145 | return "Set \$wgShowExceptionDetails = true; " . |
||
| 146 | "in LocalSettings.php to show detailed debugging information.\n"; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return the title of the page when reporting this error in a HTTP response. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getPageTitle() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Output the exception report using HTML. |
||
| 161 | */ |
||
| 162 | public function reportHTML() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Output a report about the exception and takes care of formatting. |
||
| 199 | * It will be either HTML or plain text based on isCommandLine(). |
||
| 200 | */ |
||
| 201 | public function report() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Check whether we are in command line mode or not to report the exception |
||
| 226 | * in the correct format. |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public static function isCommandLine() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Send a header, if we haven't already sent them. We shouldn't, |
||
| 236 | * but sometimes we might in a weird case like Export |
||
| 237 | * @param string $header |
||
| 238 | */ |
||
| 239 | private static function header( $header ) { |
||
| 249 | } |
||
| 250 |
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: