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:
Complex classes like ReportingCloud often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReportingCloud, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ReportingCloud extends AbstractReportingCloud |
||
33 | { |
||
34 | /** |
||
35 | * GET methods |
||
36 | * ================================================================================================================= |
||
37 | */ |
||
38 | |||
39 | /** |
||
40 | * Check a corpus of text for spelling errors. |
||
41 | * |
||
42 | * Return an array of misspelled words, if spelling errors are found in the corpus of text. |
||
43 | * |
||
44 | * Return null, if no misspelled words are found in the corpus of text. |
||
45 | * |
||
46 | * @param string $text Corpus of text that should be spell checked |
||
47 | * @param string $language Language of specified text |
||
48 | * |
||
49 | * @return array|null |
||
50 | */ |
||
51 | 1 | public function proofingCheck($text, $language) |
|
73 | |||
74 | /** |
||
75 | * Return an array of available dictionaries on the Reporting Cloud service |
||
76 | * |
||
77 | * @return array|null |
||
78 | */ |
||
79 | 1 | View Code Duplication | public function getAvailableDictionaries() |
91 | |||
92 | /** |
||
93 | * Return an array of suggestions for a misspelled word. |
||
94 | * |
||
95 | * @param string $word Word that should be spell checked |
||
96 | * @param string $language Language of specified text |
||
97 | * @param int $max Maximum number of suggestions to return |
||
98 | * |
||
99 | * @return array|null |
||
100 | */ |
||
101 | 1 | public function getProofingSuggestions($word, $language, $max = 10) |
|
123 | |||
124 | /** |
||
125 | * Return an array of merge blocks and merge fields in a template file in template storage. |
||
126 | * |
||
127 | * @param string $templateName Template name |
||
128 | * |
||
129 | * @throws InvalidArgumentException |
||
130 | * |
||
131 | * @return array|null |
||
132 | */ |
||
133 | 1 | public function getTemplateInfo($templateName) |
|
153 | |||
154 | /** |
||
155 | * Return an array of binary data. |
||
156 | * Each record in the array is the binary data of a thumbnail image |
||
157 | * |
||
158 | * @param string $templateName Template name |
||
159 | * @param integer $zoomFactor Zoom factor |
||
160 | * @param integer $fromPage From page |
||
161 | * @param integer $toPage To page |
||
162 | * @param string $imageFormat Image format |
||
163 | * |
||
164 | * @throws InvalidArgumentException |
||
165 | * |
||
166 | * @return array|null |
||
167 | */ |
||
168 | 6 | public function getTemplateThumbnails($templateName, $zoomFactor, $fromPage, $toPage, $imageFormat) |
|
194 | |||
195 | /** |
||
196 | * Return the number of templates in template storage |
||
197 | * |
||
198 | * @return integer |
||
199 | */ |
||
200 | 1 | public function getTemplateCount() |
|
204 | |||
205 | /** |
||
206 | * Return an array properties for the templates in template storage |
||
207 | * |
||
208 | * @return array|null |
||
209 | */ |
||
210 | 1 | View Code Duplication | public function getTemplateList() |
230 | |||
231 | /** |
||
232 | * Return the number of pages in a template in template storage |
||
233 | * |
||
234 | * @param string $templateName Template name |
||
235 | * |
||
236 | * @throws InvalidArgumentException |
||
237 | * |
||
238 | * @return integer |
||
239 | */ |
||
240 | 2 | View Code Duplication | public function getTemplatePageCount($templateName) |
250 | |||
251 | /** |
||
252 | * Return true, if the template exists in template storage |
||
253 | * |
||
254 | * @param string $templateName Template name |
||
255 | * |
||
256 | * @throws InvalidArgumentException |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | 2 | View Code Duplication | public function templateExists($templateName) |
270 | |||
271 | /** |
||
272 | * Return an array of available fonts on the Reporting Cloud service |
||
273 | * |
||
274 | * @return array|null |
||
275 | */ |
||
276 | 1 | View Code Duplication | public function getFontList() |
288 | |||
289 | /** |
||
290 | * Return an array properties for the ReportingCloud account |
||
291 | * |
||
292 | * @return array|null |
||
293 | */ |
||
294 | 1 | View Code Duplication | public function getAccountSettings() |
312 | |||
313 | /** |
||
314 | * Download the binary data of a template from template storage |
||
315 | * |
||
316 | * @param string $templateName Template name |
||
317 | * |
||
318 | * @throws InvalidArgumentException |
||
319 | * |
||
320 | * @return null|resource |
||
321 | */ |
||
322 | 2 | public function downloadTemplate($templateName) |
|
340 | |||
341 | /** |
||
342 | * Execute a GET request via REST client |
||
343 | * |
||
344 | * @param string $uri URI |
||
345 | * @param array $query Query |
||
346 | * |
||
347 | * @return mixed|null |
||
348 | */ |
||
349 | 12 | protected function get($uri, $query = []) |
|
365 | |||
366 | |||
367 | /** |
||
368 | * POST methods |
||
369 | * ================================================================================================================= |
||
370 | */ |
||
371 | |||
372 | /** |
||
373 | * Upload a template to template storage |
||
374 | * |
||
375 | * @param string $templateFilename Template name |
||
376 | * |
||
377 | * @throws InvalidArgumentException |
||
378 | * |
||
379 | * @return bool |
||
380 | */ |
||
381 | 13 | public function uploadTemplate($templateFilename) |
|
411 | |||
412 | /** |
||
413 | * Convert a document on the local file system to a different format |
||
414 | * |
||
415 | * @param string $documentFilename Document filename |
||
416 | * @param string $returnFormat Return format |
||
417 | * |
||
418 | * @throws InvalidArgumentException |
||
419 | * |
||
420 | * @return null|resource |
||
421 | */ |
||
422 | 6 | public function convertDocument($documentFilename, $returnFormat) |
|
452 | |||
453 | /** |
||
454 | * Merge data into a template and return an array of binary data. |
||
455 | * Each record in the array is the binary data of one document |
||
456 | * |
||
457 | * @param array $mergeData Array of merge data |
||
458 | * @param string $returnFormat Return format |
||
459 | * @param string $templateName Template name |
||
460 | * @param string $templateFilename Template filename on local file system |
||
461 | * @param boolean $append Append flag |
||
462 | * @param array $mergeSettings Array of merge settings |
||
463 | * |
||
464 | * @throws InvalidArgumentException |
||
465 | * |
||
466 | * @return null|string |
||
467 | */ |
||
468 | 13 | public function mergeDocument($mergeData, $returnFormat, $templateName = null, $templateFilename = null, $append = null, $mergeSettings = []) |
|
530 | |||
531 | /** |
||
532 | * Perform find and replace in document and return binary data. |
||
533 | * |
||
534 | * @param array $findAndReplaceData Array of find and replace data |
||
535 | * @param string $returnFormat Return format |
||
536 | * @param string $templateName Template name |
||
537 | * @param string $templateFilename Template filename on local file system |
||
538 | * @param array $mergeSettings Array of merge settings |
||
539 | * |
||
540 | * @throws InvalidArgumentException |
||
541 | * |
||
542 | * @return null|string |
||
543 | */ |
||
544 | 11 | public function findAndReplaceDocument($findAndReplaceData, $returnFormat, $templateName = null, $templateFilename = null, $mergeSettings = []) |
|
598 | |||
599 | |||
600 | /** |
||
601 | * DELETE methods |
||
602 | * ================================================================================================================= |
||
603 | */ |
||
604 | |||
605 | /** |
||
606 | * Delete a template in template storage |
||
607 | * |
||
608 | * @param string $templateName Template name |
||
609 | * |
||
610 | * @throws InvalidArgumentException |
||
611 | * |
||
612 | * @return bool |
||
613 | */ |
||
614 | 11 | public function deleteTemplate($templateName) |
|
636 | } |
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.