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 REST_Connector 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 REST_Connector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class REST_Connector { |
||
21 | /** |
||
22 | * The Connection Manager. |
||
23 | * |
||
24 | * @var Manager |
||
25 | */ |
||
26 | private $connection; |
||
27 | |||
28 | /** |
||
29 | * This property stores the localized "Insufficient Permissions" error message. |
||
30 | * |
||
31 | * @var string Generic error message when user is not allowed to perform an action. |
||
32 | */ |
||
33 | private static $user_permissions_error_msg; |
||
34 | |||
35 | const JETPACK__DEBUGGER_PUBLIC_KEY = "\r\n" . '-----BEGIN PUBLIC KEY-----' . "\r\n" |
||
36 | . 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm+uLLVoxGCY71LS6KFc6' . "\r\n" |
||
37 | . '1UnF6QGBAsi5XF8ty9kR3/voqfOkpW+gRerM2Kyjy6DPCOmzhZj7BFGtxSV2ZoMX' . "\r\n" |
||
38 | . '9ZwWxzXhl/Q/6k8jg8BoY1QL6L2K76icXJu80b+RDIqvOfJruaAeBg1Q9NyeYqLY' . "\r\n" |
||
39 | . 'lEVzN2vIwcFYl+MrP/g6Bc2co7Jcbli+tpNIxg4Z+Hnhbs7OJ3STQLmEryLpAxQO' . "\r\n" |
||
40 | . 'q8cbhQkMx+FyQhxzSwtXYI/ClCUmTnzcKk7SgGvEjoKGAmngILiVuEJ4bm7Q1yok' . "\r\n" |
||
41 | . 'xl9+wcfW6JAituNhml9dlHCWnn9D3+j8pxStHihKy2gVMwiFRjLEeD8K/7JVGkb/' . "\r\n" |
||
42 | . 'EwIDAQAB' . "\r\n" |
||
43 | . '-----END PUBLIC KEY-----' . "\r\n"; |
||
44 | |||
45 | /** |
||
46 | * Constructor. |
||
47 | * |
||
48 | * @param Manager $connection The Connection Manager. |
||
49 | */ |
||
50 | public function __construct( Manager $connection ) { |
||
146 | |||
147 | /** |
||
148 | * Handles verification that a site is registered. |
||
149 | * |
||
150 | * @since 5.4.0 |
||
151 | * |
||
152 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
153 | * |
||
154 | * @return string|WP_Error |
||
155 | */ |
||
156 | public function verify_registration( WP_REST_Request $request ) { |
||
161 | |||
162 | /** |
||
163 | * Handles verification that a site is registered |
||
164 | * |
||
165 | * @since 5.4.0 |
||
166 | * |
||
167 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
168 | * |
||
169 | * @return array|wp-error |
||
|
|||
170 | */ |
||
171 | public static function remote_authorize( $request ) { |
||
181 | |||
182 | /** |
||
183 | * Get connection status for this Jetpack site. |
||
184 | * |
||
185 | * @since 4.3.0 |
||
186 | * |
||
187 | * @param bool $rest_response Should we return a rest response or a simple array. Default to rest response. |
||
188 | * |
||
189 | * @return WP_REST_Response|array Connection information. |
||
190 | */ |
||
191 | public static function connection_status( $rest_response = true ) { |
||
229 | |||
230 | /** |
||
231 | * Get plugins connected to the Jetpack. |
||
232 | * |
||
233 | * @since 8.6.0 |
||
234 | * |
||
235 | * @return WP_REST_Response|WP_Error Response or error object, depending on the request result. |
||
236 | */ |
||
237 | public function get_connection_plugins() { |
||
253 | |||
254 | /** |
||
255 | * Verify that user can view Jetpack admin page and can activate plugins. |
||
256 | * |
||
257 | * @since 8.8.0 |
||
258 | * |
||
259 | * @return bool|WP_Error Whether user has the capability 'activate_plugins'. |
||
260 | */ |
||
261 | public static function activate_plugins_permission_check() { |
||
268 | |||
269 | /** |
||
270 | * Permission check for the connection_plugins endpoint |
||
271 | * |
||
272 | * @return bool|WP_Error |
||
273 | */ |
||
274 | public static function connection_plugins_permission_check() { |
||
286 | |||
287 | /** |
||
288 | * Verifies if the request was signed with the Jetpack Debugger key |
||
289 | * |
||
290 | * @param string|null $pub_key The public key used to verify the signature. Default is the Jetpack Debugger key. This is used for testing purposes. |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | public static function is_request_signed_by_jetpack_debugger( $pub_key = null ) { |
||
330 | |||
331 | /** |
||
332 | * Verify that user is allowed to disconnect Jetpack. |
||
333 | * |
||
334 | * @since 8.8.0 |
||
335 | * |
||
336 | * @return bool|WP_Error Whether user has the capability 'jetpack_disconnect'. |
||
337 | */ |
||
338 | public static function jetpack_reconnect_permission_check() { |
||
345 | |||
346 | /** |
||
347 | * Returns generic error message when user is not allowed to perform an action. |
||
348 | * |
||
349 | * @return string The error message. |
||
350 | */ |
||
351 | public static function get_user_permissions_error_msg() { |
||
354 | |||
355 | /** |
||
356 | * The endpoint tried to partially or fully reconnect the website to WP.com. |
||
357 | * |
||
358 | * @since 8.8.0 |
||
359 | * |
||
360 | * @return \WP_REST_Response|WP_Error |
||
361 | */ |
||
362 | public function connection_reconnect() { |
||
398 | |||
399 | /** |
||
400 | * Verify that user is allowed to connect Jetpack. |
||
401 | * |
||
402 | * @since 9.7.0 |
||
403 | * |
||
404 | * @return bool|WP_Error Whether user has the capability 'jetpack_connect'. |
||
405 | */ |
||
406 | public static function jetpack_register_permission_check() { |
||
413 | |||
414 | /** |
||
415 | * The endpoint tried to partially or fully reconnect the website to WP.com. |
||
416 | * |
||
417 | * @since 7.7.0 |
||
418 | * |
||
419 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
420 | * |
||
421 | * @return \WP_REST_Response|WP_Error |
||
422 | */ |
||
423 | public function connection_register( $request ) { |
||
459 | |||
460 | } |
||
461 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.