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 | ), |
||
| 61 | 'block_renderer' => array( |
||
| 62 | 'path' => '/block-renderer', |
||
| 63 | 'callback' => 'block_renderer', |
||
| 64 | 'permission_callback' => 'block_renderer_permission', |
||
| 65 | 'methods' => 'POST', |
||
| 66 | 'args' => 'block_renderer_args_schema', |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Version of the API |
||
| 72 | * |
||
| 73 | * @see set_version() |
||
| 74 | * @see get_version() |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $version = '1'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Vendor slug for the API |
||
| 81 | * |
||
| 82 | * @see set_vendor() |
||
| 83 | * @see get_vendor() |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $vendor = 'carbon-fields'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * ContainerRepository instance |
||
| 90 | * |
||
| 91 | * @var ContainerRepository |
||
| 92 | */ |
||
| 93 | protected $container_repository; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param ContainerRepository $container_repository |
||
| 97 | */ |
||
| 98 | public function __construct( ContainerRepository $container_repository ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Boot up functionality |
||
| 104 | */ |
||
| 105 | public function boot() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set routes |
||
| 111 | */ |
||
| 112 | public function set_routes( $routes ) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Return routes |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function get_routes() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set version |
||
| 127 | */ |
||
| 128 | public function set_version( $version ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return version |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function get_version() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set vendor |
||
| 143 | */ |
||
| 144 | public function set_vendor( $vendor ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return vendor |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function get_vendor() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Allow access to an endpoint |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function allow_access() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Register custom routes |
||
| 168 | * |
||
| 169 | * @see register_route() |
||
| 170 | */ |
||
| 171 | public function register_routes() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Register a custom REST route |
||
| 179 | * |
||
| 180 | * @param array $route |
||
| 181 | */ |
||
| 182 | protected function register_route( $route ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Proxy method for handling get/set for theme options |
||
| 193 | * |
||
| 194 | * @param WP_REST_Request $request |
||
| 195 | * @return array|WP_REST_Response |
||
| 196 | */ |
||
| 197 | public function options_accessor( $request ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Proxy method for handling theme options permissions |
||
| 209 | * |
||
| 210 | * @param WP_REST_Request $request |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function options_permission( $request ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Wrapper method used for retrieving data from Data_Manager |
||
| 219 | * |
||
| 220 | * @param string $container_type |
||
| 221 | * @param string $object_id |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | protected function get_all_field_values( $container_type, $object_id = null ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get Carbon Fields post meta values |
||
| 245 | * |
||
| 246 | * @param array $data |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function get_post_meta( $data ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get Carbon Fields user meta values |
||
| 256 | * |
||
| 257 | * @param array $data |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function get_user_meta( $data ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get Carbon Fields term meta values |
||
| 267 | * |
||
| 268 | * @param array $data |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function get_term_meta( $data ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get Carbon Fields comment meta values |
||
| 278 | * |
||
| 279 | * @param array $data |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function get_comment_meta( $data ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get Carbon Fields association options data. |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function get_association_data() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get attachment data by given ID or URL. |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function get_attachment_data() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Retrieve Carbon theme options |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | protected function get_options() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set Carbon theme options |
||
| 341 | * |
||
| 342 | * @param WP_REST_Request $request Full data about the request. |
||
| 343 | * @return WP_Error|WP_REST_Response |
||
| 344 | */ |
||
| 345 | protected function set_options( $request ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Checks if a given request has access to read blocks. |
||
| 365 | * |
||
| 366 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L78-L116 |
||
| 367 | * |
||
| 368 | * @param WP_REST_Request |
||
| 369 | * @return true|WP_Error |
||
| 370 | */ |
||
| 371 | public function block_renderer_permission( $request ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the schema of the accepted arguments. |
||
| 405 | * |
||
| 406 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L56-L71 |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function block_renderer_args_schema() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Returns block output from block's registered render_callback. |
||
| 431 | * |
||
| 432 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php#L118-L154 |
||
| 433 | * |
||
| 434 | * @param WP_REST_Request $request |
||
| 435 | * @return WP_REST_Response|WP_Error |
||
| 436 | */ |
||
| 437 | public function block_renderer( $request ) { |
||
| 468 | } |
||
| 469 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.