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 Writing_On_GitHub_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.
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 Writing_On_GitHub_Post, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Writing_On_GitHub_Post { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Api object |
||
| 14 | * |
||
| 15 | * @var Writing_On_GitHub_Api |
||
| 16 | */ |
||
| 17 | public $api; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Post ID |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | public $id = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Blob object |
||
| 27 | * @var Writing_On_GitHub_Blob |
||
| 28 | */ |
||
| 29 | public $blob; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Post object |
||
| 33 | * @var WP_Post |
||
| 34 | */ |
||
| 35 | public $post; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Post args. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $args; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Post meta. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $meta; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Whether the post has been saved. |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $new = true; |
||
| 57 | |||
| 58 | |||
| 59 | protected $old_github_path; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Instantiates a new Post object |
||
| 63 | * |
||
| 64 | * @param int|array $id_or_args Either a post ID or an array of arguments. |
||
| 65 | * @param Writing_On_GitHub_Api $api API object. |
||
| 66 | * |
||
| 67 | * @todo remove database operations from this method |
||
| 68 | */ |
||
| 69 | public function __construct( $id_or_args, Writing_On_GitHub_Api $api ) { |
||
| 93 | |||
| 94 | public function id() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the post type |
||
| 100 | */ |
||
| 101 | public function type() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the post type |
||
| 107 | */ |
||
| 108 | public function status() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the post name |
||
| 114 | */ |
||
| 115 | public function name() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns true if the post has a password |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function has_password() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Combines the 2 content parts for GitHub |
||
| 129 | */ |
||
| 130 | public function github_content() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The post's YAML frontmatter |
||
| 145 | * |
||
| 146 | * Returns String the YAML frontmatter, ready to be written to the file |
||
| 147 | */ |
||
| 148 | public function front_matter() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the post_content |
||
| 154 | * |
||
| 155 | * Markdownify's the content if applicable |
||
| 156 | */ |
||
| 157 | public function post_content() { |
||
| 170 | |||
| 171 | public function old_github_path() { |
||
| 174 | |||
| 175 | public function set_old_github_path( $path ) { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Retrieves or calculates the proper GitHub path for a given post |
||
| 183 | * |
||
| 184 | * Returns (string) the path relative to repo root |
||
| 185 | */ |
||
| 186 | public function github_path() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get GitHub directory based on post |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function github_directory() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Build GitHub filename based on post |
||
| 232 | */ |
||
| 233 | public function github_filename() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Returns a post slug we can use in the GitHub filename |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function get_name() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * is put on github |
||
| 258 | * @return boolean |
||
| 259 | */ |
||
| 260 | public function is_on_github() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns the URL for the post on GitHub. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function github_view_url() { |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the URL for the post on GitHub. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function github_edit_url() { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Retrieve post type directory from blob path. |
||
| 297 | * |
||
| 298 | * @param string $path Path string. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function get_directory_from_path( $path ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Determines the last author to modify the post |
||
| 311 | * |
||
| 312 | * Returns Array an array containing the author name and email |
||
| 313 | */ |
||
| 314 | View Code Duplication | public function last_modified_author() { |
|
| 325 | |||
| 326 | /** |
||
| 327 | * The post's sha |
||
| 328 | * Cached as post meta, or will make a live call if need be |
||
| 329 | * |
||
| 330 | * Returns String the sha1 hash |
||
| 331 | */ |
||
| 332 | public function sha() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Save the sha to post |
||
| 358 | * |
||
| 359 | * @param string $sha |
||
| 360 | */ |
||
| 361 | public function set_sha( $sha ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * The post's metadata |
||
| 367 | * |
||
| 368 | * Returns Array the post's metadata |
||
| 369 | */ |
||
| 370 | public function meta() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns whether the Post has been saved in the DB yet. |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function is_new() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Sets the Post's meta. |
||
| 419 | * |
||
| 420 | * @param array $meta |
||
| 421 | */ |
||
| 422 | public function set_meta( $meta ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the Post's arguments. |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | public function get_args() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the Post's meta. |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | public function get_meta() { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get the blob |
||
| 446 | * @return Writing_On_GitHub_Blob |
||
| 447 | */ |
||
| 448 | public function get_blob() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set the blob |
||
| 454 | * @param Writing_On_GitHub_Blob $blob |
||
| 455 | */ |
||
| 456 | public function set_blob( Writing_On_GitHub_Blob $blob ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Sets the Post's WP_Post object. |
||
| 462 | * |
||
| 463 | * @param WP_Post $post |
||
| 464 | * |
||
| 465 | * @return $this |
||
| 466 | */ |
||
| 467 | public function set_post( WP_Post $post ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Transforms the Post into a Blob. |
||
| 476 | * |
||
| 477 | * @return Writing_On_GitHub_Blob |
||
| 478 | */ |
||
| 479 | public function to_blob() { |
||
| 488 | } |
||
| 489 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.