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 $user_id the user identifier. |
||
60 | * @return Boolean is the user connected? |
||
61 | */ |
||
62 | public function is_user_connected( $user_id ) { |
||
65 | |||
66 | /** |
||
67 | * Get the wpcom user data of the current|specified connected user. |
||
68 | * |
||
69 | * @param Integer $user_id the user identifier. |
||
70 | * @return Object the user object. |
||
71 | */ |
||
72 | public function get_connected_user_data( $user_id ) { |
||
75 | |||
76 | /** |
||
77 | * Is the user the connection owner. |
||
78 | * |
||
79 | * @param Integer $user_id the user identifier. |
||
80 | * @return Boolean is the user the connection owner? |
||
81 | */ |
||
82 | public function is_connection_owner( $user_id ) { |
||
85 | |||
86 | /** |
||
87 | * Unlinks the current user from the linked WordPress.com user |
||
88 | * |
||
89 | * @param Integer $user_id the user identifier. |
||
90 | */ |
||
91 | public static function disconnect_user( $user_id ) { |
||
94 | |||
95 | /** |
||
96 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
97 | * Should be changed to be protected. |
||
98 | */ |
||
99 | public function initialize_server() { |
||
102 | |||
103 | /** |
||
104 | * Checks if the current request is properly authenticated, bails if not. |
||
105 | * Should be changed to be protected. |
||
106 | */ |
||
107 | public function require_authentication() { |
||
110 | |||
111 | /** |
||
112 | * Verifies the correctness of the request signature. |
||
113 | * Should be changed to be protected. |
||
114 | */ |
||
115 | public function verify_signature() { |
||
118 | |||
119 | /** |
||
120 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
121 | * remain public because the call to action comes from the current site, not from |
||
122 | * WordPress.com. |
||
123 | * |
||
124 | * @return Integer zero on success, or a bitmask on failure. |
||
125 | */ |
||
126 | public function register() { |
||
129 | |||
130 | /** |
||
131 | * Returns the callable that would be used to generate secrets. |
||
132 | * |
||
133 | * @return Callable a function that returns a secure string to be used as a secret. |
||
134 | */ |
||
135 | protected function get_secret_callable() { |
||
147 | |||
148 | /** |
||
149 | * Generates two secret tokens and the end of life timestamp for them. |
||
150 | * |
||
151 | * @param String $action The action name. |
||
152 | * @param Integer $user_id The user identifier. |
||
153 | * @param Integer $exp Expiration time in seconds. |
||
154 | */ |
||
155 | public function generate_secrets( $action, $user_id, $exp ) { |
||
183 | |||
184 | /** |
||
185 | * Returns two secret tokens and the end of life timestamp for them. |
||
186 | * |
||
187 | * @param String $action The action name. |
||
188 | * @param Integer $user_id The user identifier. |
||
189 | * @return string|array an array of secrets or an error string. |
||
190 | */ |
||
191 | public function get_secrets( $action, $user_id ) { |
||
209 | |||
210 | /** |
||
211 | * Deletes secret tokens in case they, for example, have expired. |
||
212 | * |
||
213 | * @param String $action The action name. |
||
214 | * @param Integer $user_id The user identifier. |
||
215 | */ |
||
216 | public function delete_secrets( $action, $user_id ) { |
||
227 | |||
228 | /** |
||
229 | * Responds to a WordPress.com call to register the current site. |
||
230 | * Should be changed to protected. |
||
231 | * |
||
232 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
233 | */ |
||
234 | public function handle_registration( array $registration_data ) { |
||
242 | |||
243 | /** |
||
244 | * Verify a Previously Generated Secret. |
||
245 | * |
||
246 | * @param string $action The type of secret to verify. |
||
247 | * @param string $secret_1 The secret string to compare to what is stored. |
||
248 | * @param int $user_id The user ID of the owner of the secret. |
||
249 | */ |
||
250 | protected function verify_secrets( $action, $secret_1, $user_id ) { |
||
366 | |||
367 | /** |
||
368 | * Responds to a WordPress.com call to authorize the current user. |
||
369 | * Should be changed to protected. |
||
370 | */ |
||
371 | public function handle_authorization() { |
||
374 | |||
375 | /** |
||
376 | * Builds a URL to the Jetpack connection auth page. |
||
377 | * This needs rethinking. |
||
378 | * |
||
379 | * @param bool $raw If true, URL will not be escaped. |
||
380 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
381 | * If string, will be a custom redirect. |
||
382 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
383 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
384 | * |
||
385 | * @return string Connect URL |
||
386 | */ |
||
387 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
390 | |||
391 | /** |
||
392 | * Disconnects from the Jetpack servers. |
||
393 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
394 | */ |
||
395 | public function disconnect_site() { |
||
398 | |||
399 | /** |
||
400 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
401 | * |
||
402 | * @param string $text The string to hash. |
||
403 | * @return string |
||
404 | */ |
||
405 | public function sha1_base64( $text ) { |
||
408 | |||
409 | /** |
||
410 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
411 | * |
||
412 | * @param string $domain The domain to check. |
||
413 | * |
||
414 | * @return bool|WP_Error |
||
415 | */ |
||
416 | public function is_usable_domain( $domain ) { |
||
503 | |||
504 | /** |
||
505 | * Gets the requested token. |
||
506 | * |
||
507 | * Tokens are one of two types: |
||
508 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
509 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
510 | * are not associated with a user account. They represent the site's connection with |
||
511 | * the Jetpack servers. |
||
512 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
513 | * |
||
514 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
515 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
516 | * over the network; it's used only for signing things. |
||
517 | * |
||
518 | * Blog Tokens can be "Normal" or "Special". |
||
519 | * * Normal: The result of a normal connection flow. They look like |
||
520 | * "{$random_string_1}.{$random_string_2}" |
||
521 | * That is, $token_key and $private are both random strings. |
||
522 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
523 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
524 | * constant (rare). |
||
525 | * * Special: A connection token for sites that have gone through an alternative |
||
526 | * connection flow. They look like: |
||
527 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
528 | * That is, $private is a random string and $token_key has a special structure with |
||
529 | * lots of semicolons. |
||
530 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
531 | * JETPACK_BLOG_TOKEN constant. |
||
532 | * |
||
533 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
534 | * Special Blog Tokens always do. |
||
535 | * |
||
536 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
537 | * order: |
||
538 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
539 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
540 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
541 | * |
||
542 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
543 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
544 | * |
||
545 | * @return object|false |
||
546 | */ |
||
547 | public function get_access_token( $user_id = false, $token_key = false ) { |
||
632 | } |
||
633 |
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: