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 Error_Handler 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 Error_Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Error_Handler { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The name of the option that stores the errors |
||
| 47 | * |
||
| 48 | * @since 8.7.0 |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | const STORED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_errors'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The name of the option that stores the errors |
||
| 56 | * |
||
| 57 | * @since 8.7.0 |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | const STORED_VERIFIED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_verified_errors'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The prefix of the transient that controls the gate for each error code |
||
| 65 | * |
||
| 66 | * @since 8.7.0 |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | const ERROR_REPORTING_GATE = 'jetpack_connection_error_reporting_gate_'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Time in seconds a test should live in the database before being discarded |
||
| 74 | * |
||
| 75 | * @since 8.7.0 |
||
| 76 | */ |
||
| 77 | const ERROR_LIFE_TIME = DAY_IN_SECONDS; |
||
| 78 | /** |
||
| 79 | * List of known errors. Only error codes in this list will be handled |
||
| 80 | * |
||
| 81 | * @since 8.7.0 |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | public $known_errors = array( |
||
| 86 | 'malformed_token', |
||
| 87 | 'malformed_user_id', |
||
| 88 | 'unknown_user', |
||
| 89 | 'no_user_tokens', |
||
| 90 | 'empty_master_user_option', |
||
| 91 | 'no_token_for_user', |
||
| 92 | 'token_malformed', |
||
| 93 | 'user_id_mismatch', |
||
| 94 | 'no_possible_tokens', |
||
| 95 | 'no_valid_token', |
||
| 96 | 'unknown_token', |
||
| 97 | 'could_not_sign', |
||
| 98 | 'invalid_scheme', |
||
| 99 | 'invalid_secret', |
||
| 100 | 'invalid_token', |
||
| 101 | 'token_mismatch', |
||
| 102 | 'invalid_body', |
||
| 103 | 'invalid_signature', |
||
| 104 | 'invalid_body_hash', |
||
| 105 | 'invalid_nonce', |
||
| 106 | 'signature_mismatch', |
||
| 107 | ); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Holds the instance of this singleton class |
||
| 111 | * |
||
| 112 | * @since 8.7.0 |
||
| 113 | * |
||
| 114 | * @var Error_Handler $instance |
||
| 115 | */ |
||
| 116 | public static $instance = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Initialize instance, hookds and load verified errors handlers |
||
| 120 | * |
||
| 121 | * @since 8.7.0 |
||
| 122 | */ |
||
| 123 | private function __construct() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Gets the list of verified errors and act upon them |
||
| 137 | * |
||
| 138 | * @since 8.7.0 |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function handle_verified_errors() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets the instance of this singleton class |
||
| 174 | * |
||
| 175 | * @since 8.7.0 |
||
| 176 | * |
||
| 177 | * @return Error_Handler $instance |
||
| 178 | */ |
||
| 179 | public static function get_instance() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Keep track of a connection error that was encountered |
||
| 188 | * |
||
| 189 | * @since 8.7.0 |
||
| 190 | * |
||
| 191 | * @param \WP_Error $error the error object. |
||
| 192 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Checks the status of the gate |
||
| 206 | * |
||
| 207 | * This protects the site (and WPCOM) against over loads. |
||
| 208 | * |
||
| 209 | * @since 8.7.0 |
||
| 210 | * |
||
| 211 | * @param \WP_Error $error the error object. |
||
| 212 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 213 | */ |
||
| 214 | public function should_report_error( \WP_Error $error ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 249 | * |
||
| 250 | * @since 8.7.0 |
||
| 251 | * |
||
| 252 | * @param \WP_Error $error the error object. |
||
| 253 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 254 | */ |
||
| 255 | public function store_error( \WP_Error $error ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Converts a WP_Error object in the array representation we store in the database |
||
| 285 | * |
||
| 286 | * @since 8.7.0 |
||
| 287 | * |
||
| 288 | * @param \WP_Error $error the error object. |
||
| 289 | * @return boolean|array False if error is invalid or the error array |
||
| 290 | */ |
||
| 291 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Sends the error to WP.com to be verified |
||
| 322 | * |
||
| 323 | * @since 8.7.0 |
||
| 324 | * |
||
| 325 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function send_error_to_wpcom( $error_array ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Encrypt data to be sent over to WP.com |
||
| 351 | * |
||
| 352 | * @since 8.7.0 |
||
| 353 | * |
||
| 354 | * @param array|string $data the data to be encoded. |
||
| 355 | * @return boolean|string The encoded string on success, false on failure |
||
| 356 | */ |
||
| 357 | public function encrypt_data_to_wpcom( $data ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Extracts the user ID from a token |
||
| 376 | * |
||
| 377 | * @since 8.7.0 |
||
| 378 | * |
||
| 379 | * @param string $token the token used to make the xml-rpc request. |
||
| 380 | * @return string $the user id or `invalid` if user id not present. |
||
| 381 | */ |
||
| 382 | public function get_user_id_from_token( $token ) { |
||
| 383 | $parsed_token = explode( ':', wp_unslash( $token ) ); |
||
| 384 | |||
| 385 | if ( isset( $parsed_token[2] ) && ctype_digit( $parsed_token[2] ) ) { |
||
| 386 | $user_id = $parsed_token[2]; |
||
| 387 | } else { |
||
| 388 | $user_id = 'invalid'; |
||
| 389 | } |
||
| 390 | |||
| 391 | return $user_id; |
||
| 392 | |||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Gets the reported errors stored in the database |
||
| 397 | * |
||
| 398 | * @since 8.7.0 |
||
| 399 | * |
||
| 400 | * @return array $errors |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function get_stored_errors() { |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Gets the verified errors stored in the database |
||
| 417 | * |
||
| 418 | * @since 8.7.0 |
||
| 419 | * |
||
| 420 | * @return array $errors |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function get_verified_errors() { |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Removes expired errors from the array |
||
| 437 | * |
||
| 438 | * This method is called by get_stored_errors and get_verified errors and filters their result |
||
| 439 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
| 440 | * expired error will be permantently removed from the database |
||
| 441 | * |
||
| 442 | * @since 8.7.0 |
||
| 443 | * |
||
| 444 | * @param array $errors array of errors as stored in the database. |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | private function garbage_collector( $errors ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Delete all stored and verified errors from the database |
||
| 467 | * |
||
| 468 | * @since 8.7.0 |
||
| 469 | * |
||
| 470 | * @return void |
||
| 471 | */ |
||
| 472 | public function delete_all_errors() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Delete the reported errors stored in the database |
||
| 479 | * |
||
| 480 | * @since 8.7.0 |
||
| 481 | * |
||
| 482 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 483 | */ |
||
| 484 | public function delete_stored_errors() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Delete the verified errors stored in the database |
||
| 490 | * |
||
| 491 | * @since 8.7.0 |
||
| 492 | * |
||
| 493 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 494 | */ |
||
| 495 | public function delete_verified_errors() { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Gets an error based on the nonce |
||
| 501 | * |
||
| 502 | * Receives a nonce and finds the related error. |
||
| 503 | * |
||
| 504 | * @since 8.7.0 |
||
| 505 | * |
||
| 506 | * @param string $nonce The nonce created for the error we want to get. |
||
| 507 | * @return null|array Returns the error array representation or null if error not found. |
||
| 508 | */ |
||
| 509 | public function get_error_by_nonce( $nonce ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Adds an error to the verified error list |
||
| 523 | * |
||
| 524 | * @since 8.7.0 |
||
| 525 | * |
||
| 526 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function verify_error( $error ) { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Register REST API end point for error hanlding. |
||
| 547 | * |
||
| 548 | * @since 8.7.0 |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | public function register_verify_error_endpoint() { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 572 | * |
||
| 573 | * @since 8.7.0 |
||
| 574 | * |
||
| 575 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 576 | * |
||
| 577 | * @return boolean |
||
| 578 | */ |
||
| 579 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Prints a generic error notice for all connection errors |
||
| 594 | * |
||
| 595 | * @since 8.9.0 |
||
| 596 | * |
||
| 597 | * @return void |
||
| 598 | */ |
||
| 599 | public function generic_admin_notice_error() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Adds the error message to the Jetpack React Dashboard |
||
| 648 | * |
||
| 649 | * @since 8.9.0 |
||
| 650 | * |
||
| 651 | * @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | public function jetpack_react_dashboard_error( $errors ) { |
||
| 663 | |||
| 664 | } |
||
| 665 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.