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 |
||
| 14 | class Debug |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $_errors = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private $exit_on_error = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * echo the error if "checkForDev()" returns true |
||
| 28 | * |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private $echo_on_error = true; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $css_mysql_box_border = '3px solid red'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $css_mysql_box_bg = '#FFCCCC'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $logger_class_name; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DB |
||
| 50 | */ |
||
| 51 | private $_db; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | * |
||
| 56 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 57 | */ |
||
| 58 | private $logger_level; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Debug constructor. |
||
| 62 | * |
||
| 63 | * @param DB $db |
||
| 64 | */ |
||
| 65 | 11 | public function __construct(DB $db) |
|
| 66 | { |
||
| 67 | 11 | $this->_db = $db; |
|
| 68 | 11 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Check is the current user is a developer. |
||
| 72 | * |
||
| 73 | * INFO: |
||
| 74 | * By default we will return "true" if the remote-ip-address is localhost or |
||
| 75 | * if the script is called via CLI. But you can also overwrite this method or |
||
| 76 | * you can implement a global "checkForDev()"-function. |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 24 | public function checkForDev() |
|
| 81 | { |
||
| 82 | // init |
||
| 83 | 24 | $return = false; |
|
| 84 | |||
| 85 | 24 | if (function_exists('checkForDev')) { |
|
| 86 | $return = checkForDev(); |
||
| 87 | } else { |
||
| 88 | |||
| 89 | // for testing with dev-address |
||
| 90 | 24 | $noDev = isset($_GET['noDev']) ? (int)$_GET['noDev'] : 0; |
|
| 91 | 24 | $remoteIpAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false; |
|
| 92 | |||
| 93 | if ( |
||
| 94 | $noDev != 1 |
||
| 95 | 24 | && |
|
| 96 | ( |
||
| 97 | $remoteIpAddress === '127.0.0.1' |
||
| 98 | 24 | || |
|
| 99 | $remoteIpAddress === '::1' |
||
| 100 | 24 | || |
|
| 101 | 24 | PHP_SAPI === 'cli' |
|
| 102 | 24 | ) |
|
| 103 | 24 | ) { |
|
| 104 | 24 | $return = true; |
|
| 105 | 24 | } |
|
| 106 | } |
||
| 107 | |||
| 108 | 24 | return $return; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Clear the errors in "$this->_errors". |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 6 | public function clearErrors() |
|
| 117 | { |
||
| 118 | 6 | $this->_errors = array(); |
|
| 119 | |||
| 120 | 6 | return true; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Display SQL-Errors or throw Exceptions (for dev). |
||
| 125 | * |
||
| 126 | * @param string $error <p>The error message.</p> |
||
| 127 | * @param null|boolean $force_exception_after_error <p> |
||
| 128 | * If you use default "null" here, then the behavior depends |
||
| 129 | * on "$this->exit_on_error (default: true)". |
||
| 130 | * </p> |
||
| 131 | * |
||
| 132 | * @throws QueryException |
||
| 133 | */ |
||
| 134 | 24 | public function displayError($error, $force_exception_after_error = null) |
|
| 135 | { |
||
| 136 | 24 | $fileInfo = $this->getFileAndLineFromSql(); |
|
| 137 | |||
| 138 | 24 | $this->logger( |
|
| 139 | array( |
||
| 140 | 24 | 'error', |
|
| 141 | 24 | '<strong>' . date( |
|
| 142 | 'd. m. Y G:i:s' |
||
| 143 | 24 | ) . ' (' . $fileInfo['file'] . ' line: ' . $fileInfo['line'] . ') (sql-error):</strong> ' . $error . '<br>', |
|
| 144 | ) |
||
| 145 | 24 | ); |
|
| 146 | |||
| 147 | 24 | $this->_errors[] = $error; |
|
| 148 | |||
| 149 | 24 | if ($this->checkForDev() === true) { |
|
| 150 | 24 | if ($this->echo_on_error) { |
|
| 151 | 6 | $box_border = $this->css_mysql_box_border; |
|
| 152 | 6 | $box_bg = $this->css_mysql_box_bg; |
|
| 153 | |||
| 154 | echo ' |
||
| 155 | 6 | <div class="OBJ-mysql-box" style="border:' . $box_border . '; background:' . $box_bg . '; padding:10px; margin:10px;"> |
|
| 156 | <b style="font-size:14px;">MYSQL Error:</b> |
||
| 157 | <code style="display:block;"> |
||
| 158 | 6 | file / line: ' . $fileInfo['file'] . ' / ' . $fileInfo['line'] . ' |
|
| 159 | 6 | ' . $error . ' |
|
| 160 | </code> |
||
| 161 | </div> |
||
| 162 | 6 | '; |
|
| 163 | 6 | } |
|
| 164 | 24 | } |
|
| 165 | |||
| 166 | if ( |
||
| 167 | $force_exception_after_error === true |
||
| 168 | 24 | || |
|
| 169 | ( |
||
| 170 | $force_exception_after_error === null |
||
| 171 | 21 | && |
|
| 172 | 10 | $this->exit_on_error === true |
|
| 173 | 10 | ) |
|
| 174 | 24 | ) { |
|
| 175 | 3 | throw new QueryException($error); |
|
| 176 | } |
||
| 177 | 21 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get errors from "$this->_errors". |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 5 | public function getErrors() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Try to get the file & line from the current sql-query. |
||
| 191 | * |
||
| 192 | * @return array will return array['file'] and array['line'] |
||
| 193 | */ |
||
| 194 | 24 | private function getFileAndLineFromSql() |
|
| 195 | { |
||
| 196 | // init |
||
| 197 | 24 | $return = array(); |
|
| 198 | 24 | $file = ''; |
|
| 199 | 24 | $line = ''; |
|
| 200 | |||
| 201 | 24 | if (Bootup::is_php('5.4') === true) { |
|
| 202 | $referrer = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10); |
||
| 203 | } else { |
||
| 204 | 24 | $referrer = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 205 | } |
||
| 206 | |||
| 207 | 24 | foreach ($referrer as $key => $ref) { |
|
| 208 | |||
| 209 | if ( |
||
| 210 | 24 | $ref['function'] === 'execSQL' |
|
| 211 | 24 | || |
|
| 212 | 24 | $ref['function'] === 'query' |
|
| 213 | 24 | || |
|
| 214 | 24 | $ref['function'] === 'qry' |
|
| 215 | 24 | || |
|
| 216 | 24 | $ref['function'] === 'execute' |
|
| 217 | 24 | || |
|
| 218 | 24 | $ref['function'] === 'insert' |
|
| 219 | 24 | || |
|
| 220 | 24 | $ref['function'] === 'update' |
|
| 221 | 24 | || |
|
| 222 | 24 | $ref['function'] === 'replace' |
|
| 223 | 24 | || |
|
| 224 | 24 | $ref['function'] === 'delete' |
|
| 225 | 24 | ) { |
|
| 226 | 19 | $file = $referrer[$key]['file']; |
|
| 227 | 19 | $line = $referrer[$key]['line']; |
|
| 228 | 19 | } |
|
| 229 | |||
| 230 | 24 | } |
|
| 231 | |||
| 232 | 24 | $return['file'] = $file; |
|
| 233 | 24 | $return['line'] = $line; |
|
| 234 | |||
| 235 | 24 | return $return; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getLoggerClassName() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getLoggerLevel() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | public function isEchoOnError() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | public function isExitOnError() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Log the current query via "$this->logger". |
||
| 272 | * |
||
| 273 | * @param string $sql sql-query |
||
| 274 | * @param int $duration |
||
| 275 | * @param int $results field_count | insert_id | affected_rows |
||
| 276 | * @param bool $sql_error |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 98 | public function logQuery($sql, $duration, $results, $sql_error = false) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Wrapper-Function for a "Logger"-Class. |
||
| 323 | * |
||
| 324 | * INFO: |
||
| 325 | * The "Logger"-ClassName is set by "$this->logger_class_name",<br /> |
||
| 326 | * the "Logger"-Method is the [0] element from the "$log"-parameter,<br /> |
||
| 327 | * the text you want to log is the [1] element and<br /> |
||
| 328 | * the type you want to log is the next [2] element. |
||
| 329 | * |
||
| 330 | * @param string[] $log [method, text, type]<br />e.g.: array('error', 'this is a error', 'sql') |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | 25 | public function logger(array $log) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Send a error mail to the admin / dev. |
||
| 366 | * |
||
| 367 | * @param string $subject |
||
| 368 | * @param string $htmlBody |
||
| 369 | * @param int $priority |
||
| 370 | */ |
||
| 371 | 15 | public function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param boolean $echo_on_error |
||
| 405 | */ |
||
| 406 | 11 | public function setEchoOnError($echo_on_error) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param boolean $exit_on_error |
||
| 413 | */ |
||
| 414 | 11 | public function setExitOnError($exit_on_error) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $logger_class_name |
||
| 421 | */ |
||
| 422 | 11 | public function setLoggerClassName($logger_class_name) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $logger_level |
||
| 429 | */ |
||
| 430 | 11 | public function setLoggerLevel($logger_level) |
|
| 434 | |||
| 435 | } |
||
| 436 |
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: