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 |
||
32 | class Messages extends AbstractResource |
||
33 | { |
||
34 | /** |
||
35 | * List of messages in a room in historical reversed order. |
||
36 | * |
||
37 | * @param string $roomId Room id |
||
38 | * @param string|null $query Optional search query |
||
39 | * @return \Generator |
||
40 | * @throws \RuntimeException |
||
41 | * @throws \InvalidArgumentException |
||
42 | * @throws \Throwable |
||
43 | * @throws \Exception |
||
44 | * @throws \GuzzleHttp\Exception\ClientException |
||
45 | */ |
||
46 | public function all(string $roomId, string $query = null): \Generator |
||
50 | |||
51 | /** |
||
52 | * Returns all messages before target message id. |
||
53 | * |
||
54 | * @param string $roomId |
||
55 | * @param string|null $beforeId |
||
56 | * @param string|null $query |
||
57 | * @return \Generator |
||
58 | * @throws \Exception |
||
59 | * @throws \GuzzleHttp\Exception\ClientException |
||
60 | * @throws \InvalidArgumentException |
||
61 | * @throws \RuntimeException |
||
62 | * @throws \Throwable |
||
63 | */ |
||
64 | View Code Duplication | public function allBeforeId(string $roomId, string $beforeId = null, string $query = null) |
|
83 | |||
84 | /** |
||
85 | * @param string $roomId |
||
86 | * @param int $limit |
||
87 | * @param string|null $query |
||
88 | * @return Route |
||
89 | */ |
||
90 | private function routeForMessagesIterator(string $roomId, int $limit, string $query = null): Route |
||
102 | |||
103 | /** |
||
104 | * Returns all messages after target message id. |
||
105 | * |
||
106 | * @param string $roomId |
||
107 | * @param string|null $afterId |
||
108 | * @param string|null $query |
||
109 | * @return \Generator |
||
110 | * @throws \Exception |
||
111 | * @throws \GuzzleHttp\Exception\ClientException |
||
112 | * @throws \InvalidArgumentException |
||
113 | * @throws \RuntimeException |
||
114 | * @throws \Throwable |
||
115 | */ |
||
116 | View Code Duplication | public function allAfterId(string $roomId, string $afterId = null, string $query = null) |
|
135 | |||
136 | /** |
||
137 | * There is also a way to retrieve a single message using its id. |
||
138 | * |
||
139 | * @param string $roomId Room id |
||
140 | * @param string $messageId Message id |
||
141 | * @return array |
||
142 | * @throws \RuntimeException |
||
143 | * @throws \InvalidArgumentException |
||
144 | * @throws \Throwable |
||
145 | * @throws \Exception |
||
146 | * @throws \GuzzleHttp\Exception\ClientException |
||
147 | */ |
||
148 | public function find(string $roomId, string $messageId): array |
||
155 | |||
156 | /** |
||
157 | * Send a message to a room. |
||
158 | * |
||
159 | * @param string $roomId Room id |
||
160 | * @param string $content Message body |
||
161 | * @return array |
||
162 | * @throws \RuntimeException |
||
163 | * @throws \InvalidArgumentException |
||
164 | * @throws \Throwable |
||
165 | * @throws \Exception |
||
166 | * @throws \GuzzleHttp\Exception\ClientException |
||
167 | */ |
||
168 | public function create(string $roomId, string $content): array |
||
176 | |||
177 | /** |
||
178 | * Delete a message. |
||
179 | * |
||
180 | * @param string $roomId |
||
181 | * @param string $messageId |
||
182 | * @return array |
||
183 | * @throws \RuntimeException |
||
184 | * @throws \InvalidArgumentException |
||
185 | * @throws \Throwable |
||
186 | * @throws \Exception |
||
187 | * @throws \GuzzleHttp\Exception\ClientException |
||
188 | */ |
||
189 | public function delete(string $roomId, string $messageId): array |
||
193 | |||
194 | /** |
||
195 | * Update a message. |
||
196 | * |
||
197 | * @param string $roomId Room id |
||
198 | * @param string $messageId Message id |
||
199 | * @param string $content New message body |
||
200 | * @return array |
||
201 | * @throws \RuntimeException |
||
202 | * @throws \InvalidArgumentException |
||
203 | * @throws \Throwable |
||
204 | * @throws \Exception |
||
205 | * @throws \GuzzleHttp\Exception\ClientException |
||
206 | */ |
||
207 | public function update(string $roomId, string $messageId, string $content): array |
||
215 | } |
||
216 |
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.