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 |
||
| 17 | class Manager implements Manager_Interface { |
||
| 18 | |||
| 19 | const SECRETS_MISSING = 'secrets_missing'; |
||
| 20 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
| 21 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
| 22 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 23 | const JETPACK_MASTER_USER = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The procedure that should be run to generate secrets. |
||
| 27 | * |
||
| 28 | * @var Callable |
||
| 29 | */ |
||
| 30 | protected $secret_callable; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Initializes all needed hooks and request handlers. Handles API calls, upload |
||
| 34 | * requests, authentication requests. Also XMLRPC options requests. |
||
| 35 | * Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
||
| 36 | * this one. Among other things it should strip existing methods. |
||
| 37 | * |
||
| 38 | * @param Array $methods an array of API method names for the Connection to accept and |
||
| 39 | * pass on to existing callables. It's possible to specify whether |
||
| 40 | * each method should be available for unauthenticated calls or not. |
||
| 41 | * @see Jetpack::__construct |
||
| 42 | */ |
||
| 43 | public function initialize( $methods ) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns true if the current site is connected to WordPress.com. |
||
| 49 | * |
||
| 50 | * @return Boolean is the site connected? |
||
| 51 | */ |
||
| 52 | public function is_active() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns true if the user with the specified identifier is connected to |
||
| 58 | * WordPress.com. |
||
| 59 | * |
||
| 60 | * @param Integer $user_id the user identifier. |
||
| 61 | * @return Boolean is the user connected? |
||
| 62 | */ |
||
| 63 | public function is_user_connected( $user_id ) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the wpcom user data of the current|specified connected user. |
||
| 69 | * |
||
| 70 | * @param Integer $user_id the user identifier. |
||
| 71 | * @return Object the user object. |
||
| 72 | */ |
||
| 73 | public function get_connected_user_data( $user_id ) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Is the user the connection owner. |
||
| 79 | * |
||
| 80 | * @param Integer $user_id the user identifier. |
||
| 81 | * @return Boolean is the user the connection owner? |
||
| 82 | */ |
||
| 83 | public function is_connection_owner( $user_id ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Unlinks the current user from the linked WordPress.com user |
||
| 89 | * |
||
| 90 | * @param Integer $user_id the user identifier. |
||
| 91 | */ |
||
| 92 | public static function disconnect_user( $user_id ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
| 98 | * Should be changed to be protected. |
||
| 99 | */ |
||
| 100 | public function initialize_server() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Checks if the current request is properly authenticated, bails if not. |
||
| 106 | * Should be changed to be protected. |
||
| 107 | */ |
||
| 108 | public function require_authentication() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Verifies the correctness of the request signature. |
||
| 114 | * Should be changed to be protected. |
||
| 115 | */ |
||
| 116 | public function verify_signature() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 122 | * remain public because the call to action comes from the current site, not from |
||
| 123 | * WordPress.com. |
||
| 124 | * |
||
| 125 | * @return Integer zero on success, or a bitmask on failure. |
||
| 126 | */ |
||
| 127 | public function register() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the callable that would be used to generate secrets. |
||
| 133 | * |
||
| 134 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 135 | */ |
||
| 136 | protected function get_secret_callable() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 151 | * |
||
| 152 | * @param String $action The action name. |
||
| 153 | * @param Integer $user_id The user identifier. |
||
| 154 | * @param Integer $exp Expiration time in seconds. |
||
| 155 | */ |
||
| 156 | public function generate_secrets( $action, $user_id, $exp ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 187 | * |
||
| 188 | * @param String $action The action name. |
||
| 189 | * @param Integer $user_id The user identifier. |
||
| 190 | * @return string|array an array of secrets or an error string. |
||
| 191 | */ |
||
| 192 | public function get_secrets( $action, $user_id ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Deletes secret tokens in case they, for example, have expired. |
||
| 213 | * |
||
| 214 | * @param String $action The action name. |
||
| 215 | * @param Integer $user_id The user identifier. |
||
| 216 | */ |
||
| 217 | public function delete_secrets( $action, $user_id ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Responds to a WordPress.com call to register the current site. |
||
| 231 | * Should be changed to protected. |
||
| 232 | */ |
||
| 233 | public function handle_registration() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Responds to a WordPress.com call to authorize the current user. |
||
| 239 | * Should be changed to protected. |
||
| 240 | */ |
||
| 241 | public function handle_authorization() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Builds a URL to the Jetpack connection auth page. |
||
| 247 | * This needs rethinking. |
||
| 248 | * |
||
| 249 | * @param bool $raw If true, URL will not be escaped. |
||
| 250 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 251 | * If string, will be a custom redirect. |
||
| 252 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 253 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
| 254 | * |
||
| 255 | * @return string Connect URL |
||
| 256 | */ |
||
| 257 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Disconnects from the Jetpack servers. |
||
| 263 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 264 | */ |
||
| 265 | public function disconnect_site() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param $text |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | function sha1_base64( $text ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 279 | * |
||
| 280 | * @param string $domain The domain to check. |
||
| 281 | * |
||
| 282 | * @return bool|WP_Error |
||
| 283 | */ |
||
| 284 | public function is_usable_domain( $domain ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Gets the requested token. |
||
| 374 | * |
||
| 375 | * Tokens are one of two types: |
||
| 376 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 377 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 378 | * are not associated with a user account. They represent the site's connection with |
||
| 379 | * the Jetpack servers. |
||
| 380 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 381 | * |
||
| 382 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 383 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 384 | * over the network; it's used only for signing things. |
||
| 385 | * |
||
| 386 | * Blog Tokens can be "Normal" or "Special". |
||
| 387 | * * Normal: The result of a normal connection flow. They look like |
||
| 388 | * "{$random_string_1}.{$random_string_2}" |
||
| 389 | * That is, $token_key and $private are both random strings. |
||
| 390 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 391 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 392 | * constant (rare). |
||
| 393 | * * Special: A connection token for sites that have gone through an alternative |
||
| 394 | * connection flow. They look like: |
||
| 395 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 396 | * That is, $private is a random string and $token_key has a special structure with |
||
| 397 | * lots of semicolons. |
||
| 398 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 399 | * JETPACK_BLOG_TOKEN constant. |
||
| 400 | * |
||
| 401 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 402 | * Special Blog Tokens always do. |
||
| 403 | * |
||
| 404 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 405 | * order: |
||
| 406 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 407 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 408 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 409 | * |
||
| 410 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 411 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 412 | * |
||
| 413 | * @return object|false |
||
| 414 | */ |
||
| 415 | public function get_access_token( $user_id = false, $token_key = false ) { |
||
| 500 | } |
||
| 501 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: