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 | /** |
||
| 80 | * The error code for event tracking purposes. |
||
| 81 | * If there are many, only the first error code will be tracked. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $error_code; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * List of known errors. Only error codes in this list will be handled |
||
| 89 | * |
||
| 90 | * @since 8.7.0 |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public $known_errors = array( |
||
| 95 | 'malformed_token', |
||
| 96 | 'malformed_user_id', |
||
| 97 | 'unknown_user', |
||
| 98 | 'no_user_tokens', |
||
| 99 | 'empty_master_user_option', |
||
| 100 | 'no_token_for_user', |
||
| 101 | 'token_malformed', |
||
| 102 | 'user_id_mismatch', |
||
| 103 | 'no_possible_tokens', |
||
| 104 | 'no_valid_user_token', |
||
| 105 | 'no_valid_blog_token', |
||
| 106 | 'unknown_token', |
||
| 107 | 'could_not_sign', |
||
| 108 | 'invalid_scheme', |
||
| 109 | 'invalid_secret', |
||
| 110 | 'invalid_token', |
||
| 111 | 'token_mismatch', |
||
| 112 | 'invalid_body', |
||
| 113 | 'invalid_signature', |
||
| 114 | 'invalid_body_hash', |
||
| 115 | 'invalid_nonce', |
||
| 116 | 'signature_mismatch', |
||
| 117 | ); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Holds the instance of this singleton class |
||
| 121 | * |
||
| 122 | * @since 8.7.0 |
||
| 123 | * |
||
| 124 | * @var Error_Handler $instance |
||
| 125 | */ |
||
| 126 | public static $instance = null; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Initialize instance, hookds and load verified errors handlers |
||
| 130 | * |
||
| 131 | * @since 8.7.0 |
||
| 132 | */ |
||
| 133 | private function __construct() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Gets the list of verified errors and act upon them |
||
| 150 | * |
||
| 151 | * @since 8.7.0 |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function handle_verified_errors() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the instance of this singleton class |
||
| 184 | * |
||
| 185 | * @since 8.7.0 |
||
| 186 | * |
||
| 187 | * @return Error_Handler $instance |
||
| 188 | */ |
||
| 189 | public static function get_instance() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Keep track of a connection error that was encountered |
||
| 198 | * |
||
| 199 | * @since 8.7.0 |
||
| 200 | * |
||
| 201 | * @param \WP_Error $error the error object. |
||
| 202 | * @param boolean $force Force the report, even if should_report_error is false. |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function report_error( \WP_Error $error, $force = false ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Checks the status of the gate |
||
| 216 | * |
||
| 217 | * This protects the site (and WPCOM) against over loads. |
||
| 218 | * |
||
| 219 | * @since 8.7.0 |
||
| 220 | * |
||
| 221 | * @param \WP_Error $error the error object. |
||
| 222 | * @return boolean $should_report True if gate is open and the error should be reported. |
||
| 223 | */ |
||
| 224 | public function should_report_error( \WP_Error $error ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Stores the error in the database so we know there is an issue and can inform the user |
||
| 259 | * |
||
| 260 | * @since 8.7.0 |
||
| 261 | * |
||
| 262 | * @param \WP_Error $error the error object. |
||
| 263 | * @return boolean|array False if stored errors were not updated and the error array if it was successfully stored. |
||
| 264 | */ |
||
| 265 | public function store_error( \WP_Error $error ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Converts a WP_Error object in the array representation we store in the database |
||
| 295 | * |
||
| 296 | * @since 8.7.0 |
||
| 297 | * |
||
| 298 | * @param \WP_Error $error the error object. |
||
| 299 | * @return boolean|array False if error is invalid or the error array |
||
| 300 | */ |
||
| 301 | public function wp_error_to_array( \WP_Error $error ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sends the error to WP.com to be verified |
||
| 332 | * |
||
| 333 | * @since 8.7.0 |
||
| 334 | * |
||
| 335 | * @param array $error_array The array representation of the error as it is stored in the database. |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function send_error_to_wpcom( $error_array ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Encrypt data to be sent over to WP.com |
||
| 361 | * |
||
| 362 | * @since 8.7.0 |
||
| 363 | * |
||
| 364 | * @param array|string $data the data to be encoded. |
||
| 365 | * @return boolean|string The encoded string on success, false on failure |
||
| 366 | */ |
||
| 367 | public function encrypt_data_to_wpcom( $data ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Extracts the user ID from a token |
||
| 386 | * |
||
| 387 | * @since 8.7.0 |
||
| 388 | * |
||
| 389 | * @param string $token the token used to make the xml-rpc request. |
||
| 390 | * @return string $the user id or `invalid` if user id not present. |
||
| 391 | */ |
||
| 392 | public function get_user_id_from_token( $token ) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Gets the reported errors stored in the database |
||
| 407 | * |
||
| 408 | * @since 8.7.0 |
||
| 409 | * |
||
| 410 | * @return array $errors |
||
| 411 | */ |
||
| 412 | View Code Duplication | public function get_stored_errors() { |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the verified errors stored in the database |
||
| 427 | * |
||
| 428 | * @since 8.7.0 |
||
| 429 | * |
||
| 430 | * @return array $errors |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function get_verified_errors() { |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Removes expired errors from the array |
||
| 447 | * |
||
| 448 | * This method is called by get_stored_errors and get_verified errors and filters their result |
||
| 449 | * Whenever a new error is stored to the database or verified, this will be triggered and the |
||
| 450 | * expired error will be permantently removed from the database |
||
| 451 | * |
||
| 452 | * @since 8.7.0 |
||
| 453 | * |
||
| 454 | * @param array $errors array of errors as stored in the database. |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | private function garbage_collector( $errors ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Delete all stored and verified errors from the database |
||
| 477 | * |
||
| 478 | * @since 8.7.0 |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | public function delete_all_errors() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Delete all stored and verified errors from the database and returns unfiltered value |
||
| 489 | * |
||
| 490 | * This is used to hook into a couple of filters that expect true to not short circuit the disconnection flow |
||
| 491 | * |
||
| 492 | * @since 8.9.0 |
||
| 493 | * |
||
| 494 | * @param mixed $check The input sent by the filter. |
||
| 495 | * @return boolean |
||
| 496 | */ |
||
| 497 | public function delete_all_errors_and_return_unfiltered_value( $check ) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Delete the reported errors stored in the database |
||
| 504 | * |
||
| 505 | * @since 8.7.0 |
||
| 506 | * |
||
| 507 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 508 | */ |
||
| 509 | public function delete_stored_errors() { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Delete the verified errors stored in the database |
||
| 515 | * |
||
| 516 | * @since 8.7.0 |
||
| 517 | * |
||
| 518 | * @return boolean True, if option is successfully deleted. False on failure. |
||
| 519 | */ |
||
| 520 | public function delete_verified_errors() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Gets an error based on the nonce |
||
| 526 | * |
||
| 527 | * Receives a nonce and finds the related error. |
||
| 528 | * |
||
| 529 | * @since 8.7.0 |
||
| 530 | * |
||
| 531 | * @param string $nonce The nonce created for the error we want to get. |
||
| 532 | * @return null|array Returns the error array representation or null if error not found. |
||
| 533 | */ |
||
| 534 | public function get_error_by_nonce( $nonce ) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Adds an error to the verified error list |
||
| 548 | * |
||
| 549 | * @since 8.7.0 |
||
| 550 | * |
||
| 551 | * @param array $error The error array, as it was saved in the unverified errors list. |
||
| 552 | * @return void |
||
| 553 | */ |
||
| 554 | public function verify_error( $error ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Register REST API end point for error hanlding. |
||
| 572 | * |
||
| 573 | * @since 8.7.0 |
||
| 574 | * |
||
| 575 | * @return void |
||
| 576 | */ |
||
| 577 | public function register_verify_error_endpoint() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Handles verification that a xml rpc error is legit and came from WordPres.com |
||
| 597 | * |
||
| 598 | * @since 8.7.0 |
||
| 599 | * |
||
| 600 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
| 601 | * |
||
| 602 | * @return boolean |
||
| 603 | */ |
||
| 604 | public function verify_xml_rpc_error( \WP_REST_Request $request ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Prints a generic error notice for all connection errors |
||
| 619 | * |
||
| 620 | * @since 8.9.0 |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | public function generic_admin_notice_error() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Adds the error message to the Jetpack React Dashboard |
||
| 673 | * |
||
| 674 | * @since 8.9.0 |
||
| 675 | * |
||
| 676 | * @param array $errors The array of errors. See Automattic\Jetpack\Connection\Error_Handler for details on the array structure. |
||
| 677 | * @return array |
||
| 678 | */ |
||
| 679 | public function jetpack_react_dashboard_error( $errors ) { |
||
| 688 | |||
| 689 | } |
||
| 690 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.