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 |
||
12 | final class PostsApi extends HttpApi |
||
13 | { |
||
14 | /** |
||
15 | * Create a post. Required parameters: 'channel_id', 'message'. |
||
16 | * |
||
17 | * @param array $params |
||
18 | * |
||
19 | * @return Post|ResponseInterface |
||
20 | */ |
||
21 | 8 | public function createPost(array $params) |
|
27 | |||
28 | /** |
||
29 | * Partially update a post by providing only the fields you want to update. |
||
30 | * Omitted fields will not be updated. |
||
31 | * |
||
32 | * @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1patch%2Fput |
||
33 | * |
||
34 | * @param string $postId |
||
35 | * @param array $params |
||
36 | * |
||
37 | * @return Post|ResponseInterface |
||
38 | */ |
||
39 | 9 | View Code Duplication | public function patchPost(string $postId, array $params) |
49 | |||
50 | /** |
||
51 | * Update a post. |
||
52 | * |
||
53 | * @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D%2Fput |
||
54 | * |
||
55 | * @param string $postId ID of the post to update |
||
56 | * @param array $params |
||
57 | * |
||
58 | * @return Post|ResponseInterface |
||
59 | */ |
||
60 | 9 | View Code Duplication | public function updatePost(string $postId, array $params) |
70 | |||
71 | /** |
||
72 | * Get a single post. |
||
73 | * |
||
74 | * @param string $postId ID of the post to get |
||
75 | * |
||
76 | * @return Post|ResponseInterface |
||
77 | */ |
||
78 | 9 | public function getPost(string $postId) |
|
88 | |||
89 | /** |
||
90 | * Pin a post to a channel it is in based from the provided post id string. |
||
91 | * |
||
92 | * @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1pin%2Fpost |
||
93 | * |
||
94 | * @param string $postId Post GUID |
||
95 | * |
||
96 | * @return Status|ResponseInterface |
||
97 | */ |
||
98 | 9 | View Code Duplication | public function pinPost($postId) |
108 | |||
109 | /** |
||
110 | * Unpin a post to a channel it is in based from the provided post id string. |
||
111 | * |
||
112 | * @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1unpin%2Fpost |
||
113 | * |
||
114 | * @param string $postId Post GUID |
||
115 | * |
||
116 | * @return Status|ResponseInterface |
||
117 | */ |
||
118 | 9 | View Code Duplication | public function unpinPost($postId) |
128 | |||
129 | /** |
||
130 | * Soft deletes a post, by marking the post as deleted in the database. Soft deleted posts will not be returned in post queries. |
||
131 | * |
||
132 | * @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D%2Fdelete |
||
133 | * |
||
134 | * @param string $postId ID of the post to delete |
||
135 | * |
||
136 | * @return Status|ResponseInterface |
||
137 | */ |
||
138 | 9 | public function deletePost(string $postId) |
|
148 | } |
||
149 |
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.