Complex classes like WordPress_GitHub_Sync_Post 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_Post, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WordPress_GitHub_Sync_Post { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Api object |
||
| 14 | * |
||
| 15 | * @var WordPress_GitHub_Sync_Api |
||
| 16 | */ |
||
| 17 | public $api; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Post ID |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | public $id = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Post object |
||
| 27 | * @var WP_Post |
||
| 28 | */ |
||
| 29 | public $post; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Post args. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $args; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Post meta. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $meta; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Whether the post has been saved. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $new = true; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Instantiates a new Post object |
||
| 54 | * |
||
| 55 | * @param int|array $id_or_args Either a post ID or an array of arguments. |
||
| 56 | * @param WordPress_GitHub_Sync_Api $api API object. |
||
| 57 | * |
||
| 58 | * @todo remove database operations from this method |
||
| 59 | */ |
||
| 60 | 36 | public function __construct( $id_or_args, WordPress_GitHub_Sync_Api $api ) { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the post type |
||
| 87 | */ |
||
| 88 | 19 | public function type() { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the post type |
||
| 94 | */ |
||
| 95 | 22 | public function status() { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the post name |
||
| 101 | */ |
||
| 102 | 12 | public function name() { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns true if the post has a password |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | 2 | public function has_password() { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Combines the 2 content parts for GitHub |
||
| 116 | */ |
||
| 117 | 5 | public function github_content() { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * The post's YAML frontmatter |
||
| 126 | * |
||
| 127 | * Returns String the YAML frontmatter, ready to be written to the file |
||
| 128 | */ |
||
| 129 | 5 | public function front_matter() { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the post_content |
||
| 135 | * |
||
| 136 | * Markdownify's the content if applicable |
||
| 137 | */ |
||
| 138 | 5 | public function post_content() { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Retrieves or calculates the proper GitHub path for a given post |
||
| 154 | * |
||
| 155 | * Returns (string) the path relative to repo root |
||
| 156 | */ |
||
| 157 | 11 | public function github_path() { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get GitHub directory based on post |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 17 | public function github_directory() { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Build GitHub filename based on post |
||
| 205 | */ |
||
| 206 | 11 | public function github_filename() { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Returns a post slug we can use in the GitHub filename |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 11 | protected function get_name() { |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the URL for the post on GitHub. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 3 | public function github_view_url() { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Returns the URL for the post on GitHub. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 3 | public function github_edit_url() { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Retrieve post type directory from blob path. |
||
| 249 | * |
||
| 250 | * @param string $path Path string. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function get_directory_from_path( $path ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Determines the last author to modify the post |
||
| 263 | * |
||
| 264 | * Returns Array an array containing the author name and email |
||
| 265 | */ |
||
| 266 | public function last_modified_author() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * The post's sha |
||
| 280 | * Cached as post meta, or will make a live call if need be |
||
| 281 | * |
||
| 282 | * Returns String the sha1 hash |
||
| 283 | */ |
||
| 284 | 6 | public function sha() { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Save the sha to post |
||
| 310 | * |
||
| 311 | * @param string $sha |
||
| 312 | */ |
||
| 313 | public function set_sha( $sha ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * The post's metadata |
||
| 319 | * |
||
| 320 | * Returns Array the post's metadata |
||
| 321 | */ |
||
| 322 | 5 | public function meta() { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Returns whether the Post has been saved in the DB yet. |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | 7 | public function is_new() { |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Sets the Post's meta. |
||
| 369 | * |
||
| 370 | * @param array $meta |
||
| 371 | */ |
||
| 372 | 7 | public function set_meta( $meta ) { |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the Post's arguments. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | 5 | public function get_args() { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the Post's meta. |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | 5 | public function get_meta() { |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Sets the Post's WP_Post object. |
||
| 396 | * |
||
| 397 | * @param WP_Post $post |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | public function set_post( WP_Post $post ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Transforms the Post into a Blob. |
||
| 410 | * |
||
| 411 | * @return WordPress_GitHub_Sync_Blob |
||
| 412 | */ |
||
| 413 | 3 | public function to_blob() { |
|
| 421 | } |
||
| 422 |