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() { |
||
| 33 | $args = func_get_args(); |
||
| 34 | $format = array_shift( $args ); |
||
| 35 | $base = WPCOM_JSON_API__BASE; |
||
| 36 | |||
| 37 | $path = array_pop( $args ); |
||
| 38 | |||
| 39 | if ( $path ) { |
||
| 40 | $path = '/' . ltrim( $path, '/' ); |
||
| 41 | } |
||
| 42 | |||
| 43 | $args[] = $path; |
||
| 44 | |||
| 45 | // Escape any % in args before using sprintf |
||
| 46 | $escaped_args = array(); |
||
| 47 | foreach ( $args as $arg_key => $arg_value ) { |
||
| 48 | $escaped_args[ $arg_key ] = str_replace( '%', '%%', $arg_value ); |
||
| 49 | } |
||
| 50 | |||
| 51 | $relative_path = vsprintf( "$format%s", $escaped_args ); |
||
| 52 | |||
| 53 | if ( ! wp_startswith( $relative_path, '.' ) ) { |
||
| 54 | // Generic version. Match the requested version as best we can |
||
| 55 | $api_version = $this->get_closest_version_of_endpoint( $format, $relative_path ); |
||
| 56 | $base = substr( $base, 0, - 1 ) . $api_version; |
||
| 57 | } |
||
| 58 | |||
| 59 | // escape any % in the relative path before running it through sprintf again |
||
| 60 | $relative_path = str_replace( '%', '%%', $relative_path ); |
||
| 61 | // http, WPCOM_JSON_API__BASE, ... , path |
||
| 62 | // %s , %s , $format, %s |
||
| 63 | return esc_url_raw( sprintf( "https://%s$relative_path", $base ) ); |
||
| 64 | } |
||
| 65 | |||
| 66 | function get_me_link( $path = '' ) { |
||
| 69 | |||
| 70 | function get_taxonomy_link( $blog_id, $taxonomy_id, $taxonomy_type, $path = '' ) { |
||
| 71 | if ( 'category' === $taxonomy_type ) |
||
| 72 | return $this->get_link( '/sites/%d/categories/slug:%s', $blog_id, $taxonomy_id, $path ); |
||
| 73 | else |
||
| 74 | return $this->get_link( '/sites/%d/tags/slug:%s', $blog_id, $taxonomy_id, $path ); |
||
| 75 | } |
||
| 76 | |||
| 77 | function get_media_link( $blog_id, $media_id, $path = '' ) { |
||
| 80 | |||
| 81 | function get_site_link( $blog_id, $path = '' ) { |
||
| 84 | |||
| 85 | function get_post_link( $blog_id, $post_id, $path = '' ) { |
||
| 88 | |||
| 89 | function get_comment_link( $blog_id, $comment_id, $path = '' ) { |
||
| 92 | |||
| 93 | function get_publicize_connection_link( $blog_id, $publicize_connection_id, $path = '' ) { |
||
| 96 | |||
| 97 | function get_publicize_connections_link( $keyring_token_id, $path = '' ) { |
||
| 100 | |||
| 101 | function get_keyring_connection_link( $keyring_token_id, $path = '' ) { |
||
| 104 | |||
| 105 | function get_external_service_link( $external_service, $path = '' ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Try to find the closest supported version of an endpoint to the current endpoint |
||
| 111 | * |
||
| 112 | * For example, if we were looking at the path /animals/panda: |
||
| 113 | * - if the current endpoint is v1.3 and there is a v1.3 of /animals/%s available, we return 1.3 |
||
| 114 | * - if the current endpoint is v1.3 and there is no v1.3 of /animals/%s known, we fall back to the |
||
| 115 | * maximum available version of /animals/%s, e.g. 1.1 |
||
| 116 | * |
||
| 117 | * This method is used in get_link() to construct meta links for API responses. |
||
| 118 | * |
||
| 119 | * @param $template_path The generic endpoint path, e.g. /sites/%s |
||
| 120 | * @param $path string The current endpoint path, relative to the version, e.g. /sites/12345 |
||
| 121 | * @param $method string Request method used to access the endpoint path |
||
| 122 | * @return string The current version, or otherwise the maximum version available |
||
| 123 | */ |
||
| 124 | function get_closest_version_of_endpoint( $template_path, $path, $request_method = 'GET' ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get an array of endpoint paths with their associated versions |
||
| 208 | * |
||
| 209 | * The result is cached for 30 minutes. |
||
| 210 | * |
||
| 211 | * @return array Array of endpoint paths, min_versions and max_versions, keyed by last segment of path |
||
| 212 | **/ |
||
| 213 | protected function get_endpoint_path_versions() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Grab the last segment of a relative path |
||
| 250 | * |
||
| 251 | * @param string $path Path |
||
| 252 | * @return string Last path segment |
||
| 253 | */ |
||
| 254 | protected function get_last_segment_of_relative_path( $path) { |
||
| 263 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: