Complex classes like WP_REST_React_Controller 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 WP_REST_React_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class WP_REST_React_Controller { |
||
| 7 | /** |
||
| 8 | * The namespace of this controller's route. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | protected $namespace; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The base of this controller's route. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $rest_base; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructor. |
||
| 23 | */ |
||
| 24 | public function __construct() { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Register the routes for the objects of the controller. |
||
| 31 | */ |
||
| 32 | public function register_routes() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Check if a given request has access to read reactions. |
||
| 52 | * |
||
| 53 | * @param WP_REST_Request $request Full details about the request. |
||
| 54 | * @return WP_Error|boolean |
||
| 55 | */ |
||
| 56 | public function get_items_permissions_check( $request ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get a list of reactions. |
||
| 73 | * |
||
| 74 | * @param WP_REST_Request $request Full details about the request. |
||
| 75 | * @return WP_Error|WP_REST_Response |
||
| 76 | */ |
||
| 77 | public function get_items( $request ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Check if a given request has access to create a reaction |
||
| 132 | * |
||
| 133 | * @param WP_REST_Request $request Full details about the request. |
||
| 134 | * @return WP_Error|boolean |
||
| 135 | */ |
||
| 136 | public function create_item_permissions_check( $request ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Create a reaction. |
||
| 142 | * |
||
| 143 | * @param WP_REST_Request $request Full details about the request. |
||
| 144 | * @return WP_Error|WP_REST_Response |
||
| 145 | */ |
||
| 146 | public function create_item( $request ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Check if we can read a post. |
||
| 151 | * |
||
| 152 | * Correctly handles posts with the inherit status. |
||
| 153 | * |
||
| 154 | * @param object $post Post object. |
||
| 155 | * @return boolean Can we read it? |
||
| 156 | */ |
||
| 157 | public function check_read_post_permission( $post ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if we can edit a post. |
||
| 194 | * |
||
| 195 | * @param object $post Post object. |
||
| 196 | * @return boolean Can we edit it? |
||
| 197 | */ |
||
| 198 | protected function check_update_post_permission( $post ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Check if a given post type should be viewed or managed. |
||
| 210 | * |
||
| 211 | * @param object|string $post_type |
||
| 212 | * @return boolean Is post type allowed? |
||
| 213 | */ |
||
| 214 | protected function check_is_post_type_allowed( $post_type ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Prepare a reaction group output for response. |
||
| 228 | * |
||
| 229 | * @param array $reaction Reaction data. |
||
| 230 | * @param WP_REST_Request $request Request object. |
||
| 231 | * @return WP_REST_Response $response |
||
| 232 | */ |
||
| 233 | public function prepare_item_for_response( $reaction, $request ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Prepare a response for inserting into a collection. |
||
| 259 | * |
||
| 260 | * @param WP_REST_Response $response Response object. |
||
| 261 | * @return array Response data, ready for insertion into collection data. |
||
| 262 | */ |
||
| 263 | public function prepare_response_for_collection( $response ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Prepare links for the request. |
||
| 279 | * |
||
| 280 | * @param array $reaction Reaction. |
||
| 281 | * @return array Links for the given reaction. |
||
| 282 | */ |
||
| 283 | protected function prepare_links( $reaction ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the query params for collections |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function get_collection_params() { |
||
| 327 | /** |
||
| 328 | * Get the query params for collections |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function get_creation_params() { |
||
| 352 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.