Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Base |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @deprecated use all-caps version |
||
| 32 | */ |
||
| 33 | // @codingStandardsIgnoreStart |
||
| 34 | const header_prefix_for_ee = 'X-EE-'; |
||
| 35 | // @codingStandardsIgnoreEnd |
||
| 36 | |||
| 37 | const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @deprecated use all-caps version instead |
||
| 41 | */ |
||
| 42 | // @codingStandardsIgnoreStart |
||
| 43 | const header_prefix_for_wp = 'X-WP-'; |
||
| 44 | // @codingStandardsIgnoreEnd |
||
| 45 | |||
| 46 | const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Contains debug info we'll send back in the response headers |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $debug_info = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Indicates whether or not the API is in debug mode |
||
| 57 | * |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | protected $debug_mode = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Indicates the version that was requested |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $requested_version; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * flat array of headers to send in the response |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $response_headers = array(); |
||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | public function __construct() |
||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets the version the user requested |
||
| 89 | * |
||
| 90 | * @param string $version eg '4.8' |
||
| 91 | */ |
||
| 92 | public function setRequestedVersion($version) |
||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Sets some debug info that we'll send back in headers |
||
| 101 | * |
||
| 102 | * @param string $key |
||
| 103 | * @param string|array $info |
||
| 104 | */ |
||
| 105 | protected function setDebugInfo($key, $info) |
||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Sets headers for the response |
||
| 114 | * |
||
| 115 | * @param string $header_key , excluding the "X-EE-" part |
||
| 116 | * @param array|string $value if an array, multiple headers will be added, one |
||
| 117 | * for each key in the array |
||
| 118 | * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
||
| 119 | * the standard WP one |
||
| 120 | */ |
||
| 121 | protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns a flat array of headers to be added to the response |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | protected function getResponseHeaders() |
||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds error notices from EE_Error onto the provided \WP_Error |
||
| 154 | * |
||
| 155 | * @param WP_Error $wp_error_response |
||
| 156 | * @return WP_Error |
||
| 157 | */ |
||
| 158 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Sends a response, but also makes sure to attach headers that |
||
| 176 | * are handy for debugging. |
||
| 177 | * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
||
| 178 | * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
||
| 179 | * the response, others? |
||
| 180 | * |
||
| 181 | * @param array|WP_Error|Exception|RestException $response |
||
| 182 | * @return WP_REST_Response |
||
| 183 | */ |
||
| 184 | public function sendResponse($response) |
||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Converts the \WP_Error into `WP_REST_Response. |
||
| 221 | * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
||
| 222 | * (which is protected) |
||
| 223 | * |
||
| 224 | * @param WP_Error $wp_error |
||
| 225 | * @return WP_REST_Response |
||
| 226 | */ |
||
| 227 | protected function createRestResponseFromWpError(WP_Error $wp_error) |
||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Array of headers derived from EE success, attention, and error messages |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | protected function getHeadersFromEeNotices() |
||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Finds which version of the API was requested given the route, and returns it. |
||
| 289 | * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
||
| 290 | * "4.8.29". |
||
| 291 | * We should know hte requested version in this model though, so if no route is |
||
| 292 | * provided just use what we set earlier |
||
| 293 | * |
||
| 294 | * @param string $route |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getRequestedVersion($route = null) |
||
| 313 | |||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Applies the regex to the route, then creates an array using the values of |
||
| 318 | * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
||
| 319 | * For example, if you call |
||
| 320 | * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
||
| 321 | * it will return array( 'version' => '4.8', 'model' => 'events' ) |
||
| 322 | * |
||
| 323 | * @param string $route |
||
| 324 | * @param string $regex |
||
| 325 | * @param array $match_keys EXCLUDING matching the entire regex |
||
| 326 | * @return array where $match_keys are the keys (the first value of $match_keys |
||
| 327 | * becomes the first key of the return value, etc. Eg passing in $match_keys of |
||
| 328 | * array( 'model', 'id' ), will, if the regex is successful, will return |
||
| 329 | * array( 'model' => 'foo', 'id' => 'bar' ) |
||
| 330 | * @throws EE_Error if it couldn't be parsed |
||
| 331 | */ |
||
| 332 | public function parseRoute($route, $regex, $match_keys) |
||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
||
| 359 | * |
||
| 360 | * @param \WP_REST_Request $request |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function getBodyParams(\WP_REST_Request $request) |
||
| 376 | } |
||
| 377 | |||
| 379 |