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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Manager implements Manager_Interface { |
||
| 17 | |||
| 18 | const SECRETS_MISSING = 'secrets_missing'; |
||
| 19 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
| 20 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
| 21 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 22 | const JETPACK_MASTER_USER = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The procedure that should be run to generate secrets. |
||
| 26 | * |
||
| 27 | * @var Callable |
||
| 28 | */ |
||
| 29 | protected $secret_callable; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Initializes all needed hooks and request handlers. Handles API calls, upload |
||
| 33 | * requests, authentication requests. Also XMLRPC options requests. |
||
| 34 | * Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
||
| 35 | * this one. Among other things it should strip existing methods. |
||
| 36 | * |
||
| 37 | * @param Array $methods an array of API method names for the Connection to accept and |
||
| 38 | * pass on to existing callables. It's possible to specify whether |
||
| 39 | * each method should be available for unauthenticated calls or not. |
||
| 40 | * @see Jetpack::__construct |
||
| 41 | */ |
||
| 42 | public function initialize( $methods ) { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns true if the current site is connected to WordPress.com. |
||
| 48 | * |
||
| 49 | * @return Boolean is the site connected? |
||
| 50 | */ |
||
| 51 | public function is_active() { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Returns true if the user with the specified identifier is connected to |
||
| 57 | * WordPress.com. |
||
| 58 | * |
||
| 59 | * @param Integer|Boolean $user_id the user identifier. |
||
| 60 | * @return Boolean is the user connected? |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function is_user_connected( $user_id = false ) { |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Get the wpcom user data of the current|specified connected user. |
||
| 73 | * |
||
| 74 | * @param Integer $user_id the user identifier. |
||
| 75 | * @return Object the user object. |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Is the user the connection owner. |
||
| 106 | * |
||
| 107 | * @param Integer $user_id the user identifier. |
||
| 108 | * @return Boolean is the user the connection owner? |
||
| 109 | */ |
||
| 110 | public function is_connection_owner( $user_id ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Unlinks the current user from the linked WordPress.com user |
||
| 116 | * |
||
| 117 | * @param Integer $user_id the user identifier. |
||
| 118 | */ |
||
| 119 | public static function disconnect_user( $user_id ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
| 125 | * Should be changed to be protected. |
||
| 126 | */ |
||
| 127 | public function initialize_server() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Checks if the current request is properly authenticated, bails if not. |
||
| 133 | * Should be changed to be protected. |
||
| 134 | */ |
||
| 135 | public function require_authentication() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Verifies the correctness of the request signature. |
||
| 141 | * Should be changed to be protected. |
||
| 142 | */ |
||
| 143 | public function verify_signature() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 149 | * remain public because the call to action comes from the current site, not from |
||
| 150 | * WordPress.com. |
||
| 151 | * |
||
| 152 | * @return Integer zero on success, or a bitmask on failure. |
||
| 153 | */ |
||
| 154 | public function register() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns the callable that would be used to generate secrets. |
||
| 160 | * |
||
| 161 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 162 | */ |
||
| 163 | protected function get_secret_callable() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 178 | * |
||
| 179 | * @param String $action The action name. |
||
| 180 | * @param Integer $user_id The user identifier. |
||
| 181 | * @param Integer $exp Expiration time in seconds. |
||
| 182 | */ |
||
| 183 | public function generate_secrets( $action, $user_id, $exp ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 214 | * |
||
| 215 | * @param String $action The action name. |
||
| 216 | * @param Integer $user_id The user identifier. |
||
| 217 | * @return string|array an array of secrets or an error string. |
||
| 218 | */ |
||
| 219 | public function get_secrets( $action, $user_id ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Deletes secret tokens in case they, for example, have expired. |
||
| 240 | * |
||
| 241 | * @param String $action The action name. |
||
| 242 | * @param Integer $user_id The user identifier. |
||
| 243 | */ |
||
| 244 | public function delete_secrets( $action, $user_id ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Responds to a WordPress.com call to register the current site. |
||
| 258 | * Should be changed to protected. |
||
| 259 | * |
||
| 260 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
| 261 | */ |
||
| 262 | public function handle_registration( array $registration_data ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Verify a Previously Generated Secret. |
||
| 273 | * |
||
| 274 | * @param string $action The type of secret to verify. |
||
| 275 | * @param string $secret_1 The secret string to compare to what is stored. |
||
| 276 | * @param int $user_id The user ID of the owner of the secret. |
||
| 277 | */ |
||
| 278 | protected function verify_secrets( $action, $secret_1, $user_id ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Responds to a WordPress.com call to authorize the current user. |
||
| 397 | * Should be changed to protected. |
||
| 398 | */ |
||
| 399 | public function handle_authorization() { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Builds a URL to the Jetpack connection auth page. |
||
| 405 | * This needs rethinking. |
||
| 406 | * |
||
| 407 | * @param bool $raw If true, URL will not be escaped. |
||
| 408 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 409 | * If string, will be a custom redirect. |
||
| 410 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 411 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
| 412 | * |
||
| 413 | * @return string Connect URL |
||
| 414 | */ |
||
| 415 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Disconnects from the Jetpack servers. |
||
| 421 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 422 | */ |
||
| 423 | public function disconnect_site() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
| 429 | * |
||
| 430 | * @param string $text The string to hash. |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function sha1_base64( $text ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 439 | * |
||
| 440 | * @param string $domain The domain to check. |
||
| 441 | * |
||
| 442 | * @return bool|WP_Error |
||
| 443 | */ |
||
| 444 | public function is_usable_domain( $domain ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Gets the requested token. |
||
| 534 | * |
||
| 535 | * Tokens are one of two types: |
||
| 536 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 537 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 538 | * are not associated with a user account. They represent the site's connection with |
||
| 539 | * the Jetpack servers. |
||
| 540 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 541 | * |
||
| 542 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 543 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 544 | * over the network; it's used only for signing things. |
||
| 545 | * |
||
| 546 | * Blog Tokens can be "Normal" or "Special". |
||
| 547 | * * Normal: The result of a normal connection flow. They look like |
||
| 548 | * "{$random_string_1}.{$random_string_2}" |
||
| 549 | * That is, $token_key and $private are both random strings. |
||
| 550 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 551 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 552 | * constant (rare). |
||
| 553 | * * Special: A connection token for sites that have gone through an alternative |
||
| 554 | * connection flow. They look like: |
||
| 555 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 556 | * That is, $private is a random string and $token_key has a special structure with |
||
| 557 | * lots of semicolons. |
||
| 558 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 559 | * JETPACK_BLOG_TOKEN constant. |
||
| 560 | * |
||
| 561 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 562 | * Special Blog Tokens always do. |
||
| 563 | * |
||
| 564 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 565 | * order: |
||
| 566 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 567 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 568 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 569 | * |
||
| 570 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 571 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 572 | * @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
||
| 573 | * |
||
| 574 | * @return object|false |
||
| 575 | */ |
||
| 576 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
| 661 | } |
||
| 662 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: