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 ) { |
||
167 | |||
168 | /** |
||
169 | * Handles verification that a site is registered. |
||
170 | * |
||
171 | * @since 5.4.0 |
||
172 | * |
||
173 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
174 | * |
||
175 | * @return string|WP_Error |
||
176 | */ |
||
177 | public function verify_registration( WP_REST_Request $request ) { |
||
182 | |||
183 | /** |
||
184 | * Handles verification that a site is registered |
||
185 | * |
||
186 | * @since 5.4.0 |
||
187 | * |
||
188 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
189 | * |
||
190 | * @return array|wp-error |
||
|
|||
191 | */ |
||
192 | public static function remote_authorize( $request ) { |
||
202 | |||
203 | /** |
||
204 | * Get connection status for this Jetpack site. |
||
205 | * |
||
206 | * @since 4.3.0 |
||
207 | * |
||
208 | * @param bool $rest_response Should we return a rest response or a simple array. Default to rest response. |
||
209 | * |
||
210 | * @return WP_REST_Response|array Connection information. |
||
211 | */ |
||
212 | public static function connection_status( $rest_response = true ) { |
||
250 | |||
251 | /** |
||
252 | * Get plugins connected to the Jetpack. |
||
253 | * |
||
254 | * @since 8.6.0 |
||
255 | * |
||
256 | * @return WP_REST_Response|WP_Error Response or error object, depending on the request result. |
||
257 | */ |
||
258 | public function get_connection_plugins() { |
||
274 | |||
275 | /** |
||
276 | * Verify that user can view Jetpack admin page and can activate plugins. |
||
277 | * |
||
278 | * @since 8.8.0 |
||
279 | * |
||
280 | * @return bool|WP_Error Whether user has the capability 'activate_plugins'. |
||
281 | */ |
||
282 | public static function activate_plugins_permission_check() { |
||
289 | |||
290 | /** |
||
291 | * Permission check for the connection_plugins endpoint |
||
292 | * |
||
293 | * @return bool|WP_Error |
||
294 | */ |
||
295 | public static function connection_plugins_permission_check() { |
||
307 | |||
308 | /** |
||
309 | * Verifies if the request was signed with the Jetpack Debugger key |
||
310 | * |
||
311 | * @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. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public static function is_request_signed_by_jetpack_debugger( $pub_key = null ) { |
||
351 | |||
352 | /** |
||
353 | * Verify that user is allowed to disconnect Jetpack. |
||
354 | * |
||
355 | * @since 8.8.0 |
||
356 | * |
||
357 | * @return bool|WP_Error Whether user has the capability 'jetpack_disconnect'. |
||
358 | */ |
||
359 | public static function jetpack_reconnect_permission_check() { |
||
366 | |||
367 | /** |
||
368 | * Returns generic error message when user is not allowed to perform an action. |
||
369 | * |
||
370 | * @return string The error message. |
||
371 | */ |
||
372 | public static function get_user_permissions_error_msg() { |
||
375 | |||
376 | /** |
||
377 | * The endpoint tried to partially or fully reconnect the website to WP.com. |
||
378 | * |
||
379 | * @since 8.8.0 |
||
380 | * |
||
381 | * @return \WP_REST_Response|WP_Error |
||
382 | */ |
||
383 | public function connection_reconnect() { |
||
419 | |||
420 | /** |
||
421 | * Verify that user is allowed to connect Jetpack. |
||
422 | * |
||
423 | * @since 9.7.0 |
||
424 | * |
||
425 | * @return bool|WP_Error Whether user has the capability 'jetpack_connect'. |
||
426 | */ |
||
427 | public static function jetpack_register_permission_check() { |
||
434 | |||
435 | /** |
||
436 | * The endpoint tried to partially or fully reconnect the website to WP.com. |
||
437 | * |
||
438 | * @since 7.7.0 |
||
439 | * |
||
440 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
441 | * |
||
442 | * @return \WP_REST_Response|WP_Error |
||
443 | */ |
||
444 | public function connection_register( $request ) { |
||
480 | |||
481 | /** |
||
482 | * Get the authorization URL. |
||
483 | * |
||
484 | * @since 9.8.0 |
||
485 | * |
||
486 | * @param \WP_REST_Request $request The request sent to the WP REST API. |
||
487 | * |
||
488 | * @return \WP_REST_Response|WP_Error |
||
489 | */ |
||
490 | public function connection_authorize_url( $request ) { |
||
509 | |||
510 | } |
||
511 |
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.