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 | * Used internally when we want to look for the Normal Blog Token |
||
| 19 | * without knowing its token key ahead of time. |
||
| 20 | */ |
||
| 21 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 22 | const SECRETS_MISSING = 'secrets_missing'; |
||
| 23 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
| 24 | const SECRETS_OPTION_NAME = 'secrets'; |
||
| 25 | const MASTER_USER = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The object for managing options. |
||
| 29 | * |
||
| 30 | * @var \Automattic\Jetpack\Options\Manager |
||
| 31 | */ |
||
| 32 | protected $option_manager; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The procedure that should be run to generate secrets. |
||
| 36 | * |
||
| 37 | * @var Callable |
||
| 38 | */ |
||
| 39 | protected $secret_callable; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Initializes all needed hooks and request handlers. Handles API calls, upload |
||
| 43 | * requests, authentication requests. Also XMLRPC options requests. |
||
| 44 | * Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
||
| 45 | * this one. Among other things it should strip existing methods. |
||
| 46 | * |
||
| 47 | * @param Array $methods an array of API method names for the Connection to accept and |
||
| 48 | * pass on to existing callables. It's possible to specify whether |
||
| 49 | * each method should be available for unauthenticated calls or not. |
||
| 50 | * @see Jetpack::__construct |
||
| 51 | */ |
||
| 52 | public function initialize( $methods ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns true if the current site is connected to WordPress.com. |
||
| 58 | * |
||
| 59 | * @return Boolean is the site connected? |
||
| 60 | */ |
||
| 61 | public function is_active() { |
||
| 64 | |||
| 65 | public function get_magic_normal_token_key() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns true if the user with the specified identifier is connected to |
||
| 71 | * WordPress.com. |
||
| 72 | * |
||
| 73 | * @param Integer $user_id the user identifier. |
||
| 74 | * @return Boolean is the user connected? |
||
| 75 | */ |
||
| 76 | public function is_user_connected( $user_id ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the wpcom user data of the current|specified connected user. |
||
| 87 | * |
||
| 88 | * @param Integer $user_id the user identifier. |
||
| 89 | * @return Object the user object. |
||
| 90 | */ |
||
| 91 | public function get_connected_user_data( $user_id ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Is the user the connection owner. |
||
| 97 | * |
||
| 98 | * @param Integer $user_id the user identifier. |
||
| 99 | * @return Boolean is the user the connection owner? |
||
| 100 | */ |
||
| 101 | public function is_connection_owner( $user_id ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Unlinks the current user from the linked WordPress.com user |
||
| 107 | * |
||
| 108 | * @param Integer $user_id the user identifier. |
||
| 109 | */ |
||
| 110 | public static function disconnect_user( $user_id ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
| 116 | * Should be changed to be protected. |
||
| 117 | */ |
||
| 118 | public function initialize_server() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Checks if the current request is properly authenticated, bails if not. |
||
| 124 | * Should be changed to be protected. |
||
| 125 | */ |
||
| 126 | public function require_authentication() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Verifies the correctness of the request signature. |
||
| 132 | * Should be changed to be protected. |
||
| 133 | */ |
||
| 134 | public function verify_signature() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 140 | * remain public because the call to action comes from the current site, not from |
||
| 141 | * WordPress.com. |
||
| 142 | * |
||
| 143 | * @return Integer zero on success, or a bitmask on failure. |
||
| 144 | */ |
||
| 145 | public function register() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the callable that would be used to generate secrets. |
||
| 151 | * |
||
| 152 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 153 | */ |
||
| 154 | protected function get_secret_callable() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the object that is to be used for all option manipulation. |
||
| 169 | * |
||
| 170 | * @return Object $manager an option manager object. |
||
| 171 | */ |
||
| 172 | protected function get_option_manager() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Generates 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 | * @param Integer $exp Expiration time in seconds. |
||
| 191 | */ |
||
| 192 | public function generate_secrets( $action, $user_id, $exp ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 220 | * |
||
| 221 | * @param String $action The action name. |
||
| 222 | * @param Integer $user_id The user identifier. |
||
| 223 | * @return string|array an array of secrets or an error string. |
||
| 224 | */ |
||
| 225 | public function get_secrets( $action, $user_id ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Deletes secret tokens in case they, for example, have expired. |
||
| 243 | * |
||
| 244 | * @param String $action The action name. |
||
| 245 | * @param Integer $user_id The user identifier. |
||
| 246 | */ |
||
| 247 | 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 | public function handle_registration() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Responds to a WordPress.com call to authorize the current user. |
||
| 266 | * Should be changed to protected. |
||
| 267 | */ |
||
| 268 | public function handle_authorization() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Builds a URL to the Jetpack connection auth page. |
||
| 274 | * This needs rethinking. |
||
| 275 | * |
||
| 276 | * @param bool $raw If true, URL will not be escaped. |
||
| 277 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 278 | * If string, will be a custom redirect. |
||
| 279 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 280 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
| 281 | * |
||
| 282 | * @return string Connect URL |
||
| 283 | */ |
||
| 284 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Disconnects from the Jetpack servers. |
||
| 290 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 291 | */ |
||
| 292 | public function disconnect_site() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Gets the requested token. |
||
| 298 | * |
||
| 299 | * Tokens are one of two types: |
||
| 300 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 301 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 302 | * are not associated with a user account. They represent the site's connection with |
||
| 303 | * the Jetpack servers. |
||
| 304 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 305 | * |
||
| 306 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 307 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 308 | * over the network; it's used only for signing things. |
||
| 309 | * |
||
| 310 | * Blog Tokens can be "Normal" or "Special". |
||
| 311 | * * Normal: The result of a normal connection flow. They look like |
||
| 312 | * "{$random_string_1}.{$random_string_2}" |
||
| 313 | * That is, $token_key and $private are both random strings. |
||
| 314 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 315 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 316 | * constant (rare). |
||
| 317 | * * Special: A connection token for sites that have gone through an alternative |
||
| 318 | * connection flow. They look like: |
||
| 319 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 320 | * That is, $private is a random string and $token_key has a special structure with |
||
| 321 | * lots of semicolons. |
||
| 322 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 323 | * JETPACK_BLOG_TOKEN constant. |
||
| 324 | * |
||
| 325 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 326 | * Special Blog Tokens always do. |
||
| 327 | * |
||
| 328 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 329 | * order: |
||
| 330 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 331 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 332 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 333 | * |
||
| 334 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 335 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 336 | * false : Use first token. Default. |
||
| 337 | * Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY : Use first Normal Token. |
||
| 338 | * non-empty string : Use matching token |
||
| 339 | * @return object|false |
||
| 340 | */ |
||
| 341 | public function get_access_token( $user_id = false, $token_key = false ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 427 | * |
||
| 428 | * @param $domain |
||
| 429 | * @param array $extra |
||
| 430 | * |
||
| 431 | * @return bool|WP_Error |
||
| 432 | */ |
||
| 433 | public function is_usable_domain( $domain, $extra = array() ) { |
||
| 486 | } |
||
| 487 |
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: