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 |
||
30 | class EmailValidationV4 extends HttpApi |
||
31 | { |
||
32 | /** |
||
33 | * Addresses are validated based off defined checks. |
||
34 | * |
||
35 | * @param string $address An email address to validate. Maximum: 512 characters. |
||
36 | * @param bool $providerLookup A provider lookup will be performed if Mailgun’s internal analysis is insufficient |
||
37 | * |
||
38 | * @return ValidateResponse|ResponseInterface |
||
39 | * |
||
40 | * @throws Exception Thrown when we don't catch a Client or Server side Exception |
||
41 | */ |
||
42 | 2 | View Code Duplication | public function validate(string $address, bool $providerLookup = true) |
55 | |||
56 | /** |
||
57 | * @param string $listId ID given when the list created |
||
58 | * @param mixed $filePath File path or file content |
||
59 | * |
||
60 | * @return mixed|ResponseInterface |
||
61 | * |
||
62 | * @throws Exception |
||
63 | */ |
||
64 | 1 | View Code Duplication | public function createBulkJob(string $listId, $filePath) |
85 | |||
86 | /** |
||
87 | * @param string $listId ID given when the list created |
||
88 | * |
||
89 | * @return DeleteBulkJobResponse|ResponseInterface |
||
90 | * |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | 1 | View Code Duplication | public function deleteBulkJob(string $listId) |
101 | |||
102 | /** |
||
103 | * @param string $listId ID given when the list created |
||
104 | * |
||
105 | * @return GetBulkJobResponse|ResponseInterface |
||
106 | * |
||
107 | * @throws Exception |
||
108 | */ |
||
109 | 1 | View Code Duplication | public function getBulkJob(string $listId) |
117 | |||
118 | /** |
||
119 | * @param int $limit Jobs limit |
||
120 | * |
||
121 | * @return GetBulkJobsResponse|ResponseInterface |
||
122 | * |
||
123 | * @throws Exception |
||
124 | */ |
||
125 | 1 | View Code Duplication | public function getBulkJobs(int $limit = 500) |
135 | |||
136 | /** |
||
137 | * @param int $limit Previews Limit |
||
138 | * |
||
139 | * @return mixed|ResponseInterface |
||
140 | * |
||
141 | * @throws Exception |
||
142 | */ |
||
143 | 1 | View Code Duplication | public function getBulkPreviews(int $limit = 500) |
153 | |||
154 | /** |
||
155 | * @param string $previewId ID given when the list created |
||
156 | * @param mixed $filePath File path or file content |
||
157 | * |
||
158 | * @return mixed|ResponseInterface |
||
159 | * |
||
160 | * @throws Exception |
||
161 | */ |
||
162 | 1 | View Code Duplication | public function createBulkPreview(string $previewId, $filePath) |
183 | |||
184 | /** |
||
185 | * @param string $previewId ID given when the list created |
||
186 | * |
||
187 | * @return mixed|ResponseInterface |
||
188 | * |
||
189 | * @throws Exception |
||
190 | */ |
||
191 | 1 | View Code Duplication | public function getBulkPreview(string $previewId) |
199 | |||
200 | /** |
||
201 | * @param string $previewId ID given when the list created |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | 1 | public function deleteBulkPreview(string $previewId) |
|
213 | |||
214 | /** |
||
215 | * @param string $previewId ID given when the list created |
||
216 | * |
||
217 | * @return mixed|ResponseInterface |
||
218 | * |
||
219 | * @throws Exception |
||
220 | */ |
||
221 | 1 | public function promoteBulkPreview(string $previewId) |
|
229 | |||
230 | /** |
||
231 | * @param string $fieldName Field Name |
||
232 | * @param array $filePath ['fileContent' => 'content'] or ['filePath' => '/foo/bar'] |
||
233 | * |
||
234 | * @return array File Data |
||
235 | */ |
||
236 | 2 | private function prepareFile(string $fieldName, array $filePath): array |
|
259 | |||
260 | /** |
||
261 | * Close open resources. |
||
262 | * |
||
263 | * @param array $params Resource params |
||
264 | */ |
||
265 | 2 | View Code Duplication | private function closeResources(array $params): void |
273 | } |
||
274 |
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.