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 | 10 | public function __construct(DB $db) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Check is the current user is a developer. |
||
| 65 | * |
||
| 66 | * INFO: |
||
| 67 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 68 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 69 | * you can implement a global "checkForDev()"-function. |
||
| 70 | * |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | 20 | public function checkForDev() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Clear the errors in "$this->_errors". |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 4 | public function clearErrors() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 118 | * |
||
| 119 | * @param string $error |
||
| 120 | * @param null|boolean $force_exception_after_error |
||
| 121 | * |
||
| 122 | * @throws \Exception |
||
| 123 | */ |
||
| 124 | 20 | public function displayError($error, $force_exception_after_error = null) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get errors from "$this->_errors". |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 3 | public function getErrors() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Try to get the file & line from the current sql-query. |
||
| 181 | * |
||
| 182 | * @return array will return array['file'] and array['line'] |
||
| 183 | */ |
||
| 184 | 20 | private function getFileAndLineFromSql() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getLoggerClassName() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getLoggerLevel() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | public function isEchoOnError() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return boolean |
||
| 252 | */ |
||
| 253 | public function isExitOnError() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Log the current query via "$this->logger". |
||
| 260 | * |
||
| 261 | * @param string $sql sql-query |
||
| 262 | * @param int $duration |
||
| 263 | * @param int $results result counter |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 25 | public function logQuery($sql, $duration, $results) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Wrapper-Function for a "Logger"-Class. |
||
| 296 | * |
||
| 297 | * INFO: |
||
| 298 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 299 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 300 | * the text you want to log is the [1] element and<br /> |
||
| 301 | * the type you want to log is the next [2] element. |
||
| 302 | * |
||
| 303 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 304 | */ |
||
| 305 | 21 | public function logger(array $log) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Send a error mail to the admin / dev. |
||
| 335 | * |
||
| 336 | * @param string $subject |
||
| 337 | * @param string $htmlBody |
||
| 338 | * @param int $priority |
||
| 339 | */ |
||
| 340 | 10 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param boolean $echo_on_error |
||
| 374 | */ |
||
| 375 | 10 | public function setEchoOnError($echo_on_error) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param boolean $exit_on_error |
||
| 382 | */ |
||
| 383 | 10 | public function setExitOnError($exit_on_error) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $logger_class_name |
||
| 390 | */ |
||
| 391 | 10 | public function setLoggerClassName($logger_class_name) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $logger_level |
||
| 398 | */ |
||
| 399 | 10 | public function setLoggerLevel($logger_level) |
|
| 403 | |||
| 404 | } |
||
| 405 |
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: