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:
1 | <?php |
||
11 | class Writing_On_GitHub_Export { |
||
12 | |||
13 | /** |
||
14 | * Application container. |
||
15 | * |
||
16 | * @var Writing_On_GitHub |
||
17 | */ |
||
18 | protected $app; |
||
19 | |||
20 | /** |
||
21 | * Initializes a new export manager. |
||
22 | * |
||
23 | * @param Writing_On_GitHub $app Application container. |
||
24 | */ |
||
25 | public function __construct( Writing_On_GitHub $app ) { |
||
28 | |||
29 | /** |
||
30 | * Updates all of the current posts in the database on master. |
||
31 | * |
||
32 | * @param bool $force |
||
33 | * |
||
34 | * @return string|WP_Error |
||
35 | */ |
||
36 | public function full( $force = false ) { |
||
37 | $posts = $this->app->database()->fetch_all_supported( $force ); |
||
38 | |||
39 | if ( is_wp_error( $posts ) ) { |
||
40 | /* @var WP_Error $posts */ |
||
41 | return $posts; |
||
42 | } |
||
43 | |||
44 | $error = ''; |
||
45 | |||
46 | View Code Duplication | foreach ( $posts as $post ) { |
|
|
|||
47 | $result = $this->update( $post->id() ); |
||
48 | if ( is_wp_error( $result ) ) { |
||
49 | /* @var WP_Error $result */ |
||
50 | $error = wogh_append_error( $error, $result ); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if ( is_wp_error( $error ) ) { |
||
55 | /* @var WP_Error $error */ |
||
56 | return $error; |
||
57 | } |
||
58 | |||
59 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Check if it exists in github |
||
65 | * @param int $post_id |
||
66 | * @return boolean |
||
67 | */ |
||
68 | protected function github_path( $post_id ) { |
||
77 | |||
78 | /** |
||
79 | * Updates the provided post ID in master. |
||
80 | * |
||
81 | * @param int $post_id Post ID to update. |
||
82 | * |
||
83 | * @return string|WP_Error |
||
84 | */ |
||
85 | public function update( $post_id ) { |
||
86 | $post = $this->app->database()->fetch_by_id( $post_id ); |
||
87 | |||
88 | if ( is_wp_error( $post ) ) { |
||
89 | /* @var WP_Error $post */ |
||
90 | return $post; |
||
91 | } |
||
92 | |||
93 | if ( 'trash' === $post->status() ) { |
||
94 | return $this->delete( $post_id ); |
||
95 | } |
||
96 | |||
97 | if ( $old_github_path = $this->github_path( $post->id() ) ) { |
||
98 | error_log("old_github_path: $old_github_path"); |
||
99 | $post->set_old_github_path($old_github_path); |
||
100 | } |
||
101 | |||
102 | $result = $this->new_posts( array( $post ) ); |
||
103 | |||
104 | if ( is_wp_error( $result ) ) { |
||
105 | /* @var WP_Error $result */ |
||
106 | return $result; |
||
107 | } |
||
108 | |||
109 | return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Updates GitHub-created posts with latest WordPress data. |
||
114 | * |
||
115 | * @param Writing_On_GitHub_Post[] $posts Array of Posts to create. |
||
116 | * |
||
117 | * @return string|WP_Error |
||
118 | */ |
||
119 | public function new_posts( array $posts ) { |
||
137 | |||
138 | protected function new_post( $post, $persist ) { |
||
139 | $github_path = $post->github_path(); |
||
203 | |||
204 | /** |
||
205 | * Deletes a provided post ID from master. |
||
206 | * |
||
207 | * @param int $post_id Post ID to delete. |
||
208 | * |
||
209 | * @return string|WP_Error |
||
210 | */ |
||
211 | public function delete( $post_id ) { |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Saves the export user to the database. |
||
245 | * |
||
246 | * @param int $user_id User ID to export with. |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function set_user( $user_id ) { |
||
253 | |||
254 | /** |
||
255 | * Gets the commit message tag. |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function get_commit_msg_tag() { |
||
268 | } |
||
269 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.