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 |
||
18 | class Manager implements Manager_Interface { |
||
19 | |||
20 | const SECRETS_MISSING = 'secrets_missing'; |
||
21 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
22 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
23 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
24 | const JETPACK_MASTER_USER = true; |
||
25 | |||
26 | /** |
||
27 | * The procedure that should be run to generate secrets. |
||
28 | * |
||
29 | * @var Callable |
||
30 | */ |
||
31 | protected $secret_callable; |
||
32 | |||
33 | /** |
||
34 | * Initializes all needed hooks and request handlers. Handles API calls, upload |
||
35 | * requests, authentication requests. Also XMLRPC options requests. |
||
36 | * Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
||
37 | * this one. Among other things it should strip existing methods. |
||
38 | * |
||
39 | * @param Array $methods an array of API method names for the Connection to accept and |
||
40 | * pass on to existing callables. It's possible to specify whether |
||
41 | * each method should be available for unauthenticated calls or not. |
||
42 | * @see Jetpack::__construct |
||
43 | */ |
||
44 | public function initialize( $methods ) { |
||
47 | |||
48 | /** |
||
49 | * Returns true if the current site is connected to WordPress.com. |
||
50 | * |
||
51 | * @return Boolean is the site connected? |
||
52 | */ |
||
53 | public function is_active() { |
||
56 | |||
57 | /** |
||
58 | * Returns true if the user with the specified identifier is connected to |
||
59 | * WordPress.com. |
||
60 | * |
||
61 | * @param Integer $user_id the user identifier. |
||
62 | * @return Boolean is the user connected? |
||
63 | */ |
||
64 | public function is_user_connected( $user_id ) { |
||
67 | |||
68 | /** |
||
69 | * Get the wpcom user data of the current|specified connected user. |
||
70 | * |
||
71 | * @param Integer $user_id the user identifier. |
||
72 | * @return Object the user object. |
||
73 | */ |
||
74 | public function get_connected_user_data( $user_id ) { |
||
77 | |||
78 | /** |
||
79 | * Is the user the connection owner. |
||
80 | * |
||
81 | * @param Integer $user_id the user identifier. |
||
82 | * @return Boolean is the user the connection owner? |
||
83 | */ |
||
84 | public function is_connection_owner( $user_id ) { |
||
87 | |||
88 | /** |
||
89 | * Unlinks the current user from the linked WordPress.com user |
||
90 | * |
||
91 | * @param Integer $user_id the user identifier. |
||
92 | */ |
||
93 | public static function disconnect_user( $user_id ) { |
||
96 | |||
97 | /** |
||
98 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
99 | * Should be changed to be protected. |
||
100 | */ |
||
101 | public function initialize_server() { |
||
104 | |||
105 | /** |
||
106 | * Checks if the current request is properly authenticated, bails if not. |
||
107 | * Should be changed to be protected. |
||
108 | */ |
||
109 | public function require_authentication() { |
||
112 | |||
113 | /** |
||
114 | * Verifies the correctness of the request signature. |
||
115 | * Should be changed to be protected. |
||
116 | */ |
||
117 | public function verify_signature() { |
||
120 | |||
121 | /** |
||
122 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
123 | * remain public because the call to action comes from the current site, not from |
||
124 | * WordPress.com. |
||
125 | * |
||
126 | * @return Integer zero on success, or a bitmask on failure. |
||
127 | */ |
||
128 | public function register() { |
||
131 | |||
132 | /** |
||
133 | * Returns the callable that would be used to generate secrets. |
||
134 | * |
||
135 | * @return Callable a function that returns a secure string to be used as a secret. |
||
136 | */ |
||
137 | protected function get_secret_callable() { |
||
149 | |||
150 | /** |
||
151 | * Generates two secret tokens and the end of life timestamp for them. |
||
152 | * |
||
153 | * @param String $action The action name. |
||
154 | * @param Integer $user_id The user identifier. |
||
155 | * @param Integer $exp Expiration time in seconds. |
||
156 | */ |
||
157 | public function generate_secrets( $action, $user_id, $exp ) { |
||
185 | |||
186 | /** |
||
187 | * Returns two secret tokens and the end of life timestamp for them. |
||
188 | * |
||
189 | * @param String $action The action name. |
||
190 | * @param Integer $user_id The user identifier. |
||
191 | * @return string|array an array of secrets or an error string. |
||
192 | */ |
||
193 | public function get_secrets( $action, $user_id ) { |
||
211 | |||
212 | /** |
||
213 | * Deletes secret tokens in case they, for example, have expired. |
||
214 | * |
||
215 | * @param String $action The action name. |
||
216 | * @param Integer $user_id The user identifier. |
||
217 | */ |
||
218 | public function delete_secrets( $action, $user_id ) { |
||
229 | |||
230 | /** |
||
231 | * Responds to a WordPress.com call to register the current site. |
||
232 | * Should be changed to protected. |
||
233 | * |
||
234 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
235 | */ |
||
236 | public function handle_registration( array $registration_data ) { |
||
244 | |||
245 | /** |
||
246 | * Verify a Previously Generated Secret. |
||
247 | * |
||
248 | * @param string $action The type of secret to verify. |
||
249 | * @param string $secret_1 The secret string to compare to what is stored. |
||
250 | * @param int $user_id The user ID of the owner of the secret. |
||
251 | */ |
||
252 | protected function verify_secrets( $action, $secret_1, $user_id ) { |
||
350 | |||
351 | /** |
||
352 | * Responds to a WordPress.com call to authorize the current user. |
||
353 | * Should be changed to protected. |
||
354 | */ |
||
355 | public function handle_authorization() { |
||
358 | |||
359 | /** |
||
360 | * Builds a URL to the Jetpack connection auth page. |
||
361 | * This needs rethinking. |
||
362 | * |
||
363 | * @param bool $raw If true, URL will not be escaped. |
||
364 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
365 | * If string, will be a custom redirect. |
||
366 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
367 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
368 | * |
||
369 | * @return string Connect URL |
||
370 | */ |
||
371 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
374 | |||
375 | /** |
||
376 | * Disconnects from the Jetpack servers. |
||
377 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
378 | */ |
||
379 | public function disconnect_site() { |
||
382 | |||
383 | /** |
||
384 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
385 | * |
||
386 | * @param string $text The string to hash. |
||
387 | * @return string |
||
388 | */ |
||
389 | public function sha1_base64( $text ) { |
||
392 | |||
393 | /** |
||
394 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
395 | * |
||
396 | * @param string $domain The domain to check. |
||
397 | * |
||
398 | * @return bool|WP_Error |
||
399 | */ |
||
400 | public function is_usable_domain( $domain ) { |
||
487 | |||
488 | /** |
||
489 | * Gets the requested token. |
||
490 | * |
||
491 | * Tokens are one of two types: |
||
492 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
493 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
494 | * are not associated with a user account. They represent the site's connection with |
||
495 | * the Jetpack servers. |
||
496 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
497 | * |
||
498 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
499 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
500 | * over the network; it's used only for signing things. |
||
501 | * |
||
502 | * Blog Tokens can be "Normal" or "Special". |
||
503 | * * Normal: The result of a normal connection flow. They look like |
||
504 | * "{$random_string_1}.{$random_string_2}" |
||
505 | * That is, $token_key and $private are both random strings. |
||
506 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
507 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
508 | * constant (rare). |
||
509 | * * Special: A connection token for sites that have gone through an alternative |
||
510 | * connection flow. They look like: |
||
511 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
512 | * That is, $private is a random string and $token_key has a special structure with |
||
513 | * lots of semicolons. |
||
514 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
515 | * JETPACK_BLOG_TOKEN constant. |
||
516 | * |
||
517 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
518 | * Special Blog Tokens always do. |
||
519 | * |
||
520 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
521 | * order: |
||
522 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
523 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
524 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
525 | * |
||
526 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
527 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
528 | * |
||
529 | * @return object|false |
||
530 | */ |
||
531 | public function get_access_token( $user_id = false, $token_key = false ) { |
||
616 | } |
||
617 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.