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 |
||
| 15 | class Debug |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $_errors = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private $exit_on_error = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * echo the error if "checkForDev()" returns true |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $echo_on_error = true; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $css_mysql_box_border = '3px solid red'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $css_mysql_box_bg = '#FFCCCC'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $logger_class_name; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DB |
||
| 51 | */ |
||
| 52 | private $_db; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | * |
||
| 57 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 58 | */ |
||
| 59 | private $logger_level; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Debug constructor. |
||
| 63 | * |
||
| 64 | * @param DB $db |
||
| 65 | */ |
||
| 66 | 23 | public function __construct(DB $db) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Check is the current user is a developer. |
||
| 73 | * |
||
| 74 | * INFO: |
||
| 75 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 76 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 77 | * you can implement a global "checkForDev()"-function. |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | 40 | public function checkForDev(): bool |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Clear the errors in "$this->_errors". |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | 18 | public function clearErrors(): bool |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 126 | * |
||
| 127 | * @param string $error <p>The error message.</p> |
||
| 128 | * @param null|bool $force_exception_after_error <p> |
||
| 129 | * If you use default "null" here, then the behavior depends |
||
| 130 | * on "$this->exit_on_error (default: true)". |
||
| 131 | * </p> |
||
| 132 | * |
||
| 133 | * @throws QueryException |
||
| 134 | */ |
||
| 135 | 68 | public function displayError($error, $force_exception_after_error = null) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get errors from "$this->_errors". |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 15 | public function getErrors(): array |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Try to get the file & line from the current sql-query. |
||
| 201 | * |
||
| 202 | * @return array will return array['file'] and array['line'] |
||
| 203 | */ |
||
| 204 | 68 | private function getFileAndLineFromSql(): array |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getLoggerClassName(): string |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getLoggerLevel(): string |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function isEchoOnError(): bool |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isExitOnError(): bool |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Log the current query via "$this->logger". |
||
| 278 | * |
||
| 279 | * @param string $sql sql-query |
||
| 280 | * @param int $duration |
||
| 281 | * @param int $results field_count | insert_id | affected_rows |
||
| 282 | * @param bool $sql_error |
||
| 283 | * |
||
| 284 | * @return mixed <p>Will return false, if no logging was used.</p> |
||
| 285 | */ |
||
| 286 | 164 | public function logQuery($sql, $duration, $results, $sql_error = false) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Wrapper-Function for a "Logger"-Class. |
||
| 334 | * |
||
| 335 | * INFO: |
||
| 336 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 337 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 338 | * the text you want to log is the [1] element and<br /> |
||
| 339 | * the type you want to log is the next [2] element. |
||
| 340 | * |
||
| 341 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 342 | * |
||
| 343 | * @return mixed <p>Will return false, if no logging was used.</p> |
||
| 344 | */ |
||
| 345 | 71 | public function logger(array $log) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Send a error mail to the admin / dev. |
||
| 377 | * |
||
| 378 | * @param string $subject |
||
| 379 | * @param string $htmlBody |
||
| 380 | * @param int $priority |
||
| 381 | */ |
||
| 382 | 41 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param bool $echo_on_error |
||
| 416 | */ |
||
| 417 | 23 | public function setEchoOnError($echo_on_error) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param bool $exit_on_error |
||
| 424 | */ |
||
| 425 | 23 | public function setExitOnError($exit_on_error) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $logger_class_name |
||
| 432 | */ |
||
| 433 | 23 | public function setLoggerClassName($logger_class_name) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $logger_level |
||
| 440 | */ |
||
| 441 | 23 | public function setLoggerLevel($logger_level) |
|
| 445 | |||
| 446 | } |
||
| 447 |