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() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the instance of this singleton class |
||
| 94 | * |
||
| 95 | * @return Error_Handler $instance |
||
| 96 | */ |
||
| 97 | public static function get_instance() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Keep track of a connection error that was encoutered |
||
| 106 | * |
||
| 107 | * @param \WP_Error $error the error object. |
||
| 108 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Checks the status of the gate |
||
| 122 | * |
||
| 123 | * This protects the site (and WPCOM) against over loads. |
||
| 124 | * |
||
| 125 | * @param \WP_Error $error the error object. |
||
| 126 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 127 | */ |
||
| 128 | public function should_report_error( \WP_Error $error ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 151 | * |
||
| 152 | * @param \WP_Error $error the error object. |
||
| 153 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 154 | */ |
||
| 155 | public function store_error( \WP_Error $error ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Converts a WP_Error object in the array representation we store in the database |
||
| 185 | * |
||
| 186 | * @param \WP_Error $error the error object. |
||
| 187 | * @return boolean|array False if error is invalid or the error array |
||
| 188 | */ |
||
| 189 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Sends the error to WP.com to be verified |
||
| 221 | * |
||
| 222 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function send_error_to_wpcom( $error_array ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Encrypt data to be sent over to WP.com |
||
| 244 | * |
||
| 245 | * @param array|string $data the data to be encoded. |
||
| 246 | * @return boolean|string The encoded string on success, false on failure |
||
| 247 | */ |
||
| 248 | public function encrypt_data_to_wpcom( $data ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Extracts the user ID from a token |
||
| 267 | * |
||
| 268 | * @param string $token the token used to make the xml-rpc request. |
||
| 269 | * @return string $the user id or `invalid` if user id not present. |
||
| 270 | */ |
||
| 271 | public function get_user_id_from_token( $token ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the reported errors stored in the database |
||
| 286 | * |
||
| 287 | * @return array $errors |
||
| 288 | */ |
||
| 289 | public function get_stored_errors() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Gets the verified errors stored in the database |
||
| 301 | * |
||
| 302 | * @return array $errors |
||
| 303 | */ |
||
| 304 | public function get_verified_errors() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete the reported errors stored in the database |
||
| 314 | * |
||
| 315 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 316 | */ |
||
| 317 | public function delete_stored_errors() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Delete the verified errors stored in the database |
||
| 323 | * |
||
| 324 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 325 | */ |
||
| 326 | public function delete_verified_errors() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets an error based on the nonce |
||
| 332 | * |
||
| 333 | * Receives a nonce and finds the related error. If error is found, move it to the verified errors option. |
||
| 334 | * |
||
| 335 | * @param string $nonce The nonce created for the error we want to get. |
||
| 336 | * @return null|array Returns the error array representation or null if error not found. |
||
| 337 | */ |
||
| 338 | public function get_error_by_nonce( $nonce ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Adds an error to the verified error list |
||
| 352 | * |
||
| 353 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | public function verify_error( $error ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Register REST API end point for error hanlding. |
||
| 374 | * |
||
| 375 | * @since 8.7.0 |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function register_verify_error_endpoint() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 398 | * |
||
| 399 | * @since 8.7.0 |
||
| 400 | * |
||
| 401 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 402 | * |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 417 | |||
| 418 | } |
||
| 419 |
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.