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 |
||
| 24 | class Error_Handler { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The name of the option that stores the errors |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const STORED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_errors'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The name of the option that stores the errors |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const STORED_VERIFIED_ERRORS_OPTION = 'jetpack_connection_xmlrpc_verified_errors'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The prefix of the transient that controls the gate for each error code |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const ERROR_REPORTING_GATE = 'jetpack_connection_error_reporting_gate_'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Time in seconds a test should live in the database before being discarded |
||
| 49 | */ |
||
| 50 | const ERROR_LIFE_TIME = DAY_IN_SECONDS * 3; |
||
| 51 | /** |
||
| 52 | * List of known errors. Only error codes in this list will be handled |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | public $known_errors = array( |
||
| 57 | 'malformed_token', |
||
| 58 | 'malformed_user_id', |
||
| 59 | 'unknown_user', |
||
| 60 | 'no_user_tokens', |
||
| 61 | 'empty_master_user_option', |
||
| 62 | 'no_token_for_user', |
||
| 63 | 'token_malformed', |
||
| 64 | 'user_id_mismatch', |
||
| 65 | 'no_possible_tokens', |
||
| 66 | 'no_valid_token', |
||
| 67 | 'unknown_token', |
||
| 68 | 'could_not_sign', |
||
| 69 | 'invalid_scheme', |
||
| 70 | 'invalid_secret', |
||
| 71 | 'invalid_token', |
||
| 72 | 'token_mismatch', |
||
| 73 | 'invalid_body', |
||
| 74 | 'invalid_signature', |
||
| 75 | 'invalid_body_hash', |
||
| 76 | 'invalid_nonce', |
||
| 77 | 'signature_mismatch', |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Holds the instance of this singleton class |
||
| 82 | * |
||
| 83 | * @var Error_Handler $instance |
||
| 84 | */ |
||
| 85 | public static $instance = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Initialize hooks |
||
| 89 | */ |
||
| 90 | private function __construct() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Gets the list of verified errors and act upon them |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function handle_verified_errors() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Gets the instance of this singleton class |
||
| 127 | * |
||
| 128 | * @return Error_Handler $instance |
||
| 129 | */ |
||
| 130 | public static function get_instance() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Keep track of a connection error that was encoutered |
||
| 139 | * |
||
| 140 | * @param \WP_Error $error the error object. |
||
| 141 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Checks the status of the gate |
||
| 155 | * |
||
| 156 | * This protects the site (and WPCOM) against over loads. |
||
| 157 | * |
||
| 158 | * @param \WP_Error $error the error object. |
||
| 159 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 160 | */ |
||
| 161 | public function should_report_error( \WP_Error $error ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 184 | * |
||
| 185 | * @param \WP_Error $error the error object. |
||
| 186 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 187 | */ |
||
| 188 | public function store_error( \WP_Error $error ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Converts a WP_Error object in the array representation we store in the database |
||
| 218 | * |
||
| 219 | * @param \WP_Error $error the error object. |
||
| 220 | * @return boolean|array False if error is invalid or the error array |
||
| 221 | */ |
||
| 222 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Sends the error to WP.com to be verified |
||
| 254 | * |
||
| 255 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function send_error_to_wpcom( $error_array ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Encrypt data to be sent over to WP.com |
||
| 277 | * |
||
| 278 | * @param array|string $data the data to be encoded. |
||
| 279 | * @return boolean|string The encoded string on success, false on failure |
||
| 280 | */ |
||
| 281 | public function encrypt_data_to_wpcom( $data ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Extracts the user ID from a token |
||
| 300 | * |
||
| 301 | * @param string $token the token used to make the xml-rpc request. |
||
| 302 | * @return string $the user id or `invalid` if user id not present. |
||
| 303 | */ |
||
| 304 | public function get_user_id_from_token( $token ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the reported errors stored in the database |
||
| 319 | * |
||
| 320 | * @return array $errors |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function get_stored_errors() { |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Gets the verified errors stored in the database |
||
| 337 | * |
||
| 338 | * @return array $errors |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function get_verified_errors() { |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Removes expired errors from the array |
||
| 355 | * |
||
| 356 | * This method is calleb by get_stored_errors and get_verified errors and filters their result |
||
| 357 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
| 358 | * expired error will be permantently removed from the database |
||
| 359 | * |
||
| 360 | * @param array $errors array of errors as stored in the database. |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | private function garbage_collector( $errors ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Delete all stored and verified errors from the database |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function delete_all_errors() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Delete the reported errors stored in the database |
||
| 393 | * |
||
| 394 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 395 | */ |
||
| 396 | public function delete_stored_errors() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Delete the verified errors stored in the database |
||
| 402 | * |
||
| 403 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 404 | */ |
||
| 405 | public function delete_verified_errors() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Gets an error based on the nonce |
||
| 411 | * |
||
| 412 | * Receives a nonce and finds the related error. If error is found, move it to the verified errors option. |
||
| 413 | * |
||
| 414 | * @param string $nonce The nonce created for the error we want to get. |
||
| 415 | * @return null|array Returns the error array representation or null if error not found. |
||
| 416 | */ |
||
| 417 | public function get_error_by_nonce( $nonce ) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Adds an error to the verified error list |
||
| 431 | * |
||
| 432 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function verify_error( $error ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Register REST API end point for error hanlding. |
||
| 453 | * |
||
| 454 | * @since 8.7.0 |
||
| 455 | * |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | public function register_verify_error_endpoint() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 477 | * |
||
| 478 | * @since 8.7.0 |
||
| 479 | * |
||
| 480 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 481 | * |
||
| 482 | * @return boolean |
||
| 483 | */ |
||
| 484 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 496 | |||
| 497 | } |
||
| 498 |
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.