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 | 'association_options' => array( |
||
| 56 | 'path' => '/association/options', |
||
| 57 | 'callback' => 'get_association_options', |
||
| 58 | 'permission_callback' => 'allow_access', |
||
| 59 | 'methods' => 'GET', |
||
| 60 | ), |
||
| 61 | 'attachment_data' => array( |
||
| 62 | 'path' => '/attachment', |
||
| 63 | 'callback' => 'get_attachment_data', |
||
| 64 | 'permission_callback' => 'allow_access', |
||
| 65 | 'methods' => 'GET', |
||
| 66 | 'args' => 'attachment_data_args_schema', |
||
| 67 | ), |
||
| 68 | 'block_renderer' => array( |
||
| 69 | 'path' => '/block-renderer', |
||
| 70 | 'callback' => 'block_renderer', |
||
| 71 | 'permission_callback' => 'block_renderer_permission', |
||
| 72 | 'methods' => 'POST', |
||
| 73 | 'args' => 'block_renderer_args_schema', |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Version of the API |
||
| 79 | * |
||
| 80 | * @see set_version() |
||
| 81 | * @see get_version() |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $version = '1'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Vendor slug for the API |
||
| 88 | * |
||
| 89 | * @see set_vendor() |
||
| 90 | * @see get_vendor() |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $vendor = 'carbon-fields'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * ContainerRepository instance |
||
| 97 | * |
||
| 98 | * @var ContainerRepository |
||
| 99 | */ |
||
| 100 | protected $container_repository; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param ContainerRepository $container_repository |
||
| 104 | */ |
||
| 105 | public function __construct( ContainerRepository $container_repository ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Boot up functionality |
||
| 111 | */ |
||
| 112 | public function boot() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set routes |
||
| 118 | * |
||
| 119 | * @param array $routes |
||
| 120 | */ |
||
| 121 | public function set_routes( $routes ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return routes |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function get_routes() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set version |
||
| 136 | * |
||
| 137 | * @param string $version |
||
| 138 | */ |
||
| 139 | public function set_version( $version ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Return version |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function get_version() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set vendor |
||
| 154 | * |
||
| 155 | * @param string $vendor |
||
| 156 | */ |
||
| 157 | public function set_vendor( $vendor ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return vendor |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function get_vendor() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Allow access to an endpoint |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function allow_access() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Register custom routes |
||
| 181 | * |
||
| 182 | * @see register_route() |
||
| 183 | */ |
||
| 184 | public function register_routes() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Register a custom REST route |
||
| 192 | * |
||
| 193 | * @param array $route |
||
| 194 | */ |
||
| 195 | protected function register_route( $route ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Proxy method for handling get/set for theme options |
||
| 206 | * |
||
| 207 | * @param \WP_REST_Request $request |
||
| 208 | * @return array|\WP_REST_Response |
||
| 209 | */ |
||
| 210 | public function options_accessor( $request ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Proxy method for handling theme options permissions |
||
| 222 | * |
||
| 223 | * @param \WP_REST_Request $request |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function options_permission( $request ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Wrapper method used for retrieving data from Data_Manager |
||
| 232 | * |
||
| 233 | * @param string $container_type |
||
| 234 | * @param string $object_id |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | protected function get_all_field_values( $container_type, $object_id = null ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get Carbon Fields post meta values |
||
| 258 | * |
||
| 259 | * @param array $data |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function get_post_meta( $data ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get Carbon Fields user meta values |
||
| 269 | * |
||
| 270 | * @param array $data |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function get_user_meta( $data ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get Carbon Fields term meta values |
||
| 280 | * |
||
| 281 | * @param array $data |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | public function get_term_meta( $data ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get Carbon Fields comment meta values |
||
| 291 | * |
||
| 292 | * @param array $data |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function get_comment_meta( $data ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get Carbon Fields association selected options. |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function get_association_data() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get Carbon Fields association options data. |
||
| 345 | * |
||
| 346 | * @access public |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function get_association_options() { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get attachment data by given ID or URL. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function get_attachment_data() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Retrieve Carbon theme options |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | protected function get_options() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set Carbon theme options |
||
| 390 | * |
||
| 391 | * @param \WP_REST_Request $request Full data about the request. |
||
| 392 | * @return \WP_Error|\WP_REST_Response |
||
| 393 | */ |
||
| 394 | protected function set_options( $request ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Checks if a given request has access to read blocks. |
||
| 414 | * |
||
| 415 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L78-L116 |
||
| 416 | * |
||
| 417 | * @param \WP_REST_Request |
||
| 418 | * @return true|\WP_Error |
||
| 419 | */ |
||
| 420 | public function block_renderer_permission( $request ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Returns the schema of the accepted arguments. |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | public function attachment_data_args_schema() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Returns the schema of the accepted arguments. |
||
| 474 | * |
||
| 475 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L56-L71 |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function block_renderer_args_schema() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Returns block output from block's registered render_callback. |
||
| 500 | * |
||
| 501 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L118-L154 |
||
| 502 | * |
||
| 503 | * @param \WP_REST_Request $request |
||
| 504 | * @return \WP_REST_Response|\WP_Error |
||
| 505 | */ |
||
| 506 | public function block_renderer( $request ) { |
||
| 537 | } |
||
| 538 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.