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 | * Holds the instance of this singleton class |
||
| 49 | * |
||
| 50 | * @var Error_Handler $instance |
||
| 51 | */ |
||
| 52 | public static $instance = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initialize hooks |
||
| 56 | */ |
||
| 57 | private function __construct() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Gets the instance of this singleton class |
||
| 65 | * |
||
| 66 | * @return Error_Handler $instance |
||
| 67 | */ |
||
| 68 | public static function get_instance() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Keep track of a connection error that was encoutered |
||
| 77 | * |
||
| 78 | * @param \WP_Error $error the error object. |
||
| 79 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Checks the status of the gate |
||
| 93 | * |
||
| 94 | * This protects the site (and WPCOM) against over loads. |
||
| 95 | * |
||
| 96 | * @param \WP_Error $error the error object. |
||
| 97 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 98 | */ |
||
| 99 | public function should_report_error( \WP_Error $error ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 122 | * |
||
| 123 | * @param \WP_Error $error the error object. |
||
| 124 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 125 | */ |
||
| 126 | public function store_error( \WP_Error $error ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Converts a WP_Error object in the array representation we store in the database |
||
| 156 | * |
||
| 157 | * @param \WP_Error $error the error object. |
||
| 158 | * @return boolean|array False if error is invalid or the error array |
||
| 159 | */ |
||
| 160 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sends the error to WP.com to be verified |
||
| 192 | * |
||
| 193 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function send_error_to_wpcom( $error_array ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Extracts the user ID from a token |
||
| 225 | * |
||
| 226 | * @param string $token the token used to make the xml-rpc request. |
||
| 227 | * @return string $the user id or `invalid` if user id not present. |
||
| 228 | */ |
||
| 229 | public function get_user_id_from_token( $token ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets the reported errors stored in the database |
||
| 244 | * |
||
| 245 | * @return array $errors |
||
| 246 | */ |
||
| 247 | public function get_stored_errors() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Gets the verified errors stored in the database |
||
| 259 | * |
||
| 260 | * @return array $errors |
||
| 261 | */ |
||
| 262 | public function get_verified_errors() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Delete the reported errors stored in the database |
||
| 272 | * |
||
| 273 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 274 | */ |
||
| 275 | public function delete_stored_errors() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Delete the verified errors stored in the database |
||
| 281 | * |
||
| 282 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 283 | */ |
||
| 284 | public function delete_verified_errors() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Gets an error based on the nonce |
||
| 290 | * |
||
| 291 | * Receives a nonce and finds the related error. If error is found, move it to the verified errors option. |
||
| 292 | * |
||
| 293 | * @param string $nonce The nonce created for the error we want to get. |
||
| 294 | * @return null|array Returns the error array representation or null if error not found. |
||
| 295 | */ |
||
| 296 | public function get_error_by_nonce( $nonce ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Adds an error to the verified error list |
||
| 310 | * |
||
| 311 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | public function verify_error( $error ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Register REST API end point for error hanlding. |
||
| 332 | * |
||
| 333 | * @since 8.7.0 |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | public function register_verify_error_endpoint() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 356 | * |
||
| 357 | * @since 8.7.0 |
||
| 358 | * |
||
| 359 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 360 | * |
||
| 361 | * @return boolean |
||
| 362 | */ |
||
| 363 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 375 | |||
| 376 | } |
||
| 377 |
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.