Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Error 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 EE_Error, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class EE_Error extends Exception |
||
| 28 | { |
||
| 29 | |||
| 30 | const OPTIONS_KEY_NOTICES = 'ee_notices'; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * name of the file to log exceptions to |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private static $_exception_log_file = 'espresso_error_log.txt'; |
||
|
|
|||
| 39 | |||
| 40 | /** |
||
| 41 | * stores details for all exception |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $_all_exceptions = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * tracks number of errors |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private static $_error_count = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array $_espresso_notices |
||
| 56 | */ |
||
| 57 | private static $_espresso_notices = array('success' => false, 'errors' => false, 'attention' => false); |
||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @override default exception handling |
||
| 63 | * @param string $message |
||
| 64 | * @param int $code |
||
| 65 | * @param Exception|null $previous |
||
| 66 | */ |
||
| 67 | public function __construct($message, $code = 0, Exception $previous = null) |
||
| 68 | { |
||
| 69 | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
||
| 70 | parent::__construct($message, $code); |
||
| 71 | } else { |
||
| 72 | parent::__construct($message, $code, $previous); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * error_handler |
||
| 79 | * |
||
| 80 | * @param $code |
||
| 81 | * @param $message |
||
| 82 | * @param $file |
||
| 83 | * @param $line |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public static function error_handler($code, $message, $file, $line) |
||
| 87 | { |
||
| 88 | $type = EE_Error::error_type($code); |
||
| 89 | $site = site_url(); |
||
| 90 | switch ($site) { |
||
| 91 | case 'http://ee4.eventespresso.com/' : |
||
| 92 | case 'http://ee4decaf.eventespresso.com/' : |
||
| 93 | case 'http://ee4hf.eventespresso.com/' : |
||
| 94 | case 'http://ee4a.eventespresso.com/' : |
||
| 95 | case 'http://ee4ad.eventespresso.com/' : |
||
| 96 | case 'http://ee4b.eventespresso.com/' : |
||
| 97 | case 'http://ee4bd.eventespresso.com/' : |
||
| 98 | case 'http://ee4d.eventespresso.com/' : |
||
| 99 | case 'http://ee4dd.eventespresso.com/' : |
||
| 100 | $to = '[email protected]'; |
||
| 101 | break; |
||
| 102 | default : |
||
| 103 | $to = get_option('admin_email'); |
||
| 104 | } |
||
| 105 | $subject = $type . ' ' . $message . ' in ' . EVENT_ESPRESSO_VERSION . ' on ' . site_url(); |
||
| 106 | $msg = EE_Error::_format_error($type, $message, $file, $line); |
||
| 107 | if (function_exists('wp_mail')) { |
||
| 108 | add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
||
| 109 | wp_mail($to, $subject, $msg); |
||
| 110 | } |
||
| 111 | echo '<div id="message" class="espresso-notices error"><p>'; |
||
| 112 | echo $type . ': ' . $message . '<br />' . $file . ' line ' . $line; |
||
| 113 | echo '<br /></p></div>'; |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * error_type |
||
| 120 | * http://www.php.net/manual/en/errorfunc.constants.php#109430 |
||
| 121 | * |
||
| 122 | * @param $code |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public static function error_type($code) |
||
| 126 | { |
||
| 127 | switch ($code) { |
||
| 128 | case E_ERROR: // 1 // |
||
| 129 | return 'E_ERROR'; |
||
| 130 | case E_WARNING: // 2 // |
||
| 131 | return 'E_WARNING'; |
||
| 132 | case E_PARSE: // 4 // |
||
| 133 | return 'E_PARSE'; |
||
| 134 | case E_NOTICE: // 8 // |
||
| 135 | return 'E_NOTICE'; |
||
| 136 | case E_CORE_ERROR: // 16 // |
||
| 137 | return 'E_CORE_ERROR'; |
||
| 138 | case E_CORE_WARNING: // 32 // |
||
| 139 | return 'E_CORE_WARNING'; |
||
| 140 | case E_COMPILE_ERROR: // 64 // |
||
| 141 | return 'E_COMPILE_ERROR'; |
||
| 142 | case E_COMPILE_WARNING: // 128 // |
||
| 143 | return 'E_COMPILE_WARNING'; |
||
| 144 | case E_USER_ERROR: // 256 // |
||
| 145 | return 'E_USER_ERROR'; |
||
| 146 | case E_USER_WARNING: // 512 // |
||
| 147 | return 'E_USER_WARNING'; |
||
| 148 | case E_USER_NOTICE: // 1024 // |
||
| 149 | return 'E_USER_NOTICE'; |
||
| 150 | case E_STRICT: // 2048 // |
||
| 151 | return 'E_STRICT'; |
||
| 152 | case E_RECOVERABLE_ERROR: // 4096 // |
||
| 153 | return 'E_RECOVERABLE_ERROR'; |
||
| 154 | case E_DEPRECATED: // 8192 // |
||
| 155 | return 'E_DEPRECATED'; |
||
| 156 | case E_USER_DEPRECATED: // 16384 // |
||
| 157 | return 'E_USER_DEPRECATED'; |
||
| 158 | case E_ALL: // 16384 // |
||
| 159 | return 'E_ALL'; |
||
| 160 | } |
||
| 161 | return ''; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * fatal_error_handler |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public static function fatal_error_handler() |
||
| 172 | { |
||
| 173 | $last_error = error_get_last(); |
||
| 174 | if ($last_error['type'] === E_ERROR) { |
||
| 175 | EE_Error::error_handler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * _format_error |
||
| 183 | * |
||
| 184 | * @param $code |
||
| 185 | * @param $message |
||
| 186 | * @param $file |
||
| 187 | * @param $line |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | private static function _format_error($code, $message, $file, $line) |
||
| 191 | { |
||
| 192 | $html = "<table cellpadding='5'><thead bgcolor='#f8f8f8'><th>Item</th><th align='left'>Details</th></thead><tbody>"; |
||
| 193 | $html .= "<tr valign='top'><td><b>Code</b></td><td>$code</td></tr>"; |
||
| 194 | $html .= "<tr valign='top'><td><b>Error</b></td><td>$message</td></tr>"; |
||
| 195 | $html .= "<tr valign='top'><td><b>File</b></td><td>$file</td></tr>"; |
||
| 196 | $html .= "<tr valign='top'><td><b>Line</b></td><td>$line</td></tr>"; |
||
| 197 | $html .= '</tbody></table>'; |
||
| 198 | return $html; |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * set_content_type |
||
| 205 | * |
||
| 206 | * @param $content_type |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public static function set_content_type($content_type) |
||
| 210 | { |
||
| 211 | return 'text/html'; |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @return void |
||
| 218 | * @throws EE_Error |
||
| 219 | * @throws ReflectionException |
||
| 220 | */ |
||
| 221 | public function get_error() |
||
| 222 | { |
||
| 223 | if (apply_filters('FHEE__EE_Error__get_error__show_normal_exceptions', false)) { |
||
| 224 | throw $this; |
||
| 225 | } |
||
| 226 | // get separate user and developer messages if they exist |
||
| 227 | $msg = explode('||', $this->getMessage()); |
||
| 228 | $user_msg = $msg[0]; |
||
| 229 | $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
||
| 230 | $msg = WP_DEBUG ? $dev_msg : $user_msg; |
||
| 231 | // add details to _all_exceptions array |
||
| 232 | $x_time = time(); |
||
| 233 | self::$_all_exceptions[$x_time]['name'] = get_class($this); |
||
| 234 | self::$_all_exceptions[$x_time]['file'] = $this->getFile(); |
||
| 235 | self::$_all_exceptions[$x_time]['line'] = $this->getLine(); |
||
| 236 | self::$_all_exceptions[$x_time]['msg'] = $msg; |
||
| 237 | self::$_all_exceptions[$x_time]['code'] = $this->getCode(); |
||
| 238 | self::$_all_exceptions[$x_time]['trace'] = $this->getTrace(); |
||
| 239 | self::$_all_exceptions[$x_time]['string'] = $this->getTraceAsString(); |
||
| 240 | self::$_error_count++; |
||
| 241 | //add_action( 'shutdown', array( $this, 'display_errors' )); |
||
| 242 | $this->display_errors(); |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param bool $check_stored |
||
| 248 | * @param string $type_to_check |
||
| 249 | * @return bool |
||
| 250 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 251 | * @throws \InvalidArgumentException |
||
| 252 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 253 | * @throws InvalidInterfaceException |
||
| 254 | */ |
||
| 255 | public static function has_error($check_stored = false, $type_to_check = 'errors') |
||
| 256 | { |
||
| 257 | $has_error = isset(self::$_espresso_notices[$type_to_check]) |
||
| 258 | && ! empty(self::$_espresso_notices[$type_to_check]) |
||
| 259 | ? true |
||
| 260 | : false; |
||
| 261 | if ($check_stored && ! $has_error) { |
||
| 262 | $notices = EE_Error::getStoredNotices(); |
||
| 263 | foreach ($notices as $type => $notice) { |
||
| 264 | if ($type === $type_to_check && $notice) { |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | return $has_error; |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @echo string |
||
| 276 | * @throws \ReflectionException |
||
| 277 | */ |
||
| 278 | public function display_errors() |
||
| 279 | { |
||
| 280 | $trace_details = ''; |
||
| 281 | $output = ' |
||
| 282 | <style type="text/css"> |
||
| 283 | #ee-error-message { |
||
| 284 | max-width:90% !important; |
||
| 285 | margin: 0 5%; |
||
| 286 | } |
||
| 287 | .ee-error-dev-msg-pg, |
||
| 288 | .error .ee-error-dev-msg-pg { |
||
| 289 | padding:1em; |
||
| 290 | margin:0 0 1em; |
||
| 291 | border:2px solid #E44064; |
||
| 292 | background:#fff; |
||
| 293 | border-radius:3px; |
||
| 294 | } |
||
| 295 | #ee-trace-details { |
||
| 296 | padding:3px; |
||
| 297 | margin:0 0 1em; |
||
| 298 | border:1px solid #666; |
||
| 299 | background:#fff; |
||
| 300 | border-radius:3px; |
||
| 301 | } |
||
| 302 | #ee-trace-details table { |
||
| 303 | border:1px solid #666; |
||
| 304 | border-bottom:none; |
||
| 305 | background:#f9f9f9; |
||
| 306 | } |
||
| 307 | #ee-trace-details table th { |
||
| 308 | background:#eee; |
||
| 309 | border-bottom:1px solid #666; |
||
| 310 | } |
||
| 311 | #ee-trace-details table td { |
||
| 312 | border-bottom:1px solid #666; |
||
| 313 | } |
||
| 314 | #ee-trace-details table td.odd { |
||
| 315 | background:#f3f3f3; |
||
| 316 | } |
||
| 317 | .display-ee-error-trace-lnk { |
||
| 318 | color:blue; |
||
| 319 | cursor:pointer; |
||
| 320 | } |
||
| 321 | .display-ee-error-trace-lnk:hover { |
||
| 322 | text-decoration:underline; |
||
| 323 | } |
||
| 324 | .hidden { |
||
| 325 | display:none; |
||
| 326 | } |
||
| 327 | .small-text { |
||
| 328 | font-size: .85em; |
||
| 329 | line-height: 1.4em; |
||
| 330 | letter-spacing: 1px; |
||
| 331 | } |
||
| 332 | .lt-grey-text { |
||
| 333 | color: #a8a8a8; |
||
| 334 | } |
||
| 335 | </style> |
||
| 336 | <div id="ee-error-message" class="error">'; |
||
| 337 | if (! WP_DEBUG) { |
||
| 338 | $output .= ' |
||
| 339 | <p>'; |
||
| 340 | } |
||
| 341 | // cycle thru errors |
||
| 342 | foreach (self::$_all_exceptions as $time => $ex) { |
||
| 343 | $error_code = ''; |
||
| 344 | // process trace info |
||
| 345 | if (empty($ex['trace'])) { |
||
| 346 | $trace_details .= __( |
||
| 347 | 'Sorry, but no trace information was available for this exception.', |
||
| 348 | 'event_espresso' |
||
| 349 | ); |
||
| 350 | } else { |
||
| 351 | $trace_details .= ' |
||
| 352 | <div id="ee-trace-details"> |
||
| 353 | <table width="100%" border="0" cellpadding="5" cellspacing="0"> |
||
| 354 | <tr> |
||
| 355 | <th scope="col" align="right" style="width:2.5%;">#</th> |
||
| 356 | <th scope="col" align="right" style="width:3.5%;">Line</th> |
||
| 357 | <th scope="col" align="left" style="width:40%;">File</th> |
||
| 358 | <th scope="col" align="left">Class</th> |
||
| 359 | <th scope="col" align="left">Method( arguments )</th> |
||
| 360 | </tr>'; |
||
| 361 | $last_on_stack = count($ex['trace']) - 1; |
||
| 362 | // reverse array so that stack is in proper chronological order |
||
| 363 | $sorted_trace = array_reverse($ex['trace']); |
||
| 364 | foreach ($sorted_trace as $nmbr => $trace) { |
||
| 365 | $file = isset($trace['file']) ? $trace['file'] : ''; |
||
| 366 | $class = isset($trace['class']) ? $trace['class'] : ''; |
||
| 367 | $type = isset($trace['type']) ? $trace['type'] : ''; |
||
| 368 | $function = isset($trace['function']) ? $trace['function'] : ''; |
||
| 369 | $args = isset($trace['args']) ? $this->_convert_args_to_string($trace['args']) : ''; |
||
| 370 | $line = isset($trace['line']) ? $trace['line'] : ''; |
||
| 371 | $zebra = ($nmbr % 2) ? ' odd' : ''; |
||
| 372 | View Code Duplication | if (empty($file) && ! empty($class)) { |
|
| 373 | $a = new ReflectionClass($class); |
||
| 374 | $file = $a->getFileName(); |
||
| 375 | if (empty($line) && ! empty($function)) { |
||
| 376 | try { |
||
| 377 | //if $function is a closure, this throws an exception |
||
| 378 | $b = new ReflectionMethod($class, $function); |
||
| 379 | $line = $b->getStartLine(); |
||
| 380 | } catch (Exception $closure_exception) { |
||
| 381 | $line = 'unknown'; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | if ($nmbr === $last_on_stack) { |
||
| 386 | $file = $ex['file'] !== '' ? $ex['file'] : $file; |
||
| 387 | $line = $ex['line'] !== '' ? $ex['line'] : $line; |
||
| 388 | $error_code = self::generate_error_code($file, $trace['function'], $line); |
||
| 389 | } |
||
| 390 | $nmbr_dsply = ! empty($nmbr) ? $nmbr : ' '; |
||
| 391 | $line_dsply = ! empty($line) ? $line : ' '; |
||
| 392 | $file_dsply = ! empty($file) ? $file : ' '; |
||
| 393 | $class_dsply = ! empty($class) ? $class : ' '; |
||
| 394 | $type_dsply = ! empty($type) ? $type : ' '; |
||
| 395 | $function_dsply = ! empty($function) ? $function : ' '; |
||
| 396 | $args_dsply = ! empty($args) ? '( ' . $args . ' )' : ''; |
||
| 397 | $trace_details .= ' |
||
| 398 | <tr> |
||
| 399 | <td align="right" class="' . $zebra . '">' . $nmbr_dsply . '</td> |
||
| 400 | <td align="right" class="' . $zebra . '">' . $line_dsply . '</td> |
||
| 401 | <td align="left" class="' . $zebra . '">' . $file_dsply . '</td> |
||
| 402 | <td align="left" class="' . $zebra . '">' . $class_dsply . '</td> |
||
| 403 | <td align="left" class="' . $zebra . '">' . $type_dsply . $function_dsply . $args_dsply . '</td> |
||
| 404 | </tr>'; |
||
| 405 | } |
||
| 406 | $trace_details .= ' |
||
| 407 | </table> |
||
| 408 | </div>'; |
||
| 409 | } |
||
| 410 | $ex['code'] = $ex['code'] ? $ex['code'] : $error_code; |
||
| 411 | // add generic non-identifying messages for non-privileged users |
||
| 412 | if (! WP_DEBUG) { |
||
| 413 | $output .= '<span class="ee-error-user-msg-spn">' |
||
| 414 | . trim($ex['msg']) |
||
| 415 | . '</span> <sup>' |
||
| 416 | . $ex['code'] |
||
| 417 | . '</sup><br />'; |
||
| 418 | } else { |
||
| 419 | // or helpful developer messages if debugging is on |
||
| 420 | $output .= ' |
||
| 421 | <div class="ee-error-dev-msg-dv"> |
||
| 422 | <p class="ee-error-dev-msg-pg"> |
||
| 423 | <strong class="ee-error-dev-msg-str">An ' |
||
| 424 | . $ex['name'] |
||
| 425 | . ' exception was thrown!</strong> <span>code: ' |
||
| 426 | . $ex['code'] |
||
| 427 | . '</span><br /> |
||
| 428 | <span class="big-text">"' |
||
| 429 | . trim($ex['msg']) |
||
| 430 | . '"</span><br/> |
||
| 431 | <a id="display-ee-error-trace-' |
||
| 432 | . self::$_error_count |
||
| 433 | . $time |
||
| 434 | . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-' |
||
| 435 | . self::$_error_count |
||
| 436 | . $time |
||
| 437 | . '"> |
||
| 438 | ' |
||
| 439 | . __('click to view backtrace and class/method details', 'event_espresso') |
||
| 440 | . ' |
||
| 441 | </a><br /> |
||
| 442 | <span class="small-text lt-grey-text">' |
||
| 443 | . $ex['file'] |
||
| 444 | . ' ( line no: ' |
||
| 445 | . $ex['line'] |
||
| 446 | . ' )</span> |
||
| 447 | </p> |
||
| 448 | <div id="ee-error-trace-' |
||
| 449 | . self::$_error_count |
||
| 450 | . $time |
||
| 451 | . '-dv" class="ee-error-trace-dv" style="display: none;"> |
||
| 452 | ' |
||
| 453 | . $trace_details; |
||
| 454 | if (! empty($class)) { |
||
| 455 | $output .= ' |
||
| 456 | <div style="padding:3px; margin:0 0 1em; border:1px solid #666; background:#fff; border-radius:3px;"> |
||
| 457 | <div style="padding:1em 2em; border:1px solid #666; background:#f9f9f9;"> |
||
| 458 | <h3>Class Details</h3>'; |
||
| 459 | $a = new ReflectionClass($class); |
||
| 460 | $output .= ' |
||
| 461 | <pre>' . $a . '</pre> |
||
| 462 | </div> |
||
| 463 | </div>'; |
||
| 464 | } |
||
| 465 | $output .= ' |
||
| 466 | </div> |
||
| 467 | </div> |
||
| 468 | <br />'; |
||
| 469 | } |
||
| 470 | $this->write_to_error_log($time, $ex); |
||
| 471 | } |
||
| 472 | // remove last linebreak |
||
| 473 | $output = substr($output, 0, -6); |
||
| 474 | if (! WP_DEBUG) { |
||
| 475 | $output .= ' |
||
| 476 | </p>'; |
||
| 477 | } |
||
| 478 | $output .= ' |
||
| 479 | </div>'; |
||
| 480 | $output .= self::_print_scripts(true); |
||
| 481 | if (defined('DOING_AJAX')) { |
||
| 482 | echo wp_json_encode(array('error' => $output)); |
||
| 483 | exit(); |
||
| 484 | } |
||
| 485 | echo $output; |
||
| 486 | die(); |
||
| 487 | } |
||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * generate string from exception trace args |
||
| 493 | * |
||
| 494 | * @param array $arguments |
||
| 495 | * @param bool $array |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | private function _convert_args_to_string($arguments = array(), $array = false) |
||
| 499 | { |
||
| 500 | $arg_string = ''; |
||
| 501 | if (! empty($arguments)) { |
||
| 502 | $args = array(); |
||
| 503 | foreach ($arguments as $arg) { |
||
| 504 | if (! empty($arg)) { |
||
| 505 | if (is_string($arg)) { |
||
| 506 | $args[] = " '" . $arg . "'"; |
||
| 507 | } elseif (is_array($arg)) { |
||
| 508 | $args[] = 'ARRAY(' . $this->_convert_args_to_string($arg, true); |
||
| 509 | } elseif ($arg === null) { |
||
| 510 | $args[] = ' NULL'; |
||
| 511 | } elseif (is_bool($arg)) { |
||
| 512 | $args[] = ($arg) ? ' TRUE' : ' FALSE'; |
||
| 513 | } elseif (is_object($arg)) { |
||
| 514 | $args[] = ' OBJECT ' . get_class($arg); |
||
| 515 | } elseif (is_resource($arg)) { |
||
| 516 | $args[] = get_resource_type($arg); |
||
| 517 | } else { |
||
| 518 | $args[] = $arg; |
||
| 519 | } |
||
| 520 | } |
||
| 521 | } |
||
| 522 | $arg_string = implode(', ', $args); |
||
| 523 | } |
||
| 524 | if ($array) { |
||
| 525 | $arg_string .= ' )'; |
||
| 526 | } |
||
| 527 | return $arg_string; |
||
| 528 | } |
||
| 529 | |||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * add error message |
||
| 534 | * |
||
| 535 | * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
||
| 536 | * separate messages for user || dev |
||
| 537 | * @param string $file the file that the error occurred in - just use __FILE__ |
||
| 538 | * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
||
| 539 | * @param string $line the line number where the error occurred - just use __LINE__ |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | public static function add_error($msg = null, $file = null, $func = null, $line = null) |
||
| 543 | { |
||
| 544 | self::_add_notice('errors', $msg, $file, $func, $line); |
||
| 545 | self::$_error_count++; |
||
| 546 | } |
||
| 547 | |||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * If WP_DEBUG is active, throws an exception. If WP_DEBUG is off, just |
||
| 552 | * adds an error |
||
| 553 | * |
||
| 554 | * @param string $msg |
||
| 555 | * @param string $file |
||
| 556 | * @param string $func |
||
| 557 | * @param string $line |
||
| 558 | * @throws EE_Error |
||
| 559 | */ |
||
| 560 | public static function throw_exception_if_debugging($msg = null, $file = null, $func = null, $line = null) |
||
| 561 | { |
||
| 562 | if (WP_DEBUG) { |
||
| 563 | throw new EE_Error($msg); |
||
| 564 | } |
||
| 565 | EE_Error::add_error($msg, $file, $func, $line); |
||
| 566 | } |
||
| 567 | |||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * add success message |
||
| 572 | * |
||
| 573 | * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
||
| 574 | * separate messages for user || dev |
||
| 575 | * @param string $file the file that the error occurred in - just use __FILE__ |
||
| 576 | * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
||
| 577 | * @param string $line the line number where the error occurred - just use __LINE__ |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | public static function add_success($msg = null, $file = null, $func = null, $line = null) |
||
| 581 | { |
||
| 582 | self::_add_notice('success', $msg, $file, $func, $line); |
||
| 583 | } |
||
| 584 | |||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * add attention message |
||
| 589 | * |
||
| 590 | * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
||
| 591 | * separate messages for user || dev |
||
| 592 | * @param string $file the file that the error occurred in - just use __FILE__ |
||
| 593 | * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
||
| 594 | * @param string $line the line number where the error occurred - just use __LINE__ |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | public static function add_attention($msg = null, $file = null, $func = null, $line = null) |
||
| 598 | { |
||
| 599 | self::_add_notice('attention', $msg, $file, $func, $line); |
||
| 600 | } |
||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * @param string $type whether the message is for a success or error notification |
||
| 606 | * @param string $msg the message to display to users or developers |
||
| 607 | * - adding a double pipe || (OR) creates separate messages for user || dev |
||
| 608 | * @param string $file the file that the error occurred in - just use __FILE__ |
||
| 609 | * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
||
| 610 | * @param string $line the line number where the error occurred - just use __LINE__ |
||
| 611 | * @return void |
||
| 612 | */ |
||
| 613 | private static function _add_notice($type = 'success', $msg = '', $file = '', $func = '', $line = '') |
||
| 614 | { |
||
| 615 | if (empty($msg)) { |
||
| 616 | EE_Error::doing_it_wrong( |
||
| 617 | 'EE_Error::add_' . $type . '()', |
||
| 618 | sprintf( |
||
| 619 | __('Notifications are not much use without a message! Please add a message to the EE_Error::add_%s() call made in %s on line %d', |
||
| 620 | 'event_espresso'), |
||
| 621 | $type, |
||
| 622 | $file, |
||
| 623 | $line |
||
| 624 | ), |
||
| 625 | EVENT_ESPRESSO_VERSION |
||
| 626 | ); |
||
| 627 | } |
||
| 628 | if ($type === 'errors' && (empty($file) || empty($func) || empty($line))) { |
||
| 629 | EE_Error::doing_it_wrong( |
||
| 630 | 'EE_Error::add_error()', |
||
| 631 | __('You need to provide the file name, function name, and line number that the error occurred on in order to better assist with debugging.', |
||
| 632 | 'event_espresso'), |
||
| 633 | EVENT_ESPRESSO_VERSION |
||
| 634 | ); |
||
| 635 | } |
||
| 636 | // get separate user and developer messages if they exist |
||
| 637 | $msg = explode('||', $msg); |
||
| 638 | $user_msg = $msg[0]; |
||
| 639 | $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
||
| 640 | /** |
||
| 641 | * Do an action so other code can be triggered when a notice is created |
||
| 642 | * |
||
| 643 | * @param string $type can be 'errors', 'attention', or 'success' |
||
| 644 | * @param string $user_msg message displayed to user when WP_DEBUG is off |
||
| 645 | * @param string $user_msg message displayed to user when WP_DEBUG is on |
||
| 646 | * @param string $file file where error was generated |
||
| 647 | * @param string $func function where error was generated |
||
| 648 | * @param string $line line where error was generated |
||
| 649 | */ |
||
| 650 | do_action('AHEE__EE_Error___add_notice', $type, $user_msg, $dev_msg, $file, $func, $line); |
||
| 651 | $msg = WP_DEBUG ? $dev_msg : $user_msg; |
||
| 652 | // add notice if message exists |
||
| 653 | if (! empty($msg)) { |
||
| 654 | // get error code |
||
| 655 | $notice_code = EE_Error::generate_error_code($file, $func, $line); |
||
| 656 | if (WP_DEBUG && $type === 'errors') { |
||
| 657 | $msg .= '<br/><span class="tiny-text">' . $notice_code . '</span>'; |
||
| 658 | } |
||
| 659 | // add notice. Index by code if it's not blank |
||
| 660 | if ($notice_code) { |
||
| 661 | self::$_espresso_notices[$type][$notice_code] = $msg; |
||
| 662 | } else { |
||
| 663 | self::$_espresso_notices[$type][] = $msg; |
||
| 664 | } |
||
| 665 | add_action('wp_footer', array('EE_Error', 'enqueue_error_scripts'), 1); |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * in some case it may be necessary to overwrite the existing success messages |
||
| 672 | * |
||
| 673 | * @return void |
||
| 674 | */ |
||
| 675 | public static function overwrite_success() |
||
| 676 | { |
||
| 677 | self::$_espresso_notices['success'] = false; |
||
| 678 | } |
||
| 679 | |||
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * in some case it may be necessary to overwrite the existing attention messages |
||
| 684 | * |
||
| 685 | * @return void |
||
| 686 | */ |
||
| 687 | public static function overwrite_attention() |
||
| 688 | { |
||
| 689 | self::$_espresso_notices['attention'] = false; |
||
| 690 | } |
||
| 691 | |||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * in some case it may be necessary to overwrite the existing error messages |
||
| 696 | * |
||
| 697 | * @return void |
||
| 698 | */ |
||
| 699 | public static function overwrite_errors() |
||
| 700 | { |
||
| 701 | self::$_espresso_notices['errors'] = false; |
||
| 702 | } |
||
| 703 | |||
| 704 | |||
| 705 | |||
| 706 | /** |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | public static function reset_notices() |
||
| 715 | |||
| 716 | |||
| 717 | |||
| 718 | /** |
||
| 719 | * @return int |
||
| 720 | */ |
||
| 721 | public static function has_notices() |
||
| 722 | { |
||
| 723 | $has_notices = 0; |
||
| 724 | // check for success messages |
||
| 725 | $has_notices = self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success']) |
||
| 726 | ? 3 |
||
| 727 | : $has_notices; |
||
| 728 | // check for attention messages |
||
| 729 | $has_notices = self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention']) |
||
| 730 | ? 2 |
||
| 731 | : $has_notices; |
||
| 732 | // check for error messages |
||
| 733 | $has_notices = self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors']) |
||
| 738 | |||
| 739 | |||
| 740 | /** |
||
| 741 | * This simply returns non formatted error notices as they were sent into the EE_Error object. |
||
| 742 | * |
||
| 743 | * @since 4.9.0 |
||
| 744 | * @return array |
||
| 745 | */ |
||
| 746 | public static function get_vanilla_notices() |
||
| 760 | |||
| 761 | |||
| 762 | /** |
||
| 763 | * @return array |
||
| 764 | * @throws InvalidArgumentException |
||
| 765 | * @throws InvalidDataTypeException |
||
| 766 | * @throws InvalidInterfaceException |
||
| 767 | */ |
||
| 768 | public static function getStoredNotices() |
||
| 784 | |||
| 785 | |||
| 786 | /** |
||
| 787 | * @param array $notices |
||
| 788 | * @return bool |
||
| 789 | * @throws InvalidArgumentException |
||
| 790 | * @throws InvalidDataTypeException |
||
| 791 | * @throws InvalidInterfaceException |
||
| 792 | */ |
||
| 793 | public static function storeNotices(array $notices) |
||
| 812 | |||
| 813 | |||
| 814 | /** |
||
| 815 | * @return bool|TRUE |
||
| 816 | * @throws InvalidArgumentException |
||
| 817 | * @throws InvalidDataTypeException |
||
| 818 | * @throws InvalidInterfaceException |
||
| 819 | */ |
||
| 820 | public static function clearNotices() |
||
| 837 | |||
| 838 | |||
| 839 | /** |
||
| 840 | * saves notices to the db for retrieval on next request |
||
| 841 | * |
||
| 842 | * @return void |
||
| 843 | * @throws InvalidArgumentException |
||
| 844 | * @throws InvalidDataTypeException |
||
| 845 | * @throws InvalidInterfaceException |
||
| 846 | */ |
||
| 847 | public static function stashNoticesBeforeRedirect() |
||
| 851 | |||
| 852 | |||
| 853 | /** |
||
| 854 | * compile all error or success messages into one string |
||
| 855 | * |
||
| 856 | * @see EE_Error::get_raw_notices if you want the raw notices without any preparations made to them |
||
| 857 | * @param boolean $format_output whether or not to format the messages for display in the WP admin |
||
| 858 | * @param boolean $save_to_transient whether or not to save notices to the db for retrieval on next request |
||
| 859 | * - ONLY do this just before redirecting |
||
| 860 | * @param boolean $remove_empty whether or not to unset empty messages |
||
| 861 | * @return array |
||
| 862 | * @throws InvalidArgumentException |
||
| 863 | * @throws InvalidDataTypeException |
||
| 864 | * @throws InvalidInterfaceException |
||
| 865 | */ |
||
| 866 | public static function get_notices($format_output = true, $save_to_transient = false, $remove_empty = true) |
||
| 928 | |||
| 929 | |||
| 930 | /** |
||
| 931 | * @return bool |
||
| 932 | * @throws InvalidArgumentException |
||
| 933 | * @throws InvalidDataTypeException |
||
| 934 | * @throws InvalidInterfaceException |
||
| 935 | */ |
||
| 936 | private static function combineExistingAndNewNotices() |
||
| 959 | |||
| 960 | |||
| 961 | /** |
||
| 962 | * @param string $success_messages |
||
| 963 | * @param string $attention_messages |
||
| 964 | * @param string $error_messages |
||
| 965 | * @return string |
||
| 966 | */ |
||
| 967 | private static function formatNoticesOutput($success_messages, $attention_messages, $error_messages) |
||
| 1009 | |||
| 1010 | |||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * _print_scripts |
||
| 1014 | * |
||
| 1015 | * @param bool $force_print |
||
| 1016 | * @return string |
||
| 1017 | */ |
||
| 1018 | View Code Duplication | private static function _print_scripts($force_print = false) |
|
| 1044 | |||
| 1045 | |||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * @return void |
||
| 1049 | */ |
||
| 1050 | public static function enqueue_error_scripts() |
||
| 1054 | |||
| 1055 | |||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * create error code from filepath, function name, |
||
| 1059 | * and line number where exception or error was thrown |
||
| 1060 | * |
||
| 1061 | * @param string $file |
||
| 1062 | * @param string $func |
||
| 1063 | * @param string $line |
||
| 1064 | * @return string |
||
| 1065 | */ |
||
| 1066 | View Code Duplication | public static function generate_error_code($file = '', $func = '', $line = '') |
|
| 1074 | |||
| 1075 | |||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * write exception details to log file |
||
| 1079 | * Since 4.9.53.rc.006 this writes to the standard PHP log file, not EE's custom log file |
||
| 1080 | * |
||
| 1081 | * @param int $time |
||
| 1082 | * @param array $ex |
||
| 1083 | * @param bool $clear |
||
| 1084 | * @return void |
||
| 1085 | */ |
||
| 1086 | public function write_to_error_log($time = 0, $ex = array(), $clear = false) |
||
| 1112 | |||
| 1113 | |||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * This is just a wrapper for the EEH_Debug_Tools::instance()->doing_it_wrong() method. |
||
| 1117 | * doing_it_wrong() is used in those cases where a normal PHP error won't get thrown, |
||
| 1118 | * but the code execution is done in a manner that could lead to unexpected results |
||
| 1119 | * (i.e. running to early, or too late in WP or EE loading process). |
||
| 1120 | * A good test for knowing whether to use this method is: |
||
| 1121 | * 1. Is there going to be a PHP error if something isn't setup/used correctly? |
||
| 1122 | * Yes -> use EE_Error::add_error() or throw new EE_Error() |
||
| 1123 | * 2. If this is loaded before something else, it won't break anything, |
||
| 1124 | * but just wont' do what its supposed to do? Yes -> use EE_Error::doing_it_wrong() |
||
| 1125 | * |
||
| 1126 | * @uses constant WP_DEBUG test if wp_debug is on or not |
||
| 1127 | * @param string $function The function that was called |
||
| 1128 | * @param string $message A message explaining what has been done incorrectly |
||
| 1129 | * @param string $version The version of Event Espresso where the error was added |
||
| 1130 | * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
||
| 1131 | * for a deprecated function. This allows deprecation to occur during one version, |
||
| 1132 | * but not have any notices appear until a later version. This allows developers |
||
| 1133 | * extra time to update their code before notices appear. |
||
| 1134 | * @param int $error_type |
||
| 1135 | */ |
||
| 1136 | public static function doing_it_wrong( |
||
| 1147 | |||
| 1148 | |||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Like get_notices, but returns an array of all the notices of the given type. |
||
| 1152 | * |
||
| 1153 | * @return array { |
||
| 1154 | * @type array $success all the success messages |
||
| 1155 | * @type array $errors all the error messages |
||
| 1156 | * @type array $attention all the attention messages |
||
| 1157 | * } |
||
| 1158 | */ |
||
| 1159 | public static function get_raw_notices() |
||
| 1163 | |||
| 1164 | |||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * @deprecated 4.9.27 |
||
| 1168 | * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
||
| 1169 | * @param string $pan_message the message to be stored persistently until dismissed |
||
| 1170 | * @param bool $force_update allows one to enforce the reappearance of a persistent message. |
||
| 1171 | * @return void |
||
| 1172 | * @throws InvalidDataTypeException |
||
| 1173 | */ |
||
| 1174 | public static function add_persistent_admin_notice($pan_name = '', $pan_message, $force_update = false) |
||
| 1190 | |||
| 1191 | |||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * @deprecated 4.9.27 |
||
| 1195 | * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
||
| 1196 | * @param bool $purge |
||
| 1197 | * @param bool $return |
||
| 1198 | * @throws DomainException |
||
| 1199 | * @throws InvalidInterfaceException |
||
| 1200 | * @throws InvalidDataTypeException |
||
| 1201 | * @throws ServiceNotFoundException |
||
| 1202 | * @throws InvalidArgumentException |
||
| 1203 | */ |
||
| 1204 | public static function dismiss_persistent_admin_notice($pan_name = '', $purge = false, $return = false) |
||
| 1220 | |||
| 1221 | |||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * @deprecated 4.9.27 |
||
| 1225 | * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
||
| 1226 | * @param string $pan_message the message to be stored persistently until dismissed |
||
| 1227 | * @param string $return_url URL to go back to after nag notice is dismissed |
||
| 1228 | */ |
||
| 1229 | public static function display_persistent_admin_notices($pan_name = '', $pan_message = '', $return_url = '') |
||
| 1240 | |||
| 1241 | |||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * @deprecated 4.9.27 |
||
| 1245 | * @param string $return_url |
||
| 1246 | */ |
||
| 1247 | public static function get_persistent_admin_notices($return_url = '') |
||
| 1258 | |||
| 1259 | |||
| 1260 | |||
| 1261 | } |
||
| 1262 | // end of Class EE_Exceptions |
||
| 1301 |
This check marks private properties in classes that are never used. Those properties can be removed.