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 WordPress_GitHub_Sync_Database 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 WordPress_GitHub_Sync_Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WordPress_GitHub_Sync_Database { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Application container. |
||
| 14 | * |
||
| 15 | * @var WordPress_GitHub_Sync |
||
| 16 | */ |
||
| 17 | protected $app; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Currently whitelisted post types. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $whitelisted_post_types = array( 'post', 'page' ); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Currently whitelisted post statuses. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $whitelisted_post_statuses = array( 'publish' ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Instantiates a new Database object. |
||
| 35 | * |
||
| 36 | * @param WordPress_GitHub_Sync $app Application container. |
||
| 37 | */ |
||
| 38 | 22 | public function __construct( WordPress_GitHub_Sync $app ) { |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Queries the database for all of the supported posts. |
||
| 44 | * |
||
| 45 | * @return WordPress_GitHub_Sync_Post[]|WP_Error |
||
| 46 | */ |
||
| 47 | 3 | public function fetch_all_supported() { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Queries a post and returns it if it's supported. |
||
| 76 | * |
||
| 77 | * @param int $post_id Post ID to fetch. |
||
| 78 | * |
||
| 79 | * @return WP_Error|WordPress_GitHub_Sync_Post |
||
| 80 | */ |
||
| 81 | 5 | public function fetch_by_id( $post_id ) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Queries for a post by provided sha. |
||
| 102 | * |
||
| 103 | * @param string $sha Post sha to fetch by. |
||
| 104 | * |
||
| 105 | * @return WordPress_GitHub_Sync_Post|WP_Error |
||
| 106 | */ |
||
| 107 | 2 | public function fetch_by_sha( $sha ) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Saves an array of Post objects to the database |
||
| 137 | * and associates their author as well as their latest |
||
| 138 | * |
||
| 139 | * @param WordPress_GitHub_Sync_Post[] $posts Array of Posts to save. |
||
| 140 | * @param string $email Author email. |
||
| 141 | * |
||
| 142 | * @return string|WP_Error |
||
| 143 | */ |
||
| 144 | 6 | public function save_posts( array $posts, $email ) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Deletes a post from the database based on its GitHub path. |
||
| 199 | * |
||
| 200 | * @param string $path Path of Post to delete. |
||
| 201 | * |
||
| 202 | * @return string|WP_Error |
||
| 203 | */ |
||
| 204 | 5 | public function delete_post_by_path( $path ) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the list of post type permitted. |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | 10 | protected function get_whitelisted_post_types() { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the list of post status permitted. |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 7 | protected function get_whitelisted_post_statuses() { |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Formats a whitelist array for a query. |
||
| 307 | * |
||
| 308 | * @param array $whitelist Whitelisted posts to format into query. |
||
| 309 | * |
||
| 310 | * @return string Whitelist formatted for query |
||
| 311 | */ |
||
| 312 | protected function format_for_query( $whitelist ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Verifies that both the post's status & type |
||
| 322 | * are currently whitelisted |
||
| 323 | * |
||
| 324 | * @param WordPress_GitHub_Sync_Post $post Post to verify. |
||
| 325 | * |
||
| 326 | * @return boolean True if supported, false if not. |
||
| 327 | */ |
||
| 328 | 5 | protected function is_post_supported( WordPress_GitHub_Sync_Post $post ) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Retrieves the commit user for a provided email address. |
||
| 351 | * |
||
| 352 | * Searches for a user with provided email address or returns |
||
| 353 | * the default user saved in the database. |
||
| 354 | * |
||
| 355 | * @param string $email User email address to search for. |
||
| 356 | * |
||
| 357 | * @return WP_Error|WP_User |
||
| 358 | */ |
||
| 359 | 6 | protected function fetch_commit_user( $email ) { |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Sets the author latest revision |
||
| 384 | * of the provided post ID to the provided user. |
||
| 385 | * |
||
| 386 | * @param int $post_id Post ID to update revision author. |
||
| 387 | * @param int $user_id User ID for revision author. |
||
| 388 | * |
||
| 389 | * @return string|WP_Error |
||
| 390 | */ |
||
| 391 | 6 | protected function set_revision_author( $post_id, $user_id ) { |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Updates the user ID for the provided post ID. |
||
| 417 | * |
||
| 418 | * Bypassing triggering any hooks, including creating new revisions. |
||
| 419 | * |
||
| 420 | * @param int $post_id Post ID to update. |
||
| 421 | * @param int $user_id User ID to update to. |
||
| 422 | * |
||
| 423 | * @return string|WP_Error |
||
| 424 | */ |
||
| 425 | 6 | protected function set_post_author( $post_id, $user_id ) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Update the provided post's blob sha. |
||
| 461 | * |
||
| 462 | * @param WordPress_GitHub_Sync_Post $post Post to update. |
||
| 463 | * @param string $sha Sha to update to. |
||
| 464 | * |
||
| 465 | * @return bool|int |
||
| 466 | */ |
||
| 467 | 1 | public function set_post_sha( $post, $sha ) { |
|
| 470 | } |
||
| 471 |