Complex classes like Debugger 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 Debugger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Debugger { |
||
| 10 | /** |
||
| 11 | * @var string|null |
||
| 12 | */ |
||
| 13 | public static $documentRoot = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var bool |
||
| 17 | */ |
||
| 18 | public static $showCalledFrom = true; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | public static $output = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private static $outputStyles = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $data |
||
| 32 | * |
||
| 33 | * @codeCoverageIgnore |
||
| 34 | */ |
||
| 35 | public static function output($data) { |
||
| 36 | if (!self::$output || !is_string($data)) { |
||
| 37 | return; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (php_sapi_name() == 'cli') { |
||
| 41 | echo '######################### DEBUG #########################'; |
||
| 42 | echo "\n"; |
||
| 43 | if (self::$showCalledFrom) { |
||
| 44 | echo self::getCalledFrom(2); |
||
| 45 | echo "\n"; |
||
| 46 | } |
||
| 47 | echo $data; |
||
| 48 | echo "\n"; |
||
| 49 | } else { |
||
| 50 | echo '<pre class="xicrow-php-debug-debugger">'; |
||
| 51 | if (self::$showCalledFrom) { |
||
| 52 | echo '<div class="xicrow-php-debug-debugger__called-from">'; |
||
| 53 | echo self::getCalledFrom(2); |
||
| 54 | echo '</div>'; |
||
| 55 | } |
||
| 56 | echo $data; |
||
| 57 | echo '</pre>'; |
||
| 58 | if (self::$outputStyles) { |
||
| 59 | echo '<style type="text/css">'; |
||
| 60 | echo 'pre.xicrow-php-debug-debugger{'; |
||
| 61 | echo 'margin:5px 0;'; |
||
| 62 | echo 'padding:5px;'; |
||
| 63 | echo 'font-family:Consolas,Courier New,monospace;'; |
||
| 64 | echo 'font-weight:normal;'; |
||
| 65 | echo 'font-size:13px;'; |
||
| 66 | echo 'line-height:1.2;'; |
||
| 67 | echo 'color:#555;'; |
||
| 68 | echo 'background:#FFF;'; |
||
| 69 | echo 'border:1px solid #CCC;'; |
||
| 70 | echo 'display:block;'; |
||
| 71 | echo 'overflow:auto;'; |
||
| 72 | echo '}'; |
||
| 73 | echo 'pre.xicrow-php-debug-debugger div.xicrow-php-debug-debugger__called-from{'; |
||
| 74 | echo 'margin: 0 0 5px 0;'; |
||
| 75 | echo 'padding: 0 0 5px 0;'; |
||
| 76 | echo 'border-bottom: 1px solid #CCC;'; |
||
| 77 | echo 'font-style: italic;'; |
||
| 78 | echo '}'; |
||
| 79 | echo '</style>'; |
||
| 80 | |||
| 81 | self::$outputStyles = false; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param mixed $data |
||
| 88 | * |
||
| 89 | * @codeCoverageIgnore |
||
| 90 | */ |
||
| 91 | public static function debug($data) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param bool $reverse |
||
| 97 | * |
||
| 98 | * @codeCoverageIgnore |
||
| 99 | */ |
||
| 100 | public static function showTrace($reverse = false) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $class |
||
| 118 | * @param bool $output |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 1 | public static function reflectClass($class, $output = true) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $class |
||
| 164 | * @param string $property |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 2 | public static function reflectClassProperty($class, $property, $output = true) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $class |
||
| 204 | * @param string $method |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 2 | public static function reflectClassMethod($class, $method, $output = true) { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param int $index |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 1 | public static function getCalledFrom($index = 0) { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param array $trace |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | 1 | public static function getCalledFromTrace($trace) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param mixed $data |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 1 | public static function getDebugInformation($data, array $options = []) { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 1 | private static function getDebugInformationNull() { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param boolean $data |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | 1 | private static function getDebugInformationBoolean($data) { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param integer $data |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 1 | private static function getDebugInformationInteger($data) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param double $data |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | 1 | private static function getDebugInformationDouble($data) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param array $data |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 1 | private static function getDebugInformationArray($data, array $options = []) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param object $data |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 1 | private static function getDebugInformationObject($data, array $options = []) { |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param resource $data |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 1 | private static function getDebugInformationResource($data) { |
|
| 479 | } |
||
| 480 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: