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 | * List of known errors. Only error codes in this list will be handled |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $known_errors = array( |
||
| 53 | 'malformed_token', |
||
| 54 | 'malformed_user_id', |
||
| 55 | 'unknown_user', |
||
| 56 | 'no_user_tokens', |
||
| 57 | 'empty_master_user_option', |
||
| 58 | 'no_token_for_user', |
||
| 59 | 'token_malformed', |
||
| 60 | 'user_id_mismatch', |
||
| 61 | 'no_possible_tokens', |
||
| 62 | 'no_valid_token', |
||
| 63 | 'unknown_token', |
||
| 64 | 'could_not_sign', |
||
| 65 | 'invalid_scheme', |
||
| 66 | 'invalid_secret', |
||
| 67 | 'invalid_token', |
||
| 68 | 'token_mismatch', |
||
| 69 | 'invalid_body', |
||
| 70 | 'invalid_signature', |
||
| 71 | 'invalid_body_hash', |
||
| 72 | 'invalid_nonce', |
||
| 73 | 'signature_mismatch', |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Holds the instance of this singleton class |
||
| 78 | * |
||
| 79 | * @var Error_Handler $instance |
||
| 80 | */ |
||
| 81 | public static $instance = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Initialize hooks |
||
| 85 | */ |
||
| 86 | private function __construct() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets the list of verified errors and act upon them |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function handle_verified_errors() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets the instance of this singleton class |
||
| 123 | * |
||
| 124 | * @return Error_Handler $instance |
||
| 125 | */ |
||
| 126 | public static function get_instance() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Keep track of a connection error that was encoutered |
||
| 135 | * |
||
| 136 | * @param \WP_Error $error the error object. |
||
| 137 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Checks the status of the gate |
||
| 151 | * |
||
| 152 | * This protects the site (and WPCOM) against over loads. |
||
| 153 | * |
||
| 154 | * @param \WP_Error $error the error object. |
||
| 155 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 156 | */ |
||
| 157 | public function should_report_error( \WP_Error $error ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 180 | * |
||
| 181 | * @param \WP_Error $error the error object. |
||
| 182 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 183 | */ |
||
| 184 | public function store_error( \WP_Error $error ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Converts a WP_Error object in the array representation we store in the database |
||
| 214 | * |
||
| 215 | * @param \WP_Error $error the error object. |
||
| 216 | * @return boolean|array False if error is invalid or the error array |
||
| 217 | */ |
||
| 218 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sends the error to WP.com to be verified |
||
| 250 | * |
||
| 251 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function send_error_to_wpcom( $error_array ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Encrypt data to be sent over to WP.com |
||
| 273 | * |
||
| 274 | * @param array|string $data the data to be encoded. |
||
| 275 | * @return boolean|string The encoded string on success, false on failure |
||
| 276 | */ |
||
| 277 | public function encrypt_data_to_wpcom( $data ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Extracts the user ID from a token |
||
| 296 | * |
||
| 297 | * @param string $token the token used to make the xml-rpc request. |
||
| 298 | * @return string $the user id or `invalid` if user id not present. |
||
| 299 | */ |
||
| 300 | public function get_user_id_from_token( $token ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Gets the reported errors stored in the database |
||
| 315 | * |
||
| 316 | * @return array $errors |
||
| 317 | */ |
||
| 318 | public function get_stored_errors() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Gets the verified errors stored in the database |
||
| 330 | * |
||
| 331 | * @return array $errors |
||
| 332 | */ |
||
| 333 | public function get_verified_errors() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Delete all stored and verified errors from the database |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function delete_all_errors() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Delete the reported errors stored in the database |
||
| 353 | * |
||
| 354 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 355 | */ |
||
| 356 | public function delete_stored_errors() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Delete the verified errors stored in the database |
||
| 362 | * |
||
| 363 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 364 | */ |
||
| 365 | public function delete_verified_errors() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets an error based on the nonce |
||
| 371 | * |
||
| 372 | * Receives a nonce and finds the related error. If error is found, move it to the verified errors option. |
||
| 373 | * |
||
| 374 | * @param string $nonce The nonce created for the error we want to get. |
||
| 375 | * @return null|array Returns the error array representation or null if error not found. |
||
| 376 | */ |
||
| 377 | public function get_error_by_nonce( $nonce ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Adds an error to the verified error list |
||
| 391 | * |
||
| 392 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | public function verify_error( $error ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Register REST API end point for error hanlding. |
||
| 413 | * |
||
| 414 | * @since 8.7.0 |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | public function register_verify_error_endpoint() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 437 | * |
||
| 438 | * @since 8.7.0 |
||
| 439 | * |
||
| 440 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 441 | * |
||
| 442 | * @return boolean |
||
| 443 | */ |
||
| 444 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 456 | |||
| 457 | } |
||
| 458 |
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.