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() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the instance of this singleton class |
||
| 165 | * |
||
| 166 | * @since 8.7.0 |
||
| 167 | * |
||
| 168 | * @return Error_Handler $instance |
||
| 169 | */ |
||
| 170 | public static function get_instance() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Keep track of a connection error that was encountered |
||
| 179 | * |
||
| 180 | * @since 8.7.0 |
||
| 181 | * |
||
| 182 | * @param \WP_Error $error the error object. |
||
| 183 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Checks the status of the gate |
||
| 197 | * |
||
| 198 | * This protects the site (and WPCOM) against over loads. |
||
| 199 | * |
||
| 200 | * @since 8.7.0 |
||
| 201 | * |
||
| 202 | * @param \WP_Error $error the error object. |
||
| 203 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 204 | */ |
||
| 205 | public function should_report_error( \WP_Error $error ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 240 | * |
||
| 241 | * @since 8.7.0 |
||
| 242 | * |
||
| 243 | * @param \WP_Error $error the error object. |
||
| 244 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 245 | */ |
||
| 246 | public function store_error( \WP_Error $error ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Converts a WP_Error object in the array representation we store in the database |
||
| 276 | * |
||
| 277 | * @since 8.7.0 |
||
| 278 | * |
||
| 279 | * @param \WP_Error $error the error object. |
||
| 280 | * @return boolean|array False if error is invalid or the error array |
||
| 281 | */ |
||
| 282 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * This is been used to track blogs with deleted master user but whose tokens are still actively being used |
||
| 317 | * |
||
| 318 | * See p9dueE-1GB-p2 |
||
| 319 | * |
||
| 320 | * This tracking should be removed as long as we no longer need, possibly in 8.9 |
||
| 321 | * |
||
| 322 | * @since 8.8.1 |
||
| 323 | * |
||
| 324 | * @param string $error_code The error code. |
||
| 325 | * @param string $token The token that triggered the error. |
||
| 326 | * @param integer $user_id The user ID used to make the request that triggered the error. |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | private function track_lost_active_master_user( $error_code, $token, $user_id ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Sends the error to WP.com to be verified |
||
| 351 | * |
||
| 352 | * @since 8.7.0 |
||
| 353 | * |
||
| 354 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | public function send_error_to_wpcom( $error_array ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Encrypt data to be sent over to WP.com |
||
| 380 | * |
||
| 381 | * @since 8.7.0 |
||
| 382 | * |
||
| 383 | * @param array|string $data the data to be encoded. |
||
| 384 | * @return boolean|string The encoded string on success, false on failure |
||
| 385 | */ |
||
| 386 | public function encrypt_data_to_wpcom( $data ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Extracts the user ID from a token |
||
| 405 | * |
||
| 406 | * @since 8.7.0 |
||
| 407 | * |
||
| 408 | * @param string $token the token used to make the xml-rpc request. |
||
| 409 | * @return string $the user id or `invalid` if user id not present. |
||
| 410 | */ |
||
| 411 | public function get_user_id_from_token( $token ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets the reported errors stored in the database |
||
| 426 | * |
||
| 427 | * @since 8.7.0 |
||
| 428 | * |
||
| 429 | * @return array $errors |
||
| 430 | */ |
||
| 431 | View Code Duplication | public function get_stored_errors() { |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Gets the verified errors stored in the database |
||
| 446 | * |
||
| 447 | * @since 8.7.0 |
||
| 448 | * |
||
| 449 | * @return array $errors |
||
| 450 | */ |
||
| 451 | View Code Duplication | public function get_verified_errors() { |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Removes expired errors from the array |
||
| 466 | * |
||
| 467 | * This method is called by get_stored_errors and get_verified errors and filters their result |
||
| 468 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
| 469 | * expired error will be permantently removed from the database |
||
| 470 | * |
||
| 471 | * @since 8.7.0 |
||
| 472 | * |
||
| 473 | * @param array $errors array of errors as stored in the database. |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | private function garbage_collector( $errors ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Delete all stored and verified errors from the database |
||
| 496 | * |
||
| 497 | * @since 8.7.0 |
||
| 498 | * |
||
| 499 | * @return void |
||
| 500 | */ |
||
| 501 | public function delete_all_errors() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Delete the reported 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_stored_errors() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Delete the verified errors stored in the database |
||
| 519 | * |
||
| 520 | * @since 8.7.0 |
||
| 521 | * |
||
| 522 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 523 | */ |
||
| 524 | public function delete_verified_errors() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Gets an error based on the nonce |
||
| 530 | * |
||
| 531 | * Receives a nonce and finds the related error. |
||
| 532 | * |
||
| 533 | * @since 8.7.0 |
||
| 534 | * |
||
| 535 | * @param string $nonce The nonce created for the error we want to get. |
||
| 536 | * @return null|array Returns the error array representation or null if error not found. |
||
| 537 | */ |
||
| 538 | public function get_error_by_nonce( $nonce ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Adds an error to the verified error list |
||
| 552 | * |
||
| 553 | * @since 8.7.0 |
||
| 554 | * |
||
| 555 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | public function verify_error( $error ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Register REST API end point for error hanlding. |
||
| 576 | * |
||
| 577 | * @since 8.7.0 |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function register_verify_error_endpoint() { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 601 | * |
||
| 602 | * @since 8.7.0 |
||
| 603 | * |
||
| 604 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 605 | * |
||
| 606 | * @return boolean |
||
| 607 | */ |
||
| 608 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 620 | |||
| 621 | } |
||
| 622 |
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.