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:
| 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 | |||
| 22 | /** |
||
| 23 | * The procedure that should be run to generate secrets. |
||
| 24 | * |
||
| 25 | * @var Callable |
||
| 26 | */ |
||
| 27 | protected $secret_callable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Initializes all needed hooks and request handlers. Handles API calls, upload |
||
| 31 | * requests, authentication requests. Also XMLRPC options requests. |
||
| 32 | * Fallback XMLRPC is also a bridge, but probably can be a class that inherits |
||
| 33 | * this one. Among other things it should strip existing methods. |
||
| 34 | * |
||
| 35 | * @param Array $methods an array of API method names for the Connection to accept and |
||
| 36 | * pass on to existing callables. It's possible to specify whether |
||
| 37 | * each method should be available for unauthenticated calls or not. |
||
| 38 | * @see Jetpack::__construct |
||
| 39 | */ |
||
| 40 | public function initialize( $methods ) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns true if the current site is connected to WordPress.com. |
||
| 46 | * |
||
| 47 | * @return Boolean is the site connected? |
||
| 48 | */ |
||
| 49 | public function is_active() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns true if the user with the specified identifier is connected to |
||
| 55 | * WordPress.com. |
||
| 56 | * |
||
| 57 | * @param Integer $user_id the user identifier. |
||
| 58 | * @return Boolean is the user connected? |
||
| 59 | */ |
||
| 60 | public function is_user_connected( $user_id ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the wpcom user data of the current|specified connected user. |
||
| 66 | * |
||
| 67 | * @param Integer $user_id the user identifier. |
||
| 68 | * @return Object the user object. |
||
| 69 | */ |
||
| 70 | public function get_connected_user_data( $user_id ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Is the user the connection owner. |
||
| 76 | * |
||
| 77 | * @param Integer $user_id the user identifier. |
||
| 78 | * @return Boolean is the user the connection owner? |
||
| 79 | */ |
||
| 80 | public function is_connection_owner( $user_id ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Unlinks the current user from the linked WordPress.com user |
||
| 86 | * |
||
| 87 | * @param Integer $user_id the user identifier. |
||
| 88 | */ |
||
| 89 | public static function disconnect_user( $user_id ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Initializes a transport server, whatever it may be, saves into the object property. |
||
| 95 | * Should be changed to be protected. |
||
| 96 | */ |
||
| 97 | public function initialize_server() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Checks if the current request is properly authenticated, bails if not. |
||
| 103 | * Should be changed to be protected. |
||
| 104 | */ |
||
| 105 | public function require_authentication() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Verifies the correctness of the request signature. |
||
| 111 | * Should be changed to be protected. |
||
| 112 | */ |
||
| 113 | public function verify_signature() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 119 | * remain public because the call to action comes from the current site, not from |
||
| 120 | * WordPress.com. |
||
| 121 | * |
||
| 122 | * @return Integer zero on success, or a bitmask on failure. |
||
| 123 | */ |
||
| 124 | public function register() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns the callable that would be used to generate secrets. |
||
| 130 | * |
||
| 131 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 132 | */ |
||
| 133 | protected function get_secret_callable() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 148 | * |
||
| 149 | * @param String $action The action name. |
||
| 150 | * @param Integer $user_id The user identifier. |
||
| 151 | * @param Integer $exp Expiration time in seconds. |
||
| 152 | */ |
||
| 153 | public function generate_secrets( $action, $user_id, $exp ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 184 | * |
||
| 185 | * @param String $action The action name. |
||
| 186 | * @param Integer $user_id The user identifier. |
||
| 187 | * @return string|array an array of secrets or an error string. |
||
| 188 | */ |
||
| 189 | public function get_secrets( $action, $user_id ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Deletes secret tokens in case they, for example, have expired. |
||
| 210 | * |
||
| 211 | * @param String $action The action name. |
||
| 212 | * @param Integer $user_id The user identifier. |
||
| 213 | */ |
||
| 214 | public function delete_secrets( $action, $user_id ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Responds to a WordPress.com call to register the current site. |
||
| 228 | * Should be changed to protected. |
||
| 229 | */ |
||
| 230 | public function handle_registration() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Responds to a WordPress.com call to authorize the current user. |
||
| 236 | * Should be changed to protected. |
||
| 237 | */ |
||
| 238 | public function handle_authorization() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Builds a URL to the Jetpack connection auth page. |
||
| 244 | * This needs rethinking. |
||
| 245 | * |
||
| 246 | * @param bool $raw If true, URL will not be escaped. |
||
| 247 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 248 | * If string, will be a custom redirect. |
||
| 249 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 250 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
| 251 | * |
||
| 252 | * @return string Connect URL |
||
| 253 | */ |
||
| 254 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Disconnects from the Jetpack servers. |
||
| 260 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 261 | */ |
||
| 262 | public function disconnect_site() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 268 | * |
||
| 269 | * @param string $domain The domain to check. |
||
| 270 | * |
||
| 271 | * @return bool|WP_Error |
||
| 272 | */ |
||
| 273 | public function is_usable_domain( $domain ) { |
||
| 360 | } |
||
| 361 |