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 = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $exit_on_error = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * echo the error if "checkForDev()" returns true |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $echo_on_error = true; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $css_mysql_box_border = '3px solid red'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $css_mysql_box_bg = '#FFCCCC'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $logger_class_name; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var DB |
||
| 49 | */ |
||
| 50 | private $_db; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | * |
||
| 55 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 56 | */ |
||
| 57 | private $logger_level; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Debug constructor. |
||
| 61 | * |
||
| 62 | * @param DB $db |
||
| 63 | */ |
||
| 64 | 23 | public function __construct(DB $db) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Check is the current user is a developer. |
||
| 71 | * |
||
| 72 | * INFO: |
||
| 73 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 74 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 75 | * you can implement a global "checkForDev()"-function. |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | 12 | public function checkForDev(): bool |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Clear the errors in "$this->_errors". |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | 18 | public function clearErrors(): bool |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 124 | * |
||
| 125 | * @param string $error <p>The error message.</p> |
||
| 126 | * @param bool|null $force_exception_after_error <p> |
||
| 127 | * If you use default "null" here, then the behavior depends |
||
| 128 | * on "$this->exit_on_error (default: true)". |
||
| 129 | * </p> |
||
| 130 | * |
||
| 131 | * @throws QueryException |
||
| 132 | */ |
||
| 133 | 68 | public function displayError($error, $force_exception_after_error = null) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get errors from "$this->_errors". |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | 15 | public function getErrors(): array |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Try to get the file & line from the current sql-query. |
||
| 199 | * |
||
| 200 | * @return array will return array['file'] and array['line'] |
||
| 201 | */ |
||
| 202 | 68 | private function getFileAndLineFromSql(): array |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getLoggerClassName(): string |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getLoggerLevel(): string |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function isEchoOnError(): bool |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function isExitOnError(): bool |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Log the current query via "$this->logger". |
||
| 274 | * |
||
| 275 | * @param string $sql sql-query |
||
| 276 | * @param int $duration |
||
| 277 | * @param int $results field_count | insert_id | affected_rows |
||
| 278 | * @param bool $sql_error |
||
| 279 | * |
||
| 280 | * @return mixed <p>Will return false, if no logging was used.</p> |
||
| 281 | */ |
||
| 282 | 140 | public function logQuery($sql, $duration, $results, $sql_error = false) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Wrapper-Function for a "Logger"-Class. |
||
| 330 | * |
||
| 331 | * INFO: |
||
| 332 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 333 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 334 | * the text you want to log is the [1] element and<br /> |
||
| 335 | * the type you want to log is the next [2] element. |
||
| 336 | * |
||
| 337 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 338 | * |
||
| 339 | * @return mixed <p>Will return false, if no logging was used.</p> |
||
| 340 | */ |
||
| 341 | 71 | public function logger(array $log) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Send a error mail to the admin / dev. |
||
| 380 | * |
||
| 381 | * @param string $subject |
||
| 382 | * @param string $htmlBody |
||
| 383 | * @param int $priority |
||
| 384 | */ |
||
| 385 | 41 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @param bool $echo_on_error |
||
| 417 | */ |
||
| 418 | 23 | public function setEchoOnError($echo_on_error) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param bool $exit_on_error |
||
| 425 | */ |
||
| 426 | 23 | public function setExitOnError($exit_on_error) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $logger_class_name |
||
| 433 | */ |
||
| 434 | 23 | public function setLoggerClassName($logger_class_name) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $logger_level |
||
| 441 | */ |
||
| 442 | 23 | public function setLoggerLevel($logger_level) |
|
| 446 | } |
||
| 447 |