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 | public function is_user_connected( $user_id = false ) { |
||
63 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
64 | if ( ! $user_id ) { |
||
65 | return false; |
||
66 | } |
||
67 | |||
68 | return (bool) $this->get_access_token( $user_id ); |
||
69 | } |
||
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 ) { |
|
104 | |||
105 | /** |
||
106 | * Is the user the connection owner. |
||
107 | * |
||
108 | * @param Integer $user_id the user identifier. |
||
109 | * @return Boolean is the user the connection owner? |
||
110 | */ |
||
111 | public function is_connection_owner( $user_id ) { |
||
114 | |||
115 | /** |
||
116 | * Unlinks the current user from the linked WordPress.com user |
||
117 | * |
||
118 | * @param Integer $user_id the user identifier. |
||
119 | */ |
||
120 | public static function disconnect_user( $user_id ) { |
||
123 | |||
124 | /** |
||
125 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
126 | * Should be changed to be protected. |
||
127 | */ |
||
128 | public function initialize_server() { |
||
131 | |||
132 | /** |
||
133 | * Checks if the current request is properly authenticated, bails if not. |
||
134 | * Should be changed to be protected. |
||
135 | */ |
||
136 | public function require_authentication() { |
||
139 | |||
140 | /** |
||
141 | * Verifies the correctness of the request signature. |
||
142 | * Should be changed to be protected. |
||
143 | */ |
||
144 | public function verify_signature() { |
||
147 | |||
148 | /** |
||
149 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
150 | * remain public because the call to action comes from the current site, not from |
||
151 | * WordPress.com. |
||
152 | * |
||
153 | * @return Integer zero on success, or a bitmask on failure. |
||
154 | */ |
||
155 | public function register() { |
||
158 | |||
159 | /** |
||
160 | * Returns the callable that would be used to generate secrets. |
||
161 | * |
||
162 | * @return Callable a function that returns a secure string to be used as a secret. |
||
163 | */ |
||
164 | protected function get_secret_callable() { |
||
176 | |||
177 | /** |
||
178 | * Generates two secret tokens and the end of life timestamp for them. |
||
179 | * |
||
180 | * @param String $action The action name. |
||
181 | * @param Integer $user_id The user identifier. |
||
182 | * @param Integer $exp Expiration time in seconds. |
||
183 | */ |
||
184 | public function generate_secrets( $action, $user_id, $exp ) { |
||
212 | |||
213 | /** |
||
214 | * Returns two secret tokens and the end of life timestamp for them. |
||
215 | * |
||
216 | * @param String $action The action name. |
||
217 | * @param Integer $user_id The user identifier. |
||
218 | * @return string|array an array of secrets or an error string. |
||
219 | */ |
||
220 | public function get_secrets( $action, $user_id ) { |
||
238 | |||
239 | /** |
||
240 | * Deletes secret tokens in case they, for example, have expired. |
||
241 | * |
||
242 | * @param String $action The action name. |
||
243 | * @param Integer $user_id The user identifier. |
||
244 | */ |
||
245 | public function delete_secrets( $action, $user_id ) { |
||
256 | |||
257 | /** |
||
258 | * Responds to a WordPress.com call to register the current site. |
||
259 | * Should be changed to protected. |
||
260 | * |
||
261 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
262 | */ |
||
263 | public function handle_registration( array $registration_data ) { |
||
271 | |||
272 | /** |
||
273 | * Verify a Previously Generated Secret. |
||
274 | * |
||
275 | * @param string $action The type of secret to verify. |
||
276 | * @param string $secret_1 The secret string to compare to what is stored. |
||
277 | * @param int $user_id The user ID of the owner of the secret. |
||
278 | */ |
||
279 | protected function verify_secrets( $action, $secret_1, $user_id ) { |
||
395 | |||
396 | /** |
||
397 | * Responds to a WordPress.com call to authorize the current user. |
||
398 | * Should be changed to protected. |
||
399 | */ |
||
400 | public function handle_authorization() { |
||
403 | |||
404 | /** |
||
405 | * Builds a URL to the Jetpack connection auth page. |
||
406 | * This needs rethinking. |
||
407 | * |
||
408 | * @param bool $raw If true, URL will not be escaped. |
||
409 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
410 | * If string, will be a custom redirect. |
||
411 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
412 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
413 | * |
||
414 | * @return string Connect URL |
||
415 | */ |
||
416 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
419 | |||
420 | /** |
||
421 | * Disconnects from the Jetpack servers. |
||
422 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
423 | */ |
||
424 | public function disconnect_site() { |
||
427 | |||
428 | /** |
||
429 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
430 | * |
||
431 | * @param string $text The string to hash. |
||
432 | * @return string |
||
433 | */ |
||
434 | public function sha1_base64( $text ) { |
||
437 | |||
438 | /** |
||
439 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
440 | * |
||
441 | * @param string $domain The domain to check. |
||
442 | * |
||
443 | * @return bool|WP_Error |
||
444 | */ |
||
445 | public function is_usable_domain( $domain ) { |
||
532 | |||
533 | /** |
||
534 | * Gets the requested token. |
||
535 | * |
||
536 | * Tokens are one of two types: |
||
537 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
538 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
539 | * are not associated with a user account. They represent the site's connection with |
||
540 | * the Jetpack servers. |
||
541 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
542 | * |
||
543 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
544 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
545 | * over the network; it's used only for signing things. |
||
546 | * |
||
547 | * Blog Tokens can be "Normal" or "Special". |
||
548 | * * Normal: The result of a normal connection flow. They look like |
||
549 | * "{$random_string_1}.{$random_string_2}" |
||
550 | * That is, $token_key and $private are both random strings. |
||
551 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
552 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
553 | * constant (rare). |
||
554 | * * Special: A connection token for sites that have gone through an alternative |
||
555 | * connection flow. They look like: |
||
556 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
557 | * That is, $private is a random string and $token_key has a special structure with |
||
558 | * lots of semicolons. |
||
559 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
560 | * JETPACK_BLOG_TOKEN constant. |
||
561 | * |
||
562 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
563 | * Special Blog Tokens always do. |
||
564 | * |
||
565 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
566 | * order: |
||
567 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
568 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
569 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
570 | * |
||
571 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
572 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
573 | * @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. |
||
574 | * |
||
575 | * @return object|false |
||
576 | */ |
||
577 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
662 | } |
||
663 |
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: