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 |
||
| 11 | class WordPress_GitHub_Sync_Database { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Application container. |
||
| 15 | * |
||
| 16 | * @var WordPress_GitHub_Sync |
||
| 17 | */ |
||
| 18 | protected $app; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Cached list of currenly used shas |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $sha_list; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Currently whitelisted post types. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $whitelisted_post_types = array( 'post', 'page' ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Currently whitelisted post statuses. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $whitelisted_post_statuses = array( 'publish' ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Instantiates a new Database object. |
||
| 43 | * |
||
| 44 | * @param WordPress_GitHub_Sync $app Application container. |
||
| 45 | */ |
||
| 46 | public function __construct( WordPress_GitHub_Sync $app ) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Queries the database for all of the supported posts. |
||
| 52 | * |
||
| 53 | * @return WordPress_GitHub_Sync_Post[]|WP_Error |
||
| 54 | */ |
||
| 55 | public function fetch_all_supported() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Queries a post and returns it if it's supported. |
||
| 84 | * |
||
| 85 | * @param int $post_id Post ID to fetch. |
||
| 86 | * |
||
| 87 | * @return WP_Error|WordPress_GitHub_Sync_Post |
||
| 88 | */ |
||
| 89 | public function fetch_by_id( $post_id ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Queries for a post by provided sha. |
||
| 110 | * |
||
| 111 | * @param string $sha Post sha to fetch by. |
||
| 112 | * |
||
| 113 | * @return WordPress_GitHub_Sync_Post|WP_Error |
||
| 114 | */ |
||
| 115 | public function fetch_by_sha( $sha ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Generates a cache of currenly used shas and |
||
| 145 | * their accociated paths |
||
| 146 | * |
||
| 147 | * @return Array |
||
| 148 | */ |
||
| 149 | public function get_sha_list(){ |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Checks to see is a sha is in use. |
||
| 170 | * |
||
| 171 | * @param string $sha Post sha to check for. |
||
| 172 | * |
||
| 173 | * @return Booelan |
||
| 174 | */ |
||
| 175 | public function sha_exists($sha){ |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Checks to see is a sha is in use, and that the |
||
| 182 | * accociated path matches the given path |
||
| 183 | * |
||
| 184 | * @param string $sha Post sha to check for. |
||
| 185 | * @param string $path Patn to match the sha to. |
||
| 186 | * |
||
| 187 | * @return Booelan |
||
| 188 | */ |
||
| 189 | public function sha_exists_with_path($sha, $path){ |
||
| 198 | |||
| 199 | public function get_post_by_path($path){ |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Saves an array of Post objects to the database |
||
| 211 | * and associates their author as well as their latest |
||
| 212 | * |
||
| 213 | * @param WordPress_GitHub_Sync_Post[] $posts Array of Posts to save. |
||
| 214 | * @param string $email Author email. |
||
| 215 | * |
||
| 216 | * @return string|WP_Error |
||
| 217 | */ |
||
| 218 | public function save_posts( array $posts, $email ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Deletes a post from the database based on its GitHub path. |
||
| 270 | * |
||
| 271 | * @param string $path Path of Post to delete. |
||
| 272 | * |
||
| 273 | * @return string|WP_Error |
||
| 274 | */ |
||
| 275 | public function delete_post_by_path( $path ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Returns the list of post type permitted. |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | protected function get_whitelisted_post_types() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the list of post status permitted. |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | protected function get_whitelisted_post_statuses() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Formats a whitelist array for a query. |
||
| 336 | * |
||
| 337 | * @param array $whitelist Whitelisted posts to format into query. |
||
| 338 | * |
||
| 339 | * @return string Whitelist formatted for query |
||
| 340 | */ |
||
| 341 | protected function format_for_query( $whitelist ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Verifies that both the post's status & type |
||
| 351 | * are currently whitelisted |
||
| 352 | * |
||
| 353 | * @param WordPress_GitHub_Sync_Post $post Post to verify. |
||
| 354 | * |
||
| 355 | * @return boolean True if supported, false if not. |
||
| 356 | */ |
||
| 357 | protected function is_post_supported( WordPress_GitHub_Sync_Post $post ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Retrieves the commit user for a provided email address. |
||
| 380 | * |
||
| 381 | * Searches for a user with provided email address or returns |
||
| 382 | * the default user saved in the database. |
||
| 383 | * |
||
| 384 | * @param string $email User email address to search for. |
||
| 385 | * |
||
| 386 | * @return WP_Error|WP_User |
||
| 387 | */ |
||
| 388 | protected function fetch_commit_user( $email ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Sets the author latest revision |
||
| 413 | * of the provided post ID to the provided user. |
||
| 414 | * |
||
| 415 | * @param int $post_id Post ID to update revision author. |
||
| 416 | * @param int $user_id User ID for revision author. |
||
| 417 | * |
||
| 418 | * @return string|WP_Error |
||
| 419 | */ |
||
| 420 | protected function set_revision_author( $post_id, $user_id ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Updates the user ID for the provided post ID. |
||
| 446 | * |
||
| 447 | * Bypassing triggering any hooks, including creating new revisions. |
||
| 448 | * |
||
| 449 | * @param int $post_id Post ID to update. |
||
| 450 | * @param int $user_id User ID to update to. |
||
| 451 | * |
||
| 452 | * @return string|WP_Error |
||
| 453 | */ |
||
| 454 | protected function set_post_author( $post_id, $user_id ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Update the provided post's blob sha. |
||
| 490 | * |
||
| 491 | * @param WordPress_GitHub_Sync_Post $post Post to update. |
||
| 492 | * @param string $sha Sha to update to. |
||
| 493 | * |
||
| 494 | * @return bool|int |
||
| 495 | */ |
||
| 496 | public function set_post_sha( $post, $sha ) { |
||
| 499 | } |