Complex classes like Debug 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 Debug, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Debug |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $_errors = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $exit_on_error = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $echo_on_error = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $css_mysql_box_border = '3px solid red'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $css_mysql_box_bg = '#FFCCCC'; |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $logger_class_name; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DB |
||
| 48 | */ |
||
| 49 | private $_db; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | * |
||
| 54 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 55 | */ |
||
| 56 | private $logger_level; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Debug constructor. |
||
| 60 | * |
||
| 61 | * @param DB $db |
||
| 62 | */ |
||
| 63 | 10 | public function __construct(DB $db) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Check is the current user is a developer. |
||
| 70 | * |
||
| 71 | * INFO: |
||
| 72 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 73 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 74 | * you can implement a global "checkForDev()"-function. |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 21 | public function checkForDev() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Clear the errors in "$this->_errors". |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 4 | public function clearErrors() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 123 | * |
||
| 124 | * @param string $error <p>The error message.</p> |
||
| 125 | * @param null|boolean $force_exception_after_error <p> |
||
| 126 | * If you use default "null" here, then the behavior depends |
||
| 127 | * on "$this->exit_on_error (default: true)". |
||
| 128 | * </p> |
||
| 129 | * |
||
| 130 | * @throws \Exception |
||
| 131 | */ |
||
| 132 | 21 | public function displayError($error, $force_exception_after_error = null) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get errors from "$this->_errors". |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 3 | public function getErrors() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Try to get the file & line from the current sql-query. |
||
| 191 | * |
||
| 192 | * @return array will return array['file'] and array['line'] |
||
| 193 | */ |
||
| 194 | 21 | private function getFileAndLineFromSql() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getLoggerClassName() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getLoggerLevel() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | public function isEchoOnError() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | public function isExitOnError() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Log the current query via "$this->logger". |
||
| 272 | * |
||
| 273 | * @param string $sql sql-query |
||
| 274 | * @param int $duration |
||
| 275 | * @param int $results field_count | insert_id | affected_rows |
||
| 276 | * @param bool $sql_error |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 36 | public function logQuery($sql, $duration, $results, $sql_error = false) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Wrapper-Function for a "Logger"-Class. |
||
| 323 | * |
||
| 324 | * INFO: |
||
| 325 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 326 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 327 | * the text you want to log is the [1] element and<br /> |
||
| 328 | * the type you want to log is the next [2] element. |
||
| 329 | * |
||
| 330 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | 22 | public function logger(array $log) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Send a error mail to the admin / dev. |
||
| 366 | * |
||
| 367 | * @param string $subject |
||
| 368 | * @param string $htmlBody |
||
| 369 | * @param int $priority |
||
| 370 | */ |
||
| 371 | 11 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param boolean $echo_on_error |
||
| 405 | */ |
||
| 406 | 10 | public function setEchoOnError($echo_on_error) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param boolean $exit_on_error |
||
| 413 | */ |
||
| 414 | 10 | public function setExitOnError($exit_on_error) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $logger_class_name |
||
| 421 | */ |
||
| 422 | 10 | public function setLoggerClassName($logger_class_name) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $logger_level |
||
| 429 | */ |
||
| 430 | 10 | public function setLoggerLevel($logger_level) |
|
| 434 | |||
| 435 | } |
||
| 436 |
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: