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 Router 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 Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Router { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Carbon Fields routes |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $routes = array( |
||
| 19 | 'post_meta' => array( |
||
| 20 | 'path' => '/posts/(?P<id>\d+)', |
||
| 21 | 'callback' => 'get_post_meta', |
||
| 22 | 'permission_callback' => 'allow_access', |
||
| 23 | 'methods' => 'GET', |
||
| 24 | ), |
||
| 25 | 'term_meta' => array( |
||
| 26 | 'path' => '/terms/(?P<id>\d+)', |
||
| 27 | 'callback' => 'get_term_meta', |
||
| 28 | 'permission_callback' => 'allow_access', |
||
| 29 | 'methods' => 'GET', |
||
| 30 | ), |
||
| 31 | 'user_meta' => array( |
||
| 32 | 'path' => '/users/(?P<id>\d+)', |
||
| 33 | 'callback' => 'get_user_meta', |
||
| 34 | 'permission_callback' => 'allow_access', |
||
| 35 | 'methods' => 'GET', |
||
| 36 | ), |
||
| 37 | 'comment_meta' => array( |
||
| 38 | 'path' => '/comments/(?P<id>\d+)', |
||
| 39 | 'callback' => 'get_comment_meta', |
||
| 40 | 'permission_callback' => 'allow_access', |
||
| 41 | 'methods' => 'GET', |
||
| 42 | ), |
||
| 43 | 'theme_options' => array( |
||
| 44 | 'path' => '/options/', |
||
| 45 | 'callback' => 'options_accessor', |
||
| 46 | 'permission_callback' => 'options_permission', |
||
| 47 | 'methods' => array( 'GET', 'POST' ), |
||
| 48 | ), |
||
| 49 | 'association_data' => array( |
||
| 50 | 'path' => '/association', |
||
| 51 | 'callback' => 'get_association_data', |
||
| 52 | 'permission_callback' => 'allow_access', |
||
| 53 | 'methods' => 'GET', |
||
| 54 | ), |
||
| 55 | 'attachment_data' => array( |
||
| 56 | 'path' => '/attachment', |
||
| 57 | 'callback' => 'get_attachment_data', |
||
| 58 | 'permission_callback' => 'allow_access', |
||
| 59 | 'methods' => 'GET', |
||
| 60 | 'args' => 'attachment_data_args_schema', |
||
| 61 | ), |
||
| 62 | 'block_renderer' => array( |
||
| 63 | 'path' => '/block-renderer', |
||
| 64 | 'callback' => 'block_renderer', |
||
| 65 | 'permission_callback' => 'block_renderer_permission', |
||
| 66 | 'methods' => 'POST', |
||
| 67 | 'args' => 'block_renderer_args_schema', |
||
| 68 | ) |
||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Version of the API |
||
| 73 | * |
||
| 74 | * @see set_version() |
||
| 75 | * @see get_version() |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $version = '1'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Vendor slug for the API |
||
| 82 | * |
||
| 83 | * @see set_vendor() |
||
| 84 | * @see get_vendor() |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $vendor = 'carbon-fields'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * ContainerRepository instance |
||
| 91 | * |
||
| 92 | * @var ContainerRepository |
||
| 93 | */ |
||
| 94 | protected $container_repository; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param ContainerRepository $container_repository |
||
| 98 | */ |
||
| 99 | public function __construct( ContainerRepository $container_repository ) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Boot up functionality |
||
| 105 | */ |
||
| 106 | public function boot() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set routes |
||
| 112 | * |
||
| 113 | * @param array $routes |
||
| 114 | */ |
||
| 115 | public function set_routes( $routes ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Return routes |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function get_routes() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set version |
||
| 130 | * |
||
| 131 | * @param string $version |
||
| 132 | */ |
||
| 133 | public function set_version( $version ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Return version |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function get_version() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set vendor |
||
| 148 | * |
||
| 149 | * @param string $vendor |
||
| 150 | */ |
||
| 151 | public function set_vendor( $vendor ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return vendor |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function get_vendor() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Allow access to an endpoint |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function allow_access() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Register custom routes |
||
| 175 | * |
||
| 176 | * @see register_route() |
||
| 177 | */ |
||
| 178 | public function register_routes() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Register a custom REST route |
||
| 186 | * |
||
| 187 | * @param array $route |
||
| 188 | */ |
||
| 189 | protected function register_route( $route ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Proxy method for handling get/set for theme options |
||
| 200 | * |
||
| 201 | * @param \WP_REST_Request $request |
||
| 202 | * @return array|\WP_REST_Response |
||
| 203 | */ |
||
| 204 | public function options_accessor( $request ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Proxy method for handling theme options permissions |
||
| 216 | * |
||
| 217 | * @param \WP_REST_Request $request |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function options_permission( $request ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Wrapper method used for retrieving data from Data_Manager |
||
| 226 | * |
||
| 227 | * @param string $container_type |
||
| 228 | * @param string $object_id |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function get_all_field_values( $container_type, $object_id = null ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get Carbon Fields post meta values |
||
| 252 | * |
||
| 253 | * @param array $data |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function get_post_meta( $data ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get Carbon Fields user meta values |
||
| 263 | * |
||
| 264 | * @param array $data |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function get_user_meta( $data ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get Carbon Fields term meta values |
||
| 274 | * |
||
| 275 | * @param array $data |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function get_term_meta( $data ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get Carbon Fields comment meta values |
||
| 285 | * |
||
| 286 | * @param array $data |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function get_comment_meta( $data ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get Carbon Fields association options data. |
||
| 296 | * |
||
| 297 | * @access public |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function get_association_data() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get attachment data by given ID or URL. |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public function get_attachment_data() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Retrieve Carbon theme options |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | protected function get_options() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set Carbon theme options |
||
| 361 | * |
||
| 362 | * @param \WP_REST_Request $request Full data about the request. |
||
| 363 | * @return \WP_Error|\WP_REST_Response |
||
| 364 | */ |
||
| 365 | protected function set_options( $request ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Checks if a given request has access to read blocks. |
||
| 385 | * |
||
| 386 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L78-L116 |
||
| 387 | * |
||
| 388 | * @param \WP_REST_Request |
||
| 389 | * @return true|\WP_Error |
||
| 390 | */ |
||
| 391 | public function block_renderer_permission( $request ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Returns the schema of the accepted arguments. |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function attachment_data_args_schema() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns the schema of the accepted arguments. |
||
| 445 | * |
||
| 446 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L56-L71 |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | public function block_renderer_args_schema() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns block output from block's registered render_callback. |
||
| 471 | * |
||
| 472 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L118-L154 |
||
| 473 | * |
||
| 474 | * @param \WP_REST_Request $request |
||
| 475 | * @return \WP_REST_Response|\WP_Error |
||
| 476 | */ |
||
| 477 | public function block_renderer( $request ) { |
||
| 508 | } |
||
| 509 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.