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 WordPress_GitHub_Sync_Export { |
||
12 | |||
13 | /** |
||
14 | * Option key for export user. |
||
15 | */ |
||
16 | const EXPORT_USER_OPTION = '_wpghs_export_user_id'; |
||
17 | |||
18 | /** |
||
19 | * Application container. |
||
20 | * |
||
21 | * @var WordPress_GitHub_Sync |
||
22 | */ |
||
23 | protected $app; |
||
24 | |||
25 | /** |
||
26 | * Initializes a new export manager. |
||
27 | * |
||
28 | * @param WordPress_GitHub_Sync $app Application container. |
||
29 | */ |
||
30 | 19 | public function __construct( WordPress_GitHub_Sync $app ) { |
|
33 | |||
34 | /** |
||
35 | * Updates all of the current posts in the database on master. |
||
36 | * |
||
37 | * @return string|WP_Error |
||
38 | */ |
||
39 | 5 | View Code Duplication | public function full() { |
75 | |||
76 | /** |
||
77 | * Updates the provided post ID in master. |
||
78 | * |
||
79 | * @param int $post_id Post ID to update. |
||
80 | * |
||
81 | * @return string|WP_Error |
||
82 | */ |
||
83 | 5 | public function update( $post_id ) { |
|
122 | |||
123 | /** |
||
124 | * Updates GitHub-created posts with latest WordPress data. |
||
125 | * |
||
126 | * @param array<WordPress_GitHub_Sync_Post> $posts Array of Posts to create. |
||
127 | * |
||
128 | * @return string|WP_Error |
||
129 | */ |
||
130 | 4 | public function new_posts( array $posts ) { |
|
160 | |||
161 | /** |
||
162 | * Deletes a provided post ID from master. |
||
163 | * |
||
164 | * @param int $post_id Post ID to delete. |
||
165 | * |
||
166 | * @return string|WP_Error |
||
167 | */ |
||
168 | 4 | View Code Duplication | public function delete( $post_id ) { |
203 | |||
204 | /** |
||
205 | * Use the new tree to save sha data |
||
206 | * for all the updated posts. |
||
207 | * |
||
208 | * @param WordPress_GitHub_Sync_Post[] $posts Posts to fetch updated shas for. |
||
209 | * |
||
210 | * @return string|WP_Error |
||
211 | */ |
||
212 | 6 | protected function update_shas( array $posts ) { |
|
237 | |||
238 | /** |
||
239 | * Saves the export user to the database. |
||
240 | * |
||
241 | * @param int $user_id User ID to export with. |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 1 | public function set_user( $user_id ) { |
|
248 | |||
249 | /** |
||
250 | * Gets the commit message tag. |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 11 | protected function get_commit_msg_tag() { |
|
263 | } |
||
264 |
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.