Complex classes like WPCOM_JSON_API_Links 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 WPCOM_JSON_API_Links, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class WPCOM_JSON_API_Links { |
||
| 6 | private $api; |
||
| 7 | private static $instance; |
||
| 8 | |||
| 9 | public static function getInstance() { |
||
| 16 | |||
| 17 | // protect these methods for singleton |
||
| 18 | protected function __construct() { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Generate a URL to an endpoint |
||
| 26 | * |
||
| 27 | * Used to construct meta links in API responses |
||
| 28 | * |
||
| 29 | * @param mixed $args Optional arguments to be appended to URL |
||
|
|
|||
| 30 | * @return string Endpoint URL |
||
| 31 | **/ |
||
| 32 | function get_link() { |
||
| 65 | |||
| 66 | function get_me_link( $path = '' ) { |
||
| 69 | |||
| 70 | function get_taxonomy_link( $blog_id, $taxonomy_id, $taxonomy_type, $path = '' ) { |
||
| 71 | switch ( $taxonomy_type ) { |
||
| 72 | case 'category': |
||
| 73 | return $this->get_link( '/sites/%d/categories/slug:%s', $blog_id, $taxonomy_id, $path ); |
||
| 74 | |||
| 75 | case 'post_tag': |
||
| 76 | return $this->get_link( '/sites/%d/tags/slug:%s', $blog_id, $taxonomy_id, $path ); |
||
| 77 | |||
| 78 | default: |
||
| 79 | return $this->get_link( '/sites/%d/taxonomies/%s/terms/slug:%s', $blog_id, $taxonomy_type, $taxonomy_id, $path ); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | function get_media_link( $blog_id, $media_id, $path = '' ) { |
||
| 86 | |||
| 87 | function get_site_link( $blog_id, $path = '' ) { |
||
| 90 | |||
| 91 | function get_post_link( $blog_id, $post_id, $path = '' ) { |
||
| 94 | |||
| 95 | function get_comment_link( $blog_id, $comment_id, $path = '' ) { |
||
| 98 | |||
| 99 | function get_publicize_connection_link( $blog_id, $publicize_connection_id, $path = '' ) { |
||
| 102 | |||
| 103 | function get_publicize_connections_link( $keyring_token_id, $path = '' ) { |
||
| 106 | |||
| 107 | function get_keyring_connection_link( $keyring_token_id, $path = '' ) { |
||
| 110 | |||
| 111 | function get_external_service_link( $external_service, $path = '' ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Try to find the closest supported version of an endpoint to the current endpoint |
||
| 117 | * |
||
| 118 | * For example, if we were looking at the path /animals/panda: |
||
| 119 | * - if the current endpoint is v1.3 and there is a v1.3 of /animals/%s available, we return 1.3 |
||
| 120 | * - if the current endpoint is v1.3 and there is no v1.3 of /animals/%s known, we fall back to the |
||
| 121 | * maximum available version of /animals/%s, e.g. 1.1 |
||
| 122 | * |
||
| 123 | * This method is used in get_link() to construct meta links for API responses. |
||
| 124 | * |
||
| 125 | * @param $template_path The generic endpoint path, e.g. /sites/%s |
||
| 126 | * @param $path string The current endpoint path, relative to the version, e.g. /sites/12345 |
||
| 127 | * @param $method string Request method used to access the endpoint path |
||
| 128 | * @return string The current version, or otherwise the maximum version available |
||
| 129 | */ |
||
| 130 | function get_closest_version_of_endpoint( $template_path, $path, $request_method = 'GET' ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get an array of endpoint paths with their associated versions |
||
| 214 | * |
||
| 215 | * The result is cached for 30 minutes. |
||
| 216 | * |
||
| 217 | * @return array Array of endpoint paths, min_versions and max_versions, keyed by last segment of path |
||
| 218 | **/ |
||
| 219 | protected function get_endpoint_path_versions() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Grab the last segment of a relative path |
||
| 256 | * |
||
| 257 | * @param string $path Path |
||
| 258 | * @return string Last path segment |
||
| 259 | */ |
||
| 260 | protected function get_last_segment_of_relative_path( $path) { |
||
| 269 | } |
||
| 270 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.