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() { |
||
136 | |||
137 | /** |
||
138 | * The post's YAML frontmatter |
||
139 | * |
||
140 | * Returns String the YAML frontmatter, ready to be written to the file |
||
141 | */ |
||
142 | public function front_matter() { |
||
145 | |||
146 | /** |
||
147 | * Returns the post_content |
||
148 | * |
||
149 | * Markdownify's the content if applicable |
||
150 | */ |
||
151 | public function post_content() { |
||
164 | |||
165 | public function old_github_path() { |
||
168 | |||
169 | public function set_old_github_path( $path ) { |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Retrieves or calculates the proper GitHub path for a given post |
||
177 | * |
||
178 | * Returns (string) the path relative to repo root |
||
179 | */ |
||
180 | public function github_path() { |
||
185 | |||
186 | /** |
||
187 | * Get GitHub directory based on post |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public function github_directory() { |
||
223 | |||
224 | /** |
||
225 | * Build GitHub filename based on post |
||
226 | */ |
||
227 | public function github_filename() { |
||
236 | |||
237 | /** |
||
238 | * Returns a post slug we can use in the GitHub filename |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | protected function get_name() { |
||
249 | |||
250 | /** |
||
251 | * is put on github |
||
252 | * @return boolean |
||
253 | */ |
||
254 | public function is_on_github() { |
||
262 | |||
263 | /** |
||
264 | * Returns the URL for the post on GitHub. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | View Code Duplication | public function github_view_url() { |
|
275 | |||
276 | /** |
||
277 | * Returns the URL for the post on GitHub. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | View Code Duplication | public function github_edit_url() { |
|
288 | |||
289 | /** |
||
290 | * Retrieve post type directory from blob path. |
||
291 | * |
||
292 | * @param string $path Path string. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function get_directory_from_path( $path ) { |
||
302 | |||
303 | /** |
||
304 | * Determines the last author to modify the post |
||
305 | * |
||
306 | * Returns Array an array containing the author name and email |
||
307 | */ |
||
308 | View Code Duplication | public function last_modified_author() { |
|
319 | |||
320 | /** |
||
321 | * The post's sha |
||
322 | * Cached as post meta, or will make a live call if need be |
||
323 | * |
||
324 | * Returns String the sha1 hash |
||
325 | */ |
||
326 | public function sha() { |
||
349 | |||
350 | /** |
||
351 | * Save the sha to post |
||
352 | * |
||
353 | * @param string $sha |
||
354 | */ |
||
355 | public function set_sha( $sha ) { |
||
358 | |||
359 | /** |
||
360 | * The post's metadata |
||
361 | * |
||
362 | * Returns Array the post's metadata |
||
363 | */ |
||
364 | public function meta() { |
||
401 | |||
402 | /** |
||
403 | * Returns whether the Post has been saved in the DB yet. |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function is_new() { |
||
410 | |||
411 | /** |
||
412 | * Sets the Post's meta. |
||
413 | * |
||
414 | * @param array $meta |
||
415 | */ |
||
416 | public function set_meta( $meta ) { |
||
419 | |||
420 | /** |
||
421 | * Returns the Post's arguments. |
||
422 | * |
||
423 | * @return array |
||
424 | */ |
||
425 | public function get_args() { |
||
428 | |||
429 | /** |
||
430 | * Returns the Post's meta. |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | public function get_meta() { |
||
437 | |||
438 | /** |
||
439 | * Get the blob |
||
440 | * @return Writing_On_GitHub_Blob |
||
441 | */ |
||
442 | public function get_blob() { |
||
445 | |||
446 | /** |
||
447 | * Set the blob |
||
448 | * @param Writing_On_GitHub_Blob $blob |
||
449 | */ |
||
450 | public function set_blob( Writing_On_GitHub_Blob $blob ) { |
||
453 | |||
454 | /** |
||
455 | * Sets the Post's WP_Post object. |
||
456 | * |
||
457 | * @param WP_Post $post |
||
458 | * |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function set_post( WP_Post $post ) { |
||
467 | |||
468 | /** |
||
469 | * Transforms the Post into a Blob. |
||
470 | * |
||
471 | * @return Writing_On_GitHub_Blob |
||
472 | */ |
||
473 | public function to_blob() { |
||
482 | } |
||
483 |
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
@return
annotation as described here.