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 |
||
| 12 | class Debug |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $_errors = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | private $exit_on_error = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * echo the error if "checkForDev()" returns true |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | private $echo_on_error = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $css_mysql_box_border = '3px solid red'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $css_mysql_box_bg = '#FFCCCC'; |
||
| 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 | * define what a slow query is in ms |
||
| 60 | * |
||
| 61 | * @var float |
||
| 62 | */ |
||
| 63 | private $slowQueryTimeWarning = 0.005; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * define what a slow query is in ms |
||
| 67 | * |
||
| 68 | * @var float |
||
| 69 | */ |
||
| 70 | private $slowQueryTimeError = 0.1; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * define what a max query repeat is |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private $maxQueryRepeatWarning = 20; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * define what a max query repeat is |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | private $maxQueryRepeatError = 50; |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Debug constructor. |
||
| 89 | * |
||
| 90 | * @param DB $db |
||
| 91 | */ |
||
| 92 | 24 | public function __construct(DB $db) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Check is the current user is a developer. |
||
| 99 | * |
||
| 100 | * INFO: |
||
| 101 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 102 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 103 | * you can implement a global "checkForDev()"-function. |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | 12 | public function checkForDev(): bool |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Clear the errors in "$this->_errors". |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 18 | public function clearErrors(): bool |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 152 | * |
||
| 153 | * @param string $error <p>The error message.</p> |
||
| 154 | * @param bool|null $force_exception_after_error <p> |
||
| 155 | * If you use default "null" here, then the behavior depends |
||
| 156 | * on "$this->exit_on_error (default: true)". |
||
| 157 | * </p> |
||
| 158 | * |
||
| 159 | * @throws QueryException |
||
| 160 | */ |
||
| 161 | 69 | public function displayError($error, $force_exception_after_error = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get errors from "$this->_errors". |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 15 | public function getErrors(): array |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Try to get the file & line from the current sql-query. |
||
| 223 | * |
||
| 224 | * @return array will return array['path'] |
||
| 225 | */ |
||
| 226 | 69 | private function getFileAndLineFromSql(): array { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getLoggerClassName(): string |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getLoggerLevel(): string |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function isEchoOnError(): bool |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function isExitOnError(): bool |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Log the current query via "$this->logger". |
||
| 288 | * |
||
| 289 | * @param string $sql sql-query |
||
| 290 | * @param float|int $duration |
||
| 291 | * @param false|int|string|null $results field_count | insert_id | affected_rows |
||
| 292 | * @param bool $sql_error |
||
| 293 | * |
||
| 294 | * @return false|mixed |
||
| 295 | * <p>Will return false, if no logging was used.</p> |
||
| 296 | */ |
||
| 297 | 140 | public function logQuery($sql, $duration, $results, bool $sql_error = false) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Wrapper-Function for a "Logger"-Class. |
||
| 365 | * |
||
| 366 | * INFO: |
||
| 367 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 368 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 369 | * the text you want to log is the [1] element and<br /> |
||
| 370 | * the type you want to log is the next [2] element. |
||
| 371 | * |
||
| 372 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 373 | * |
||
| 374 | * @return false|mixed |
||
| 375 | * <p>Will return false, if no logging was used.</p> |
||
| 376 | */ |
||
| 377 | 72 | public function logger(array $log) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Send a error mail to the admin / dev. |
||
| 418 | * |
||
| 419 | * @param string $subject |
||
| 420 | * @param string $htmlBody |
||
| 421 | * @param int $priority |
||
| 422 | */ |
||
| 423 | 41 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param bool $echo_on_error |
||
| 440 | */ |
||
| 441 | 24 | public function setEchoOnError($echo_on_error) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @param bool $exit_on_error |
||
| 448 | */ |
||
| 449 | 24 | public function setExitOnError($exit_on_error) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $logger_class_name |
||
| 456 | */ |
||
| 457 | 24 | public function setLoggerClassName($logger_class_name) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $logger_level |
||
| 464 | */ |
||
| 465 | 24 | public function setLoggerLevel($logger_level) |
|
| 469 | } |
||
| 470 |