Complex classes like MWDebug 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 MWDebug, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class MWDebug { |
||
| 32 | /** |
||
| 33 | * Log lines |
||
| 34 | * |
||
| 35 | * @var array $log |
||
| 36 | */ |
||
| 37 | protected static $log = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Debug messages from wfDebug(). |
||
| 41 | * |
||
| 42 | * @var array $debug |
||
| 43 | */ |
||
| 44 | protected static $debug = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * SQL statements of the database queries. |
||
| 48 | * |
||
| 49 | * @var array $query |
||
| 50 | */ |
||
| 51 | protected static $query = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Is the debugger enabled? |
||
| 55 | * |
||
| 56 | * @var bool $enabled |
||
| 57 | */ |
||
| 58 | protected static $enabled = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Array of functions that have already been warned, formatted |
||
| 62 | * function-caller to prevent a buttload of warnings |
||
| 63 | * |
||
| 64 | * @var array $deprecationWarnings |
||
| 65 | */ |
||
| 66 | protected static $deprecationWarnings = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Enabled the debugger and load resource module. |
||
| 70 | * This is called by Setup.php when $wgDebugToolbar is true. |
||
| 71 | * |
||
| 72 | * @since 1.19 |
||
| 73 | */ |
||
| 74 | public static function init() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Disable the debugger. |
||
| 80 | * |
||
| 81 | * @since 1.28 |
||
| 82 | */ |
||
| 83 | public static function deinit() { |
||
| 84 | self::$enabled = false; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Add ResourceLoader modules to the OutputPage object if debugging is |
||
| 89 | * enabled. |
||
| 90 | * |
||
| 91 | * @since 1.19 |
||
| 92 | * @param OutputPage $out |
||
| 93 | */ |
||
| 94 | public static function addModules( OutputPage $out ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Adds a line to the log |
||
| 102 | * |
||
| 103 | * @since 1.19 |
||
| 104 | * @param mixed $str |
||
| 105 | */ |
||
| 106 | public static function log( $str ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns internal log array |
||
| 122 | * @since 1.19 |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public static function getLog() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Clears internal log array and deprecation tracking |
||
| 131 | * @since 1.19 |
||
| 132 | */ |
||
| 133 | public static function clearLog() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Adds a warning entry to the log |
||
| 140 | * |
||
| 141 | * @since 1.19 |
||
| 142 | * @param string $msg |
||
| 143 | * @param int $callerOffset |
||
| 144 | * @param int $level A PHP error level. See sendMessage() |
||
| 145 | * @param string $log 'production' will always trigger a php error, 'auto' |
||
| 146 | * will trigger an error if $wgDevelopmentWarnings is true, and 'debug' |
||
| 147 | * will only write to the debug log(s). |
||
| 148 | * |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | public static function warning( $msg, $callerOffset = 1, $level = E_USER_NOTICE, $log = 'auto' ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Show a warning that $function is deprecated. |
||
| 177 | * This will send it to the following locations: |
||
| 178 | * - Debug toolbar, with one item per function and caller, if $wgDebugToolbar |
||
| 179 | * is set to true. |
||
| 180 | * - PHP's error log, with level E_USER_DEPRECATED, if $wgDevelopmentWarnings |
||
| 181 | * is set to true. |
||
| 182 | * - MediaWiki's debug log, if $wgDevelopmentWarnings is set to false. |
||
| 183 | * |
||
| 184 | * @since 1.19 |
||
| 185 | * @param string $function Function that is deprecated. |
||
| 186 | * @param string|bool $version Version in which the function was deprecated. |
||
| 187 | * @param string|bool $component Component to which the function belongs. |
||
| 188 | * If false, it is assumbed the function is in MediaWiki core. |
||
| 189 | * @param int $callerOffset How far up the callstack is the original |
||
| 190 | * caller. 2 = function that called the function that called |
||
| 191 | * MWDebug::deprecated() (Added in 1.20). |
||
| 192 | */ |
||
| 193 | public static function deprecated( $function, $version = false, |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get an array describing the calling function at a specified offset. |
||
| 261 | * |
||
| 262 | * @param int $callerOffset How far up the callstack is the original |
||
| 263 | * caller. 0 = function that called getCallerDescription() |
||
| 264 | * @return array Array with two keys: 'file' and 'func' |
||
| 265 | */ |
||
| 266 | private static function getCallerDescription( $callerOffset ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Send a message to the debug log and optionally also trigger a PHP |
||
| 298 | * error, depending on the $level argument. |
||
| 299 | * |
||
| 300 | * @param string $msg Message to send |
||
| 301 | * @param array $caller Caller description get from getCallerDescription() |
||
| 302 | * @param string $group Log group on which to send the message |
||
| 303 | * @param int|bool $level Error level to use; set to false to not trigger an error |
||
| 304 | */ |
||
| 305 | private static function sendMessage( $msg, $caller, $group, $level ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * This is a method to pass messages from wfDebug to the pretty debugger. |
||
| 317 | * Do NOT use this method, use MWDebug::log or wfDebug() |
||
| 318 | * |
||
| 319 | * @since 1.19 |
||
| 320 | * @param string $str |
||
| 321 | * @param array $context |
||
| 322 | */ |
||
| 323 | public static function debugMsg( $str, $context = [] ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Begins profiling on a database query |
||
| 345 | * |
||
| 346 | * @since 1.19 |
||
| 347 | * @param string $sql |
||
| 348 | * @param string $function |
||
| 349 | * @param bool $isMaster |
||
| 350 | * @param float $runTime Query run time |
||
| 351 | * @return int ID number of the query to pass to queryTime or -1 if the |
||
| 352 | * debugger is disabled |
||
| 353 | */ |
||
| 354 | public static function query( $sql, $function, $isMaster, $runTime ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns a list of files included, along with their size |
||
| 396 | * |
||
| 397 | * @param IContextSource $context |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | protected static function getFilesIncluded( IContextSource $context ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Returns the HTML to add to the page for the toolbar |
||
| 416 | * |
||
| 417 | * @since 1.19 |
||
| 418 | * @param IContextSource $context |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public static function getDebugHTML( IContextSource $context ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Generate debug log in HTML for displaying at the bottom of the main |
||
| 448 | * content area. |
||
| 449 | * If $wgShowDebug is false, an empty string is always returned. |
||
| 450 | * |
||
| 451 | * @since 1.20 |
||
| 452 | * @return string HTML fragment |
||
| 453 | */ |
||
| 454 | public static function getHTMLDebugLog() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Append the debug info to given ApiResult |
||
| 476 | * |
||
| 477 | * @param IContextSource $context |
||
| 478 | * @param ApiResult $result |
||
| 479 | */ |
||
| 480 | public static function appendDebugInfoToApiResult( IContextSource $context, ApiResult $result ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns the HTML to add to the page for the toolbar |
||
| 510 | * |
||
| 511 | * @param IContextSource $context |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | public static function getDebugInfo( IContextSource $context ) { |
||
| 550 | } |
||
| 551 |