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 |
||
14 | final class FilesApi extends HttpApi |
||
15 | { |
||
16 | /** |
||
17 | * Uploads a file that can later be attached to a post. |
||
18 | * |
||
19 | * @see: https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files%2Fpost |
||
20 | * |
||
21 | * @param string $fileContents The file contents to send |
||
22 | * @param string $filename The filename to be used |
||
23 | * @param string $channelId The ID of the channel that this file will be uploaded to |
||
24 | * @param string|null $clientId A unique identifier for the file that will be returned in the response |
||
25 | * |
||
26 | * @return FileUploadInfo|ResponseInterface |
||
27 | */ |
||
28 | public function sendFile(string $fileContents, string $filename, string $channelId, string $clientId = null) |
||
58 | |||
59 | /** |
||
60 | * Get metadata for a file. |
||
61 | * |
||
62 | * @param string $fileId The ID of the file info to get |
||
63 | * |
||
64 | * @see https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files~1%7Bfile_id%7D~1info%2Fget |
||
65 | * |
||
66 | * @return FileInfo|ResponseInterface |
||
67 | */ |
||
68 | 9 | View Code Duplication | public function getFileInfo(string $fileId) |
78 | |||
79 | /** |
||
80 | * Gets a file that has been uploaded previously. |
||
81 | * |
||
82 | * @param string $fileId The ID of the file to get |
||
83 | * |
||
84 | * @return StreamInterface |
||
85 | */ |
||
86 | 9 | View Code Duplication | public function getFile(string $fileId) |
100 | |||
101 | /** |
||
102 | * Gets a public link for a file that can be accessed without logging into Mattermost. |
||
103 | * |
||
104 | * @param string $fileId The ID of the file to get a link for |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 9 | View Code Duplication | public function getFileLink(string $fileId) |
122 | } |
||
123 |
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.