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 |
||
| 125 | * @param null|boolean $force_exception_after_error |
||
| 126 | * |
||
| 127 | * @throws \Exception |
||
| 128 | */ |
||
| 129 | 21 | public function displayError($error, $force_exception_after_error = null) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get errors from "$this->_errors". |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | 3 | public function getErrors() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Try to get the file & line from the current sql-query. |
||
| 186 | * |
||
| 187 | * @return array will return array['file'] and array['line'] |
||
| 188 | */ |
||
| 189 | 21 | private function getFileAndLineFromSql() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getLoggerClassName() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getLoggerLevel() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return boolean |
||
| 251 | */ |
||
| 252 | public function isEchoOnError() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return boolean |
||
| 259 | */ |
||
| 260 | public function isExitOnError() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Log the current query via "$this->logger". |
||
| 267 | * |
||
| 268 | * @param string $sql sql-query |
||
| 269 | * @param int $duration |
||
| 270 | * @param int $results field_count | insert_id | affected_rows |
||
| 271 | * @param bool $sql_error |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 35 | public function logQuery($sql, $duration, $results, $sql_error = false) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Wrapper-Function for a "Logger"-Class. |
||
| 318 | * |
||
| 319 | * INFO: |
||
| 320 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 321 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 322 | * the text you want to log is the [1] element and<br /> |
||
| 323 | * the type you want to log is the next [2] element. |
||
| 324 | * |
||
| 325 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | 22 | public function logger(array $log) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Send a error mail to the admin / dev. |
||
| 361 | * |
||
| 362 | * @param string $subject |
||
| 363 | * @param string $htmlBody |
||
| 364 | * @param int $priority |
||
| 365 | */ |
||
| 366 | 11 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param boolean $echo_on_error |
||
| 400 | */ |
||
| 401 | 10 | public function setEchoOnError($echo_on_error) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param boolean $exit_on_error |
||
| 408 | */ |
||
| 409 | 10 | public function setExitOnError($exit_on_error) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $logger_class_name |
||
| 416 | */ |
||
| 417 | 10 | public function setLoggerClassName($logger_class_name) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $logger_level |
||
| 424 | */ |
||
| 425 | 10 | public function setLoggerLevel($logger_level) |
|
| 429 | |||
| 430 | } |
||
| 431 |
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: