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 | * @param string $data |
||
| 27 | * @param array $options |
||
| 28 | * |
||
| 29 | * @codeCoverageIgnore |
||
| 30 | */ |
||
| 31 | public static function output($data, array $options = []) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param mixed $data |
||
| 63 | * @param array $options |
||
| 64 | * |
||
| 65 | * @codeCoverageIgnore |
||
| 66 | */ |
||
| 67 | public static function debug($data, array $options = []) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $options |
||
| 77 | * |
||
| 78 | * @codeCoverageIgnore |
||
| 79 | */ |
||
| 80 | public static function showTrace(array $options = []) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $class |
||
| 103 | * @param bool $output |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 1 | public static function reflectClass($class, $output = true) { |
|
| 108 | 1 | $data = ''; |
|
| 109 | |||
| 110 | 1 | $reflectionClass = new \ReflectionClass($class); |
|
| 111 | |||
| 112 | 1 | $comment = $reflectionClass->getDocComment(); |
|
| 113 | 1 | if (!empty($comment)) { |
|
| 114 | 1 | $data .= $comment; |
|
| 115 | 1 | $data .= "\n"; |
|
| 116 | 1 | } |
|
| 117 | |||
| 118 | 1 | $data .= 'class ' . $reflectionClass->name . '{'; |
|
| 119 | 1 | $firstElement = true; |
|
| 120 | 1 | foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
|
| 121 | 1 | if (!$firstElement) { |
|
| 122 | 1 | $data .= "\n"; |
|
| 123 | 1 | } |
|
| 124 | 1 | $firstElement = false; |
|
| 125 | |||
| 126 | 1 | $data .= self::reflectClassProperty($class, $reflectionProperty->name, false); |
|
| 127 | 1 | } |
|
| 128 | |||
| 129 | 1 | foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
|
| 130 | 1 | if (!$firstElement) { |
|
| 131 | 1 | $data .= "\n"; |
|
| 132 | 1 | } |
|
| 133 | 1 | $firstElement = false; |
|
| 134 | |||
| 135 | 1 | $data .= self::reflectClassMethod($class, $reflectionMethod->name, false); |
|
| 136 | 1 | } |
|
| 137 | 1 | $data .= "\n"; |
|
| 138 | 1 | $data .= '}'; |
|
| 139 | |||
| 140 | 1 | if ($output) { |
|
| 141 | 1 | self::output($data); |
|
| 142 | 1 | } |
|
| 143 | |||
| 144 | 1 | return $data; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $class |
||
| 149 | * @param string $property |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 2 | public static function reflectClassProperty($class, $property, $output = true) { |
|
| 154 | 2 | $data = ''; |
|
| 155 | |||
| 156 | 2 | $reflectionClass = new \ReflectionClass($class); |
|
| 157 | 2 | $reflectionProperty = new \ReflectionProperty($class, $property); |
|
| 158 | |||
| 159 | 2 | $defaultPropertyValues = $reflectionClass->getDefaultProperties(); |
|
| 160 | |||
| 161 | 2 | $comment = $reflectionProperty->getDocComment(); |
|
| 162 | 2 | if (!empty($comment)) { |
|
| 163 | 2 | $data .= "\n"; |
|
| 164 | 2 | $data .= "\t"; |
|
| 165 | 2 | $data .= $comment; |
|
| 166 | 2 | } |
|
| 167 | |||
| 168 | 2 | $data .= "\n"; |
|
| 169 | 2 | $data .= "\t"; |
|
| 170 | 2 | $data .= ($reflectionProperty->isPublic() ? 'public ' : ''); |
|
| 171 | 2 | $data .= ($reflectionProperty->isPrivate() ? 'private ' : ''); |
|
| 172 | 2 | $data .= ($reflectionProperty->isProtected() ? 'protected ' : ''); |
|
| 173 | 2 | $data .= ($reflectionProperty->isStatic() ? 'static ' : ''); |
|
| 174 | 2 | $data .= '$' . $reflectionProperty->name; |
|
| 175 | 2 | if (isset($defaultPropertyValues[$property])) { |
|
| 176 | 2 | $data .= ' = ' . self::getDebugInformation($defaultPropertyValues[$property]); |
|
| 177 | 2 | } |
|
| 178 | 2 | $data .= ';'; |
|
| 179 | |||
| 180 | 2 | if ($output) { |
|
| 181 | 1 | self::output($data); |
|
| 182 | 1 | } |
|
| 183 | |||
| 184 | 2 | return $data; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $class |
||
| 189 | * @param string $method |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 2 | public static function reflectClassMethod($class, $method, $output = true) { |
|
| 194 | 2 | $data = ''; |
|
| 195 | |||
| 196 | 2 | $reflectionMethod = new \ReflectionMethod($class, $method); |
|
| 197 | |||
| 198 | 2 | $comment = $reflectionMethod->getDocComment(); |
|
| 199 | 2 | if (!empty($comment)) { |
|
| 200 | 2 | $data .= "\n"; |
|
| 201 | 2 | $data .= "\t"; |
|
| 202 | 2 | $data .= $comment; |
|
| 203 | 2 | } |
|
| 204 | |||
| 205 | 2 | $data .= "\n"; |
|
| 206 | 2 | $data .= "\t"; |
|
| 207 | 2 | $data .= ($reflectionMethod->isPublic() ? 'public ' : ''); |
|
| 208 | 2 | $data .= ($reflectionMethod->isPrivate() ? 'private ' : ''); |
|
| 209 | 2 | $data .= ($reflectionMethod->isProtected() ? 'protected ' : ''); |
|
| 210 | 2 | $data .= ($reflectionMethod->isStatic() ? 'static ' : ''); |
|
| 211 | 2 | $data .= 'function ' . $reflectionMethod->name . '('; |
|
| 212 | 2 | if ($reflectionMethod->getNumberOfParameters()) { |
|
| 213 | 2 | foreach ($reflectionMethod->getParameters() as $reflectionMethodParameterIndex => $reflectionMethodParameter) { |
|
| 214 | 2 | $data .= ($reflectionMethodParameterIndex > 0 ? ', ' : ''); |
|
| 215 | 2 | $data .= '$' . $reflectionMethodParameter->name; |
|
| 216 | 2 | if ($reflectionMethodParameter->isDefaultValueAvailable()) { |
|
| 217 | 2 | $defaultValue = self::getDebugInformation($reflectionMethodParameter->getDefaultValue()); |
|
| 218 | 2 | $defaultValue = str_replace(["\n", "\t"], '', $defaultValue); |
|
| 219 | 2 | $data .= ' = ' . $defaultValue; |
|
| 220 | 2 | } |
|
| 221 | 2 | } |
|
| 222 | 2 | } |
|
| 223 | 2 | $data .= ') {}'; |
|
| 224 | |||
| 225 | 2 | if ($output) { |
|
| 226 | 1 | self::output($data); |
|
| 227 | 1 | } |
|
| 228 | |||
| 229 | 2 | return $data; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $index |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 1 | public static function getCalledFrom($index = 0) { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param array $trace |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 1 | public static function getCalledFromTrace($trace) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param mixed $data |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 1 | public static function getDebugInformation($data, array $options = []) { |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 1 | private static function getDebugInformationNull() { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param boolean $data |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 1 | private static function getDebugInformationBoolean($data) { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param integer $data |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | 1 | private static function getDebugInformationInteger($data) { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param double $data |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 1 | private static function getDebugInformationDouble($data) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $data |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 1 | private static function getDebugInformationArray($data, array $options = []) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param object $data |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 1 | private static function getDebugInformationObject($data, array $options = []) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @param resource $data |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | 1 | private static function getDebugInformationResource($data) { |
|
| 464 | } |
||
| 465 |
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: