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() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Gets the list of verified errors and act upon them |
||
| 140 | * |
||
| 141 | * @since 8.7.0 |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public function handle_verified_errors() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Gets the instance of this singleton class |
||
| 177 | * |
||
| 178 | * @since 8.7.0 |
||
| 179 | * |
||
| 180 | * @return Error_Handler $instance |
||
| 181 | */ |
||
| 182 | public static function get_instance() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Keep track of a connection error that was encountered |
||
| 191 | * |
||
| 192 | * @since 8.7.0 |
||
| 193 | * |
||
| 194 | * @param \WP_Error $error the error object. |
||
| 195 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Checks the status of the gate |
||
| 209 | * |
||
| 210 | * This protects the site (and WPCOM) against over loads. |
||
| 211 | * |
||
| 212 | * @since 8.7.0 |
||
| 213 | * |
||
| 214 | * @param \WP_Error $error the error object. |
||
| 215 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 216 | */ |
||
| 217 | public function should_report_error( \WP_Error $error ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 252 | * |
||
| 253 | * @since 8.7.0 |
||
| 254 | * |
||
| 255 | * @param \WP_Error $error the error object. |
||
| 256 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 257 | */ |
||
| 258 | public function store_error( \WP_Error $error ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Converts a WP_Error object in the array representation we store in the database |
||
| 288 | * |
||
| 289 | * @since 8.7.0 |
||
| 290 | * |
||
| 291 | * @param \WP_Error $error the error object. |
||
| 292 | * @return boolean|array False if error is invalid or the error array |
||
| 293 | */ |
||
| 294 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sends the error to WP.com to be verified |
||
| 325 | * |
||
| 326 | * @since 8.7.0 |
||
| 327 | * |
||
| 328 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function send_error_to_wpcom( $error_array ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Encrypt data to be sent over to WP.com |
||
| 354 | * |
||
| 355 | * @since 8.7.0 |
||
| 356 | * |
||
| 357 | * @param array|string $data the data to be encoded. |
||
| 358 | * @return boolean|string The encoded string on success, false on failure |
||
| 359 | */ |
||
| 360 | public function encrypt_data_to_wpcom( $data ) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Extracts the user ID from a token |
||
| 379 | * |
||
| 380 | * @since 8.7.0 |
||
| 381 | * |
||
| 382 | * @param string $token the token used to make the xml-rpc request. |
||
| 383 | * @return string $the user id or `invalid` if user id not present. |
||
| 384 | */ |
||
| 385 | public function get_user_id_from_token( $token ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Gets the reported errors stored in the database |
||
| 400 | * |
||
| 401 | * @since 8.7.0 |
||
| 402 | * |
||
| 403 | * @return array $errors |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function get_stored_errors() { |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the verified errors stored in the database |
||
| 420 | * |
||
| 421 | * @since 8.7.0 |
||
| 422 | * |
||
| 423 | * @return array $errors |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function get_verified_errors() { |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Removes expired errors from the array |
||
| 440 | * |
||
| 441 | * This method is called by get_stored_errors and get_verified errors and filters their result |
||
| 442 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
| 443 | * expired error will be permantently removed from the database |
||
| 444 | * |
||
| 445 | * @since 8.7.0 |
||
| 446 | * |
||
| 447 | * @param array $errors array of errors as stored in the database. |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | private function garbage_collector( $errors ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Delete all stored and verified errors from the database |
||
| 470 | * |
||
| 471 | * @since 8.7.0 |
||
| 472 | * |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | public function delete_all_errors() { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Delete all stored and verified errors from the database and returns unfiltered value |
||
| 482 | * |
||
| 483 | * This is used to hook into a couple of filters that expect true to not short circuit the disconnection flow |
||
| 484 | * |
||
| 485 | * @since 8.9.0 |
||
| 486 | * |
||
| 487 | * @param mixed $check The input sent by the filter. |
||
| 488 | * @return boolean |
||
| 489 | */ |
||
| 490 | public function delete_all_errors_and_return_unfiltered_value( $check ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Delete the reported errors stored in the database |
||
| 497 | * |
||
| 498 | * @since 8.7.0 |
||
| 499 | * |
||
| 500 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 501 | */ |
||
| 502 | public function delete_stored_errors() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Delete the verified errors stored in the database |
||
| 508 | * |
||
| 509 | * @since 8.7.0 |
||
| 510 | * |
||
| 511 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 512 | */ |
||
| 513 | public function delete_verified_errors() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Gets an error based on the nonce |
||
| 519 | * |
||
| 520 | * Receives a nonce and finds the related error. |
||
| 521 | * |
||
| 522 | * @since 8.7.0 |
||
| 523 | * |
||
| 524 | * @param string $nonce The nonce created for the error we want to get. |
||
| 525 | * @return null|array Returns the error array representation or null if error not found. |
||
| 526 | */ |
||
| 527 | public function get_error_by_nonce( $nonce ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Adds an error to the verified error list |
||
| 541 | * |
||
| 542 | * @since 8.7.0 |
||
| 543 | * |
||
| 544 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 545 | * @return void |
||
| 546 | */ |
||
| 547 | public function verify_error( $error ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Register REST API end point for error hanlding. |
||
| 565 | * |
||
| 566 | * @since 8.7.0 |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | public function register_verify_error_endpoint() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 590 | * |
||
| 591 | * @since 8.7.0 |
||
| 592 | * |
||
| 593 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 594 | * |
||
| 595 | * @return boolean |
||
| 596 | */ |
||
| 597 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Prints a generic error notice for all connection errors |
||
| 612 | * |
||
| 613 | * @since 8.9.0 |
||
| 614 | * |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | public function generic_admin_notice_error() { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Adds the error message to the Jetpack React Dashboard |
||
| 666 | * |
||
| 667 | * @since 8.9.0 |
||
| 668 | * |
||
| 669 | * @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | public function jetpack_react_dashboard_error( $errors ) { |
||
| 681 | |||
| 682 | } |
||
| 683 |
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.