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() { |
||
133 | |||
134 | /** |
||
135 | * Gets the list of verified errors and act upon them |
||
136 | * |
||
137 | * @since 8.7.0 |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function handle_verified_errors() { |
||
161 | |||
162 | /** |
||
163 | * Gets the instance of this singleton class |
||
164 | * |
||
165 | * @since 8.7.0 |
||
166 | * |
||
167 | * @return Error_Handler $instance |
||
168 | */ |
||
169 | public static function get_instance() { |
||
175 | |||
176 | /** |
||
177 | * Keep track of a connection error that was encountered |
||
178 | * |
||
179 | * @since 8.7.0 |
||
180 | * |
||
181 | * @param \WP_Error $error the error object. |
||
182 | * @param boolean $force Force the report, even if should_report_error is false. |
||
183 | * @return void |
||
184 | */ |
||
185 | public function report_error( \WP_Error $error, $force = false ) { |
||
193 | |||
194 | /** |
||
195 | * Checks the status of the gate |
||
196 | * |
||
197 | * This protects the site (and WPCOM) against over loads. |
||
198 | * |
||
199 | * @since 8.7.0 |
||
200 | * |
||
201 | * @param \WP_Error $error the error object. |
||
202 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
203 | */ |
||
204 | public function should_report_error( \WP_Error $error ) { |
||
236 | |||
237 | /** |
||
238 | * Stores the error in the database so we know there is an issue and can inform the user |
||
239 | * |
||
240 | * @since 8.7.0 |
||
241 | * |
||
242 | * @param \WP_Error $error the error object. |
||
243 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
244 | */ |
||
245 | public function store_error( \WP_Error $error ) { |
||
272 | |||
273 | /** |
||
274 | * Converts a WP_Error object in the array representation we store in the database |
||
275 | * |
||
276 | * @since 8.7.0 |
||
277 | * |
||
278 | * @param \WP_Error $error the error object. |
||
279 | * @return boolean|array False if error is invalid or the error array |
||
280 | */ |
||
281 | public function wp_error_to_array( \WP_Error $error ) { |
||
309 | |||
310 | /** |
||
311 | * Sends the error to WP.com to be verified |
||
312 | * |
||
313 | * @since 8.7.0 |
||
314 | * |
||
315 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function send_error_to_wpcom( $error_array ) { |
||
338 | |||
339 | /** |
||
340 | * Encrypt data to be sent over to WP.com |
||
341 | * |
||
342 | * @since 8.7.0 |
||
343 | * |
||
344 | * @param array|string $data the data to be encoded. |
||
345 | * @return boolean|string The encoded string on success, false on failure |
||
346 | */ |
||
347 | public function encrypt_data_to_wpcom( $data ) { |
||
363 | |||
364 | /** |
||
365 | * Extracts the user ID from a token |
||
366 | * |
||
367 | * @since 8.7.0 |
||
368 | * |
||
369 | * @param string $token the token used to make the xml-rpc request. |
||
370 | * @return string $the user id or `invalid` if user id not present. |
||
371 | */ |
||
372 | public function get_user_id_from_token( $token ) { |
||
384 | |||
385 | /** |
||
386 | * Gets the reported errors stored in the database |
||
387 | * |
||
388 | * @since 8.7.0 |
||
389 | * |
||
390 | * @return array $errors |
||
391 | */ |
||
392 | View Code Duplication | public function get_stored_errors() { |
|
404 | |||
405 | /** |
||
406 | * Gets the verified errors stored in the database |
||
407 | * |
||
408 | * @since 8.7.0 |
||
409 | * |
||
410 | * @return array $errors |
||
411 | */ |
||
412 | View Code Duplication | public function get_verified_errors() { |
|
424 | |||
425 | /** |
||
426 | * Removes expired errors from the array |
||
427 | * |
||
428 | * This method is called by get_stored_errors and get_verified errors and filters their result |
||
429 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
430 | * expired error will be permantently removed from the database |
||
431 | * |
||
432 | * @since 8.7.0 |
||
433 | * |
||
434 | * @param array $errors array of errors as stored in the database. |
||
435 | * @return array |
||
436 | */ |
||
437 | private function garbage_collector( $errors ) { |
||
454 | |||
455 | /** |
||
456 | * Delete all stored and verified errors from the database |
||
457 | * |
||
458 | * @since 8.7.0 |
||
459 | * |
||
460 | * @return void |
||
461 | */ |
||
462 | public function delete_all_errors() { |
||
466 | |||
467 | /** |
||
468 | * Delete the reported errors stored in the database |
||
469 | * |
||
470 | * @since 8.7.0 |
||
471 | * |
||
472 | * @return boolean True, if option is successfully deleted. False on failure. |
||
473 | */ |
||
474 | public function delete_stored_errors() { |
||
477 | |||
478 | /** |
||
479 | * Delete the verified errors stored in the database |
||
480 | * |
||
481 | * @since 8.7.0 |
||
482 | * |
||
483 | * @return boolean True, if option is successfully deleted. False on failure. |
||
484 | */ |
||
485 | public function delete_verified_errors() { |
||
488 | |||
489 | /** |
||
490 | * Gets an error based on the nonce |
||
491 | * |
||
492 | * Receives a nonce and finds the related error. |
||
493 | * |
||
494 | * @since 8.7.0 |
||
495 | * |
||
496 | * @param string $nonce The nonce created for the error we want to get. |
||
497 | * @return null|array Returns the error array representation or null if error not found. |
||
498 | */ |
||
499 | public function get_error_by_nonce( $nonce ) { |
||
510 | |||
511 | /** |
||
512 | * Adds an error to the verified error list |
||
513 | * |
||
514 | * @since 8.7.0 |
||
515 | * |
||
516 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
517 | * @return void |
||
518 | */ |
||
519 | public function verify_error( $error ) { |
||
534 | |||
535 | /** |
||
536 | * Register REST API end point for error hanlding. |
||
537 | * |
||
538 | * @since 8.7.0 |
||
539 | * |
||
540 | * @return void |
||
541 | */ |
||
542 | public function register_verify_error_endpoint() { |
||
558 | |||
559 | /** |
||
560 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
561 | * |
||
562 | * @since 8.7.0 |
||
563 | * |
||
564 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
565 | * |
||
566 | * @return boolean |
||
567 | */ |
||
568 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
580 | |||
581 | } |
||
582 |
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.